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
LinqSpecsusing the NuGet Package Manager console by executing the following command:Install-Package LinqSpecsAdd the following two methods to the
IRepositoryinterface:IEnumerable<T> FindAll(Specification<T> specification); T FindOne(Specification<T> specification);
Add the following...