浅谈C#9.0新特性之参数非空检查简化

2020-06-16 12:00:28王振洲

参数非空检查是缩写类库很常见的操作,在一个方法中要求参数不能为空,否则抛出相应的异常。比如:

public static string HashPassword(string password)
{
  if(password is null)
  {
    throw new ArgumentNullException(nameof(password));
  }
  ...
}

当异常发生时,调用者很容易知道是什么问题。如果不加这个检查,可能就会由系统抛出未将对象引用为实例之类的错误,这不利于调用者诊断错误。

由于这个场景太常见了,于是我经常在我的项目中通过一个辅助类来做此类检查。这个类用来检查方法参数,所以命名为 Guard,主要代码如下:

public static class Guard
{
  public static void NotNull(object param, string paramName)
  {
    if (param is null)
    {
      throw new ArgumentNullException(paramName);
    }
  }

  public static void NotNullOrEmpty(string param, string paramName)
  {
    NotNull(param, paramName);
    if (param == string.Empty)
    {
      throw new ArgumentException($"The string can not be empty.", paramName);
    }
  }

  public static void NotNullOrEmpty<T>(IEnumerable<T> param, string paramName)
  {
    NotNull(param, paramName);
    if (param.Count() == 0)
    {
      throw new ArgumentException("The collection can not be empty.", paramName);
    }
  }
  ...
}

这个类包含了三个常见的非空检查,包括 null、空字符串、空集合的检查。使用示例:

public static string HashPassword(string password)
{
  Guard.NotNull(password, nameof(password));
  ...
}

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
  this IEnumerable<TSource> source,
  Func<TSource, TKey> keySelector)
{
  Guard.NotNullOrEmpty(source, nameof(source));
  ...
}

介于这种非空检查极其常见,C# 9.0 对此做了简化,增加了操作符‘!',放在参数名后面,表示此参数不接受 null 值。使用方式如下:

public static string HashPassword(string password!)
{
  ...
}

简化了很多有木有。这个提案已经纳入 C# 9.0 的特性中,但目前(2020-06-13)还没有完成开发。

这个特性只支持非 null 检查,其它参数检查场景还是不够用的,我还是会通过辅助类来进行像空字符串、空集合的检查。

这个特性在写公共类库的时候很有用,但我想大多数人在写业务逻辑代码的时候可能用不到这个特性,一般会封自己的参数检查机制。比如,我在项目中,对于上层 API 开发,我通过封装一个辅助类(ApiGuard)来对对参数进行检查,如果参数不通过,则抛出相应的业务异常,而不是 ArgumentNullException。比如下面的一段截取自我的 GeekGist 小项目的代码:

public static class ApiGuard
{
  public static void EnsureNotNull(object param, string paramName)
  {
    if (param == null) throw new BadRequestException($"{paramName} can not be null.");
  }

  public static void EnsureNotEmpty<T>(IEnumerable<T> collection, string paramName)
  {
    if (collection == null || collection.Count() == 0)
      throw new BadRequestException($"{paramName} can not be null or empty.");
  }

  public static void EnsureExist(object value, string message = "Not found")
  {
    if (value == null) throw new BadRequestException(message);
  }

  public static void EnsureNotExist(object value, string message = "Already existed")
  {
    if (value != null) throw new BadRequestException(message);
  }
  ...
}