Using LINQ specifications in the data access layer
The specification pattern is a way to encapsulate query criteria inside aptly named business rule objects, called specifications. A specification has a single purpose. It should answer whether an entity of some type satisfies the conditions or criteria for a specific business rule or not.
In this recipe, we'll show you how to set up and use the specification pattern with the NHibernate repository and LINQ expressions.
Getting ready
To complete this recipe we will need a LinqSpecs
library. The documentation and source code can be found at http://linqspecs.codeplex.com.
Complete the Setting up an NHibernate repository recipe.
How to do it…
Install
LinqSpecs
using the NuGet Package Manager console by executing the following command:Install-Package LinqSpecs
Add the following two methods to the
IRepository
interface:IEnumerable<T> FindAll(Specification<T> specification); T FindOne(Specification<T> specification);
Add the following...