Search code examples
nhibernatespecification-pattern

Specification Pattern for Querying against DataBase using NHibernate


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?


Solution

  • This is not nessary better, but can be an alternative

    interface ISpecification<T> 
    {
       bool IsSatisfiedBy(T object);
    
       Expression<Func<T, bool>> Predicate { get; }
    }
    

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