Overview
The Specification pattern encapsulates query logic in its own class, which helps classes follow the Single Responsibility Principle (SRP) and promotes reuse of common queries. Specifications can be independently unit tested. When combined with the Repository pattern, it can also help to keep it from growing with too many additional custom query methods. Specification is commonly used on projects that leverage Domain-Driven Design.
Since version 5, this package also supports applying specifications directly to EF Core DbContext
instances.
Benefits
The main benefits offered by the specification pattern in general and this package in particular are:
- Keep data access query logic in one place
- Keep data access query logic in the domain layer
- Reuse common queries throughout your application
- Provide good names to common queries to facilitate reuse and elevate language used to describe the app’s behavior
- Eliminate common pain points of Repository pattern (hiding ORM data shaping features, requiring many custom query methods)
Installing Ardalis.Specification
Install Ardalis.Specification from NuGet. The latest version is available here:
https://www.nuget.org/packages/Ardalis.Specification/
Alternately, add it to a project using this CLI command:
dotnet add package Ardalis.Specification
Getting Started
Read the Getting Started section. You can also review the sample application that is available in the GitHub repository.
Docs theme notes
This docs site is using the Just the Docs theme. Details on how to configure its metadata and navigation can be found here.