TwoRivers.Specifications 2.0.0-beta.4

This is a prerelease version of TwoRivers.Specifications.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TwoRivers.Specifications --version 2.0.0-beta.4
                    
NuGet\Install-Package TwoRivers.Specifications -Version 2.0.0-beta.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TwoRivers.Specifications" Version="2.0.0-beta.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TwoRivers.Specifications" Version="2.0.0-beta.4" />
                    
Directory.Packages.props
<PackageReference Include="TwoRivers.Specifications" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TwoRivers.Specifications --version 2.0.0-beta.4
                    
#r "nuget: TwoRivers.Specifications, 2.0.0-beta.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TwoRivers.Specifications@2.0.0-beta.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TwoRivers.Specifications&version=2.0.0-beta.4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=TwoRivers.Specifications&version=2.0.0-beta.4&prerelease
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed