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…
In
Eg.Core.Data.Impl.Queries
, add a new, empty, public interface namedICriteriaQuery
.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 TResult Execute() { var criteria = GetCriteria(); return WithinTransaction(() => Execute(criteria)); } protected abstract ICriteria GetCriteria(); protected abstract TResult Execute(ICriteria criteria...