How to Create Specifications
Basic Specification
A Specification class should inherit from Specification<T>
, where T
is the type being retrieved in the query:
public class ItemByIdSpec : Specification<Item>
A Specification can take parameters in its constructor and use these parameters to make the appropriate query. Since the above class’s name indicates that it will retrieve an Item
by id, its constructor should take in an id
parameter:
public ItemByIdSpec(int Id)
In its constructor, the Specification should define a Query
expression, using its parameter to retrieve the desired object:
Query.Where(x => x.Id == Id);
Based on the above, the most basic specification should look something like this:
public class ItemByIdSpec : Specification<Item>
{
public ItemByIdSpec(int Id)
{
Query.Where(x => x.Id == Id);
}
}
Finally: the Specification above should also implement the marker interface ISingleResultSpecification<T>
, which makes clear that this Specification will return only one result. Any “ById” Specification, and any other Specification intended to return only one result, should implement this interface to make clear that it returns a single result.
public class ItemByIdSpec : SingleResultSpecification<Item>
{
public ItemByIdSpec(int Id)
{
Query.Where(x => x.Id == Id);
}
}
Advanced Specification
From here, additional operators can be used to further refine the Specification. These operators follow LINQ syntax and are described in more detail in the Features section.