淺談C#9.0新特性之參數(shù)非空檢查簡化
參數(shù)非空檢查是縮寫類庫很常見的操作,在一個方法中要求參數(shù)不能為空,否則拋出相應的異常。比如:
public static string HashPassword(string password)
{
if(password is null)
{
throw new ArgumentNullException(nameof(password));
}
...
}
當異常發(fā)生時,調(diào)用者很容易知道是什么問題。如果不加這個檢查,可能就會由系統(tǒng)拋出未將對象引用為實例之類的錯誤,這不利于調(diào)用者診斷錯誤。
由于這個場景太常見了,于是我經(jīng)常在我的項目中通過一個輔助類來做此類檢查。這個類用來檢查方法參數(shù),所以命名為 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 對此做了簡化,增加了操作符‘!',放在參數(shù)名后面,表示此參數(shù)不接受 null 值。使用方式如下:
public static string HashPassword(string password!)
{
...
}
簡化了很多有木有。這個提案已經(jīng)納入 C# 9.0 的特性中,但目前(2020-06-13)還沒有完成開發(fā)。
這個特性只支持非 null 檢查,其它參數(shù)檢查場景還是不夠用的,我還是會通過輔助類來進行像空字符串、空集合的檢查。
這個特性在寫公共類庫的時候很有用,但我想大多數(shù)人在寫業(yè)務邏輯代碼的時候可能用不到這個特性,一般會封自己的參數(shù)檢查機制。比如,我在項目中,對于上層 API 開發(fā),我通過封裝一個輔助類(ApiGuard)來對對參數(shù)進行檢查,如果參數(shù)不通過,則拋出相應的業(yè)務異常,而不是 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);
}
...
}
使用示例:
public async Task UpdateAsync(long id, BookUpdateDto dto)
{
ApiGuard.EnsureNotNull(dto, nameof(dto));
ApiGuard.EnsureNotEmpty(dto.TagValues, nameof(dto.TagValues));
var book = await DbSet
.Include(x => x.BookTags)
.FirstOrDefaultAsync(x => x.Id == id);
ApiGuard.EnsureExist(book);
Mapper.Map(dto, book);
...
}
ApiGuard 的好處是,當 API 接口接到不合要求的參數(shù)時,可以自定義響應返回內(nèi)容。比如,增加一個 Filter 或中間件用來全局捕獲業(yè)務代碼異常,根據(jù)不同的異常返回給前端不同的狀態(tài)碼和消息提示:
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
ApiResult result;
if (exception is BadRequestException)
{
result = ApiResult.Error(exception.Message, 400);
}
else if (exception is NotFoundException)
{
message = string.IsNullOrEmpty(message) ? "Not Found" : message;
result = ApiResult.Error(message, 404);
}
else if (exception is UnauthorizedAccessException)
{
message = string.IsNullOrEmpty(message) ? "Unauthorized" : message;
result = ApiResult.Error(message, 401);
}
...
}
只是一個參數(shù)非空檢查,在實際開發(fā)中卻有不少的學問,所以學好了理論還要多實踐才能更透徹的理解它。
到此這篇關于淺談C#9.0新特性之參數(shù)非空檢查簡化的文章就介紹到這了,更多相關C#9.0 參數(shù)非空檢查 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
作者:王亮
出處:http://cnblogs.com/willick
聯(lián)系:liam.wang@live.com
相關文章
C#實現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名
這篇文章主要為大家詳細介紹了C#如何實現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名,文中的實現(xiàn)方法講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02

