LightPoint.Foundation.GuardClauses
1.3.5.7
dotnet add package LightPoint.Foundation.GuardClauses --version 1.3.5.7
NuGet\Install-Package LightPoint.Foundation.GuardClauses -Version 1.3.5.7
<PackageReference Include="LightPoint.Foundation.GuardClauses" Version="1.3.5.7" />
<PackageVersion Include="LightPoint.Foundation.GuardClauses" Version="1.3.5.7" />
<PackageReference Include="LightPoint.Foundation.GuardClauses" />
paket add LightPoint.Foundation.GuardClauses --version 1.3.5.7
#r "nuget: LightPoint.Foundation.GuardClauses, 1.3.5.7"
#:package LightPoint.Foundation.GuardClauses@1.3.5.7
#addin nuget:?package=LightPoint.Foundation.GuardClauses&version=1.3.5.7
#tool nuget:?package=LightPoint.Foundation.GuardClauses&version=1.3.5.7
简要说明
LightPoint.Foundation.GuardClauses 是用于实现卫语句(前置条件检查)模式的具体实现,用于在 Light Point 架构设计中,处理函数或方法的开始部分检查输入参数或程序状态是否满足执行该函数或方法的基本前提条件。
本项目是参考 Steve Smith 的 GuardClauses 的项目来具体实现的。
使用范例
public class Order(string name, int quantity, long max, decimal unitPrice, DateTime dateCreated)
{
private string _name = Guard.Against.NullOrWhiteSpace(name);
private int _quantity = Guard.Against.NegativeOrZero(quantity);
private long _max = Guard.Against.Zero(max);
private decimal _unitPrice = Guard.Against.Negative(unitPrice);
private DateTime _dateCreated = Guard.Against.OutOfSQLDateRange(dateCreated, "dateCreated");
public void ProcessOrder(Order order)
{
Guard.Against.Null(order);
// 具体地处理订单逻辑
}
}
当前实现的语句
- Guard.Against.Null (对象 input 为 null 时,抛出异常)
- Guard.Against.NullOrEmpty (类型为 string, guid 或者 array 的 input 为 null 或者 empty 时,抛出异常)
- Guard.Against.NullOrWhiteSpace (字符串 input 为 null, empty 或者 whitespace 时,抛出异常)
- Guard.Against.OutOfRange (类型为 integer/DateTime/enum 的 input 超出范围时,抛出异常)
- Guard.Against.EnumOutOfRange (如果 enum 超出枚举范围时,抛出异常)
- Guard.Against.OutOfSQLDateRange (如果类型 DateTime 的 input 超出 SQL Server 的 DateTime 范围时,抛出异常)
- Guard.Against.Zero (类型为数字的 input 为零时,抛出异常)
- Guard.Against.Expression (校验使用您定义的任何表达式)
- Guard.Against.InvalidFormat (校验使用正则表达式或函数定义允许的格式)
- Guard.Against.NotFound (类似于 Null,但用于基于 id 或者 key 之类的查找,如果没有结果,抛出
NotFoundException
)
扩展的写法
public static class FooGuardExtensionDemo
{
/// <summary>
/// 扩展的示例,校验传入的字符串,如果满足 input?.ToLower() == "foo",抛出一个 ArgumentException 异常
/// </summary>
/// <param name="guardClause">待扩展的卫语句接口</param>
/// <param name="input">传入的待校验的整数</param>
/// <param name="parameterName">传入的对象的参数名称,如果是 null,则通过通过 [CallerArgumentExpression(nameof(input))] 获取默认名称。</param>
/// <param name="message">可选的参数,定制的校验错误信息</param>
/// <exception cref="ArgumentException">如果 input?.ToLower() == "foo",抛出这个异常</exception>
public static void Foo(this IGuardClause guardClause, string input, [CallerArgumentExpression(nameof(input))] string? parameterName = null, string? message = null)
{
// 校验的条件是基于所需要的逻辑来实现的 !
if (input?.ToLower() == "foo")
throw new ArgumentException(message ?? $" {parameterName} 所输入的值不能满足条件。", parameterName);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LightPoint.Foundation.GuardClauses:
Package | Downloads |
---|---|
LightPoint.Foundation.Kernel
用于构建 LightPoint 设计应用架构中领域模型的基本构成骨架规约,负责定义系统数据的核心规约,这部分的规约遵循 DDD 方式构建,除了定义领域模型及其基本的聚合根的规约抽象基础模型以外,还包括 CQRS、数据软删除和审计、基础性服务(如日志)等基础数据模型和处理方法的接口和基类,未来还根据实际的需要,扩展相关的公共内容。 本项目是参考 Steve Smith 的 [Ardalis.SharedKernel](https://github.com/ardalis/Ardalis.SharedKernel/tree/main/src/Ardalis.SharedKernel) 的项目来具体实现的。 |
GitHub repositories
This package is not used by any popular GitHub repositories.