views:

640

answers:

1

How Do You Implement Specification Pattern for querying database using NHibernate?(without LINQ to NHibernate).I read a lot about Specification Pattern but most of them was about Validation and Querying Memory collection objects.

Best method as far as I know using DetachedCriteria in Specification Interface like this.

interface ISpecification<T> {

 bool IsSatisfiedBy(T object);

 DetachedCriteria CreateCriteria();

}

Is there any alternative or better way to do this?

+1  A: 

This is not nessary better, but can be an alternative

interface ISpecification<T> 
{
   bool IsSatisfiedBy(T object);

   Func<T, bool> Predicate { get; }
}

Easy to use over linq (to nhibernate) and memory-collections.

Paco
How can I use this over database ?Can you give me an example?
mcaaltuntas
return MyISession.Linq<MyEntity>().Where(specification.Preficate).ToList()
Paco
I don't really use it that simple in my code, but I use a query object to add support for eager loading, ordering, projections, etc.
Paco
To make this have any chance of performing the filter in SQL, Predicate should be an `Expression<Func<T, bool>>` instead.
Martinho Fernandes