LightPoint.Foundation.Specification
1.3.5.8
dotnet add package LightPoint.Foundation.Specification --version 1.3.5.8
NuGet\Install-Package LightPoint.Foundation.Specification -Version 1.3.5.8
<PackageReference Include="LightPoint.Foundation.Specification" Version="1.3.5.8" />
<PackageVersion Include="LightPoint.Foundation.Specification" Version="1.3.5.8" />
<PackageReference Include="LightPoint.Foundation.Specification" />
paket add LightPoint.Foundation.Specification --version 1.3.5.8
#r "nuget: LightPoint.Foundation.Specification, 1.3.5.8"
#:package LightPoint.Foundation.Specification@1.3.5.8
#addin nuget:?package=LightPoint.Foundation.Specification&version=1.3.5.8
#tool nuget:?package=LightPoint.Foundation.Specification&version=1.3.5.8
简要说明
在 C# 中,Specification 模式是一种设计模式,用于将复杂的业务规则或验证逻辑封装为独立、可重用的组件。这种模式允许你将一个或多个业务规则表示为特定的规范(Specification),然后可以轻松地在应用程序的各个部分中应用这些规范。
本项目是参考 Steve Smith 的 Specification 的项目来具体实现的。
使用指南
以下是在 C# 中应用本项目实现 Specification 模式的常见步骤和组件:
- 创建 Specification 接口:
- 定义一个名为
ISpecification<T>的接口,其中T是你要应用规范的对象类型。接口通常包含一个方法IsSatisfiedBy(T candidate),该方法返回一个布尔值,表示给定的候选对象是否满足规范。
- 定义一个名为
public interface ISpecification<T>
{
bool IsSatisfiedBy(T candidate);
}
- 实现具体的 Specification 类:
- 为每种特定的业务规则创建一个类,该类实现
ISpecification<T>接口。在IsSatisfiedBy方法中实现具体的验证逻辑。
- 为每种特定的业务规则创建一个类,该类实现
public class AdultAgeSpecification : ISpecification<Person>
{
public bool IsSatisfiedBy(Person person) => person.Age >= 18;
}
public class ValidEmailSpecification : ISpecification<Person>
{
public bool IsSatisfiedBy(Person person) => EmailValidator.IsEmailValid(person.Email);
}
- 组合 Specification:
- 你可以通过逻辑运算(如与(AND)、或(OR)、非(NOT))组合多个 Specification。为此,可以创建复合 Specification 类,它们也实现
ISpecification<T>接口,并接受其他 Specification 作为构造函数参数。
- 你可以通过逻辑运算(如与(AND)、或(OR)、非(NOT))组合多个 Specification。为此,可以创建复合 Specification 类,它们也实现
public class AndSpecification<T> : ISpecification<T>
{
private readonly ISpecification<T> _first;
private readonly ISpecification<T> _second;
public AndSpecification(ISpecification<T> first, ISpecification<T> second)
{
_first = first;
_second = second;
}
public bool IsSatisfiedBy(T candidate) => _first.IsSatisfiedBy(candidate) && _second.IsSatisfiedBy(candidate);
}
public class OrSpecification<T> : ISpecification<T>
{
// ...
}
public class NotSpecification<T> : ISpecification<T>
{
// ...
}
- 在应用程序中使用 Specification:
- 现在你可以将 Specification 对象应用于你的业务对象,以检查它们是否满足特定的规则或条件。
var person = new Person { Age = 25, Email = "john.doe@example.com" };
var adultSpecification = new AdultAgeSpecification();
var emailSpecification = new ValidEmailSpecification();
var isAdultAndHasValidEmail = new AndSpecification<Person>(adultSpecification, emailSpecification);
bool satisfiesSpec = isAdultAndHasValidEmail.IsSatisfiedBy(person);
Specification 模式的主要优点是它提高了代码的可读性、可维护性和可重用性。你可以轻松地组合和修改 Specification,以适应不断变化的业务需求,而无需修改大量的代码。此外,这种模式有助于将复杂的业务规则从主要的业务逻辑中解耦出来,使得代码更加模块化和易于测试。
| 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
- LightPoint.Foundation.Base (>= 1.3.5.7)
- LightPoint.Foundation.Result (>= 1.3.5.8)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on LightPoint.Foundation.Specification:
| Package | Downloads |
|---|---|
|
LightPoint.Foundation.Kernel
用于构建 LightPoint 设计应用架构中领域模型的基本构成骨架规约,负责定义系统数据的核心规约,这部分的规约遵循 DDD 方式构建,除了定义领域模型及其基本的聚合根的规约抽象基础模型以外,还包括 CQRS、数据软删除和审计、基础性服务(如日志)等基础数据模型和处理方法的接口和基类,未来还根据实际的需要,扩展相关的公共内容。 本项目是参考 Steve Smith 的 [Ardalis.SharedKernel](https://github.com/ardalis/Ardalis.SharedKernel/tree/main/src/Ardalis.SharedKernel) 的项目来具体实现的。 |
|
|
LightPoint.Foundation.Specification.EFCore
Specification 模式是一种设计模式,用于将复杂的业务规则或验证逻辑封装为独立、可重用的组件。这种模式允许你将一个或多个业务规则表示为特定的规范(Specification),然后可以轻松地在应用程序的各个部分中应用这些规范。 本项目是参考 Steve Smith 的 [Specification](https://github.com/ardalis/Specification) 的项目来具体实现的。 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.3.5.8 | 237 | 12/17/2024 |
| 1.3.5.6 | 231 | 11/13/2024 |
| 1.3.5.5 | 229 | 9/25/2024 |
| 1.3.5.3 | 228 | 9/1/2024 |
| 1.3.5.2 | 217 | 8/29/2024 |
| 1.3.5.1 | 176 | 8/29/2024 |
| 1.3.4.4 | 207 | 8/19/2024 |
| 1.3.4.3 | 199 | 8/19/2024 |
| 1.3.4.2 | 199 | 8/19/2024 |
| 1.3.4.1 | 204 | 8/19/2024 |
| 1.3.4 | 447 | 8/1/2024 |
| 1.3.1 | 180 | 7/25/2024 |
| 1.3.0 | 256 | 7/25/2024 |
| 1.2.0 | 270 | 4/19/2024 |
| 1.0.0 | 356 | 12/29/2023 |