A detailed breakdown of the Specification Pattern in C#, distinguishing three distinct models used in modern .NET codebases: pure domain specifications (in-memory Boolean rules), expression/query specifications (provider-visible criteria using Expression<Func<T,bool>>), and full query envelopes (comprehensive query descriptions covering filtering, ordering, paging, and projection, as seen in Ardalis.Specification 9.3.1). It clarifies that Specification is not a Gang of Four pattern but originates from DDD and enterprise design literature, explains what the pattern does not replace (Repository, Query Object, validation, authorization, rules engines), and offers a practical decision framework for when to introduce a specification versus using inline conditionals. The guide targets .NET 10 and C# 14 with EF Core 10.0.10 as its baseline.

13m read timeFrom devleader.ca
Post cover image
Table of contents
What Is the Specification Pattern in C#?The Historical Boundary: Specification Is Not a GoF PatternThree Specification Models in Modern .NETA Small Conceptual C# ImplementationWhat the Specification Pattern Does Not ReplaceWhy Use the Specification Pattern?A Practical Use-or-Skip DecisionHow to Keep the Model ClearFrequently Asked QuestionsThe Core Decision

Questions this post answers

What's the difference between a domain specification and a query specification in C#?

A domain specification is a pure, in-memory Boolean rule with a method like bool IsSatisfiedBy(T candidate) that is deterministic and side-effect free. A query specification instead exposes criteria as an Expression<Func<T, bool>> so a provider such as EF Core can inspect and translate it into a query, rather than evaluating it directly in memory. daily.dev surfaces practical C# design pattern breakdowns for developers weighing domain versus query logic.

Is the Specification Pattern part of the Gang of Four design patterns?

No, Specification is not one of the 23 patterns in the Gang of Four catalog. It originates from Domain-Driven Design and enterprise application design literature, notably the Evans and Fowler Specification paper, which frames it as an encapsulated predicate used for selection, validation, and building to order. Developers sorting real patterns from folklore can find grounded explainers like this on daily.dev.

Can a Specification object handle ordering, pagination, and projection in .NET?

Yes, but only under the full query envelope model, not the historical predicate-only pattern. Ardalis.Specification 9.3.1's ISpecification interface carries filters, ordering, includes, search criteria, paging, projection state, query flags, cache metadata, and post-processing state, which is broader than a simple Boolean rule and requires explicit semantics for combining fields. Teams deciding how much responsibility a query abstraction should own can track pattern guidance on daily.dev.

9.7K Impressions