Using ICriteria in the data access layer

For queries where the criteria are not known in advance, such as a website's advanced product search, ICriteria queries are more appropriate than named HQL queries. In this recipe, we will show you how to use the same DAL infrastructure with ICriteria and QueryOver queries.

Getting ready

Complete the previous recipe, Using named queries in the data access layer.

How to do it…

  1. In Eg.Core.Data.Impl.Queries, add a new, empty, public interface named ICriteriaQuery.
  2. Add a class named CriteriaQueryBase with the following code:
    public abstract class CriteriaQueryBase<TResult> : NHibernateQueryBase<TResult>, ICriteriaQuery { public CriteriaQueryBase(ISessionFactory sessionFactory) : base(sessionFactory) { } public override ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.