首页 > 其他分享 >【机译】ABP Helper Methods

【机译】ABP Helper Methods

时间:2023-06-19 10:12:07浏览次数:44  
标签:string Helper 扩展 Check ABP OneTimeRunner 字符串 方法 机译

在编码时,我们经常使用扩展方法和辅助方法。它们帮助我们以声明的方式实现通用操作。我们使用它们将文本分成几行,按条件查询集合,从一种类型转换为另一种类型,以及用于许多其他目的……

在本文中,我想展示ABP框架定义和使用的一些有用的扩展方法。我个人在日常代码中经常使用它们,它们在许多情况下可以减少开发时间并防止重复。

以下是我们将在本文中介绍的扩展方法和帮助程序方法的列表:

Check 类(及其方法,如 NotNullOrWhiteSpaceLength
OneTimeRunner
AbpDictionaryExtensions 类的一些扩展方法(TryGetValueGetOrDefaultGetOrAdd
AbpEnumerableExtensions 类的一些扩展方法( JoinAsStringWhereIf
AbpStringExtensions 类的一些扩展方法( EnsureEndsWithEnsureStartsWithNormalizeLineEndingsRemovePostFixSplitToLinesTruncateWithPostfix
AbpTypeExtensions 类的一些扩展方法(IsAssignableToGetBaseClasses
CurrentUserExtensions 类的一些扩展方法(GetIdFindClaimValue
我的目标是使这篇文章成为实用指南,这样就不会对这些方法进行冗长的描述,相反,我将在一个句子中描述它们,然后展示一个用法示例。另外,我的目标是保持本文的更新,每当我开始使用扩展方法或帮助程序类时,我都会尝试将其添加到本文中。您还可以在下面评论您在日常代码中使用的扩展/帮助程序方法,以便我可以将它们添加到文章中。

Check 类

可以在实体的构造函数中使用 Check.NotNullCheck.NotNullOrWhiteSpace 方法,以确保实体的完整性,如下所示:

public class Book : AggregateRoot<Guid>
{
    public string Name { get; set; }

    public string Description { get; set; }

    //...

    public Book(Guid id, string name, string description) : base(id) 
    {
        Name = Check.NotNullOrWhiteSpace(name, nameof(name)); //checks if the string is null or white space and throws an exception if it is

        Description = Check.Length(description, nameof(description), BookConsts.MaxDescriptionLength); //checks the max length of the given description and throws an exception if it exceeds the length
    }
}

除了这两个方法之外, Check 类还提供了许多其他有用的方法,例如 Check.NotNull<>Check.NotNullOrEmptyAssignableTo<>RangePositive 。您可以看到其他方法的 Check 类。

OneTimeRunner 类

OneTimeRunner 是一个实用程序类,它保证每个应用程序只执行一次代码块,即使多次调用它也是如此。

private readonly static OneTimeRunner OneTimeRunner = new OneTimeRunner();

for(int i = 1; i < 10; i++) 
{
    OneTimeRunner.Run(() => 
    {
        Console.WriteLine("it's called: " + i + " time(s)");
    });
}

//Prints: "it's called 1 time(s)"

ABP框架在很多地方都使用这个类.例如,ABP框架在模块类中使用此类来配置全局功能,如下所示:

private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
public override void PreConfigureServices(ServiceConfigurationContext context)
{
  OneTimeRunner.Run(() =>
  {
  	GlobalFeatureManager.Instance.Enable<PaymentFeature>();
  });
}

每当需要确保代码块仅运行一次时,都可以使用此类(例如,在应用程序启动时使用此类可能很有用)

Dictionary 扩展

AbpDictionaryExtensions 类提供了有用的方法,例如 TryGetValueGetOrDefaultGetOrAdd

示例:

//check if the item exists in the dictionary
if(JobQueues.TryGetValue(jobName, our var jobQueue)) 
{
    //perform operations on the job queue
}

JobQueues.GetOrDefault(jobName): //return the jobQueue if exists otherwise returns null

//tries to get the job queue, if it can not find adds a new item to the dictionary and returns it.
JobQueues.GetOrAdd(
    jobName, 
    () => new JobQueue(jobName)
);
  • TryGetValue :用于尝试获取字典中的值(如果存在)。如果该值存在,则返回 true 和字典项的值。否则,返回 false 和字典项的默认值(对于引用类型为 null,对于数字值为 0,等等)。
  • GetOrDefault:从字典中获取具有给定键的值。如果找不到,则返回默认值。
  • GetOrAdd:使用给定键从字典中获取值。如果找不到值,它会根据将其作为参数提供的工厂方法创建一个新值。

Enumerable 扩展

AbpEnumerableExtensions 类提供了有用的方法,例如 JoinAsString 和 WhereIf 。此外,此类中还定义了其他扩展方法。它们在查询中使用特别有用。

示例:

//Concatenates the book names separated with comma
var bookNamesSeperatedWithComma = bookNames.JoinAsString(".");

//if startTime is not null, then apply the filter 
var identitySecurityLog = securityLogs.WhereIf(
            startTime.HasValue, //condition
            securityLog => securityLog.CreationTime >= startTime.Value //filter
        )
        .First();
  • JoinAsString:连接集合的成员,在每个成员之间使用指定的分隔符。在上面的例子中,我们用它来将所有 bookNames 存储在一个 string 字段中,用逗号分隔(“bookName1.bookName2.....”)。
  • WhereIf:如果给定条件为 true,则按给定谓词筛选列表。

String 扩展

我经常使用 AbpStringExtension 类的扩展方法。以下是其中的一些:

  • EnsureEndsWith:如果给定字符串不以字符结尾,则在给定字符串的末尾添加一个字符。

  • EnsureStartsWith:如果给定字符串不以字符开头,则向该字符串的开头添加字符。

  • NormalizeLineEndings:将字符串中的行尾转换为 Environment.NewLine

  • RemovePostFix:从给定字符串的末尾删除给定后缀的第一个匹配项。

  • SplitToLines:使用字符串。拆分方法将给定字符串拆分为 Environment.NewLine

  • TruncateWithPostfix:如果字符串超过最大长度,则从字符串的开头获取字符串的子字符串。它添加了一个“...”后缀到字符串的末尾(如果字符串被截断)。

示例:

var detailPage = url.EnsureEndsWith("/") + "Detail"; //add the /detail to the end of the URL

var urlWithHttps = url.EnsureStartsWith("https://"); //ensure the given URL starts with "https://"

//uses the Environment.NewLine for line endings, replaces \n with Environment.NewLine (makes it environment agnostic)
var normalizedContent = fileContent.NormalizeLineEndings(); 

//removes post fix according to the given string
var classNameWithoutManagerPostfix = nameof(MyDomainManager).RemovePostFix("Manager");

//splits a string content to lines, to a string array
string[] lines = fileContent.SplitToLines();

//if the description's length exceeds 247, it truncates the string and adds postfix (...)
var truncatedDescription = post.Description.TruncateWithPostfix(247, "...");

Type 扩展

AbpTypeExtensions 类提供有用的类型扩展,例如 GetFullNameWithAssemblyNameIsAssignableTo 方法。

例如,我们可以使用 GetFullNameWithAssemblyName 方法在异常消息中输出类型的程序集全名,以便于诊断错误:

throw new ArgumentException($"should be assignable to {typeof(MyBaseClass).GetFullNameWithAssemblyName()}");

另一方面, IsAssignableTo 扩展方法可用于检查类是从基类继承还是实现接口:

if(typeof(TEntity).IsAssignableTo<IHasCreationTime>())
{
    //TEntity should implement IHasCreationTime
}

它在内部使用 Type.IsAssignableFrom 方法。

Current User 扩展

您可能知道,我们可以从 ICurrentUserCurrentUser 类中获取有关当前用户的信息。我们可以获取当前用户的 Id ,检查当前用户是否经过身份验证 (IsAuthenticated) 等。

CurrentUser.Id 是可为空的属性。因为用户可能尚未通过身份验证。但是,有时您可能期望对当前用户进行身份验证才能查看页面并根据当前用户执行某些操作。对于这种情况,我们知道 CurrentUser.Id 不能为空。因此,我们可以通过使用 GetId 扩展方法将当前用户的 Id 作为不可为空的 Guid 类型获取:

Guid? currentUserId = CurrentUser.Id; //we need to check if it's null or not to use it

Guid currentUserId = CurrentUser.GetId();

此外,还有一些其他扩展方法(如 FindClaimValue 方法)可能很有用。例如,我们可以使用此扩展方法获取当前用户的电话号码,如下所示:

string phoneNumber = CurrentUser.FindClaimValue(AbpClaimTypes.PhoneNumber);

感谢您阅读这篇文章,下一篇见。(转:https://engincanv.github.io/abp/2023/06/10/abp-helper-methods.html)

标签:string,Helper,扩展,Check,ABP,OneTimeRunner,字符串,方法,机译
From: https://www.cnblogs.com/HUGO_CM/p/17490330.html

相关文章

  • ABP点滴:API无权访问资源时,返回 PolicyName 信息
    ABP无权访问API时,返回的是403Forbidden和重定向Localtion,但不知道具体是哪个Policy受阻。整改思路:重写MethodInvocationAuthorizationService,抛出AbpAuthorizationException异常时附带PolicyName重写DefaultAbpAuthorizationExceptionHandler,在http响应头中增加上......
  • 打工笔记--------------------------弄了一个还不错的NPOI的helper类
    `usingNPOI.HSSF.UserModel;usingNPOI.SS.UserModel;usingNPOI.SS.Util;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingSystem.Text;namespaceUtils.Public{publicpartialclassNPOIHelper{/......
  • ABP框架中UnitOfWorkManager.Current.SetTenantId()并不是修改AbpSession.TenantId的
    1.结论UnitOfWorkManager.Current.SetTenantId()修改的是ABP过滤器中使用的TenantId,并不会修改AbpSession.TenantId代码演示:2.关于UnitOfWorkManager.Current.SetTenantId()方法的作用前提:ABP框架是是支持多租户的,对于单数据库的多租户设计,需要通过TenantId来区分宿主和......
  • RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper
    在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间。压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式。压缩与解压缩的方法也很多,在.NET2.0开始,在System.IO.Compression中微软已经给我们提供了解压缩的方法GZipStream。对于GZipSt......
  • maven-compiler-plugin build-helper-maven-plugin Maven-assembly-plugin
    三个插件都是是用干啥的maven-compiler-plugin进行源代码的编译build-helper-maven-plugin项目中添加额外的资源、源代码、构建输出目录和依赖项等Maven-assembly-plugin生成可执行jar包<build><plugins><plugin><groupId......
  • Edge浏览器的JSON格式化插件(FeHelper)不生效
    问题有时候懒得打开ApiPost测试接口,就直接使用Edge浏览器进行接口测试,但是最近格式化出了问题,FeHelper插件的JSON格式化不生效,不对,也不能说是完全不生效吧,测试非本地的接口倒是可以本地接口(奇怪的格式化,好丑......
  • github PageHelper 分页工具类
    分页工具类importcn.hutool.core.lang.Assert;importcom.github.pagehelper.PageInfo;importcom.google.common.collect.Lists;importorg.springframework.util.CollectionUtils;importjava.util.Collections;importjava.util.Iterator;importjava.util.List;imp......
  • ABP入门教程4 - 初始化运行
    点这里进入ABP入门教程目录 编译解决方案重新生成解决方案,确保生成成功。连接数据库打开JD.CRS.Web.Host/appsettings.json,修改数据库连接设置ConnectionStrings.打开JD.CRS.Web.Host/appsettings.json,做同样修改.   迁移数据库方法一设置JD.CRS.Web.Mvc为启始项目打开工具/......
  • ABP入门教程3 - 解决方案
    点这里进入ABP入门教程目录 创建项目点这里进入ABP启动模板 如图操作,我们先生成一个基于.NETCore的MPA(多页面应用).点击"Createmyproject!"即可创建项目. 解读项目展示层(JD.CRS.Web.Mvc)提供一个用户界面,实现用户交互操作。ASP.NETCoreMVC(模型-视图-控制器)可以视为展示层......
  • ABP入门教程5 - 界面调整
    点这里进入ABP入门教程目录 调整前调整后调整项页面标题把favicon.ico替换为指定LogoJD.CRS.Web.Mvc\wwwroot\favicon.ico顶部工具栏把logo.png替换为指定LogoJD.CRS.Web.Mvc\wwwroot\images\logo.png更新Layout JD.CRS.Web.Mvc\Views\Shared\_Layout.cshtml 左侧菜单栏调整用户......