TwoRivers.Specifications
2.0.0-beta.0
See the version list below for details.
dotnet add package TwoRivers.Specifications --version 2.0.0-beta.0
NuGet\Install-Package TwoRivers.Specifications -Version 2.0.0-beta.0
<PackageReference Include="TwoRivers.Specifications" Version="2.0.0-beta.0" />
<PackageVersion Include="TwoRivers.Specifications" Version="2.0.0-beta.0" />
<PackageReference Include="TwoRivers.Specifications" />
paket add TwoRivers.Specifications --version 2.0.0-beta.0
#r "nuget: TwoRivers.Specifications, 2.0.0-beta.0"
#:package TwoRivers.Specifications@2.0.0-beta.0
#addin nuget:?package=TwoRivers.Specifications&version=2.0.0-beta.0&prerelease
#tool nuget:?package=TwoRivers.Specifications&version=2.0.0-beta.0&prerelease
TwoRivers.Specifications
This project implements the Specification pattern. Intended for use in the Domain layer, it allows definition of business rules and criteria in a reusable and composable way.
Usage
To use the specifications, you can create classes that implement the ISpecification<T> interface.
This interface defines a method IsSatisfiedBy that takes an object of type T and returns a
boolean indicating whether the object satisfies the specification.
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
public class CustomerIsAdultSpecification : ISpecification<Customer>
{
public bool IsSatisfiedBy(Customer customer)
{
return customer.Age >= 18;
}
}
You can then use these specifications to evaluate objects in your domain logic:
var customer = new Customer { Age = 20 };
var isAdultSpec = new CustomerIsAdultSpecification();
if (isAdultSpec.IsSatisfiedBy(customer))
{
Console.WriteLine("Customer is an adult.");
}
else
{
Console.WriteLine("Customer is not an adult.");
}
Composing Specifications
One of the key benefits of the Specification pattern is that you can compose specifications together using logical operators. For example, you can create a specification that checks if a customer is an adult and has a valid email address:
public class CustomerHasValidEmailSpecification : ISpecification<Customer>
{
public bool IsSatisfiedBy(Customer customer)
{
return !string.IsNullOrEmpty(customer.Email)
&& customer.Email.IndexOf("@") > 0
&& customer.Email.LastIndexOf(".") > customer.Email.IndexOf("@");
}
}
var adultWithValidEmailSpec = new AndSpecification<Customer>(new CustomerIsAdultSpecification(), new CustomerHasValidEmailSpecification());
adultWithValidEmailSpec.IsSatisfiedBy(customer); // returns true if customer is an adult and has a valid email
Conclusion
The Specification pattern is a powerful tool for encapsulating business rules and criteria in a reusable and composable way. By defining specifications in your domain layer, you can keep your business logic clean and maintainable, while also making it easier to test and reuse your code.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- TwoRivers (>= 2.0.0-beta.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on TwoRivers.Specifications:
| Package | Downloads |
|---|---|
|
TwoRivers.Specifications.EntityFrameworkCore
Framework and tools for implementing applications by following best practices in a developer friendly way. Built from commit 59db18f48398b711c50e16d5da7aacc2e0e6f014 https://github.com/TwoRiversIT/AppFramework/commit/59db18f48398b711c50e16d5da7aacc2e0e6f014 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0-beta.8 | 72 | 3/5/2026 |
| 2.0.0-beta.7 | 63 | 3/5/2026 |
| 2.0.0-beta.6 | 64 | 3/5/2026 |
| 2.0.0-beta.5 | 63 | 3/4/2026 |
| 2.0.0-beta.4 | 64 | 3/4/2026 |
| 2.0.0-beta.3 | 67 | 3/3/2026 |
| 2.0.0-beta.2 | 66 | 3/3/2026 |
| 2.0.0-beta.1 | 62 | 3/3/2026 |
| 2.0.0-beta.0 | 66 | 3/3/2026 |
| 1.1.6 | 130 | 2/26/2026 |
| 1.1.5 | 132 | 2/25/2026 |
| 1.1.4 | 122 | 2/22/2026 |
| 1.1.3 | 126 | 2/22/2026 |
| 1.1.2 | 124 | 2/22/2026 |
| 1.1.1 | 116 | 2/22/2026 |
| 1.1.0 | 117 | 2/22/2026 |
| 1.0.0 | 135 | 2/17/2026 |