NHibernate Reusable QueryOver

To keep my queries autonomous and potentially reusable, I tended to do this in NH2:

public class FeaturedCarFinder : DetachedCriteria { public FeaturedCarFinder(int maxResults) : base(typeof(Car)) { Add(Restrictions.Eq("IsFeatured", true)); SetMaxResults(maxResults); SetProjection(BuildProjections()); SetResultTransformer(typeof(CarViewModelMessage)); } } 

I would like to use QueryOver now that I’ve moved to NH3, but I'm not sure how to do it with QueryOver?

+4
source share
1 answer

Someone from the NH Users list gave me the answer:

 public class FeaturedCarFinder : QueryOver<Car, Car> { public FeaturedCarFinder(int maxResults) { Where(c => c.IsFeatured); Take(maxResults); BuildProjections(); TransformUsing(Transformers.AliasToBean(typeof(CarViewModelMessage))); } private void BuildProjections() { SelectList(l => l.Select(c => c.IsFeatured) //... ); } } 

Very similar to using DetachedCriteria as a base class, but pay attention to using QueryOver (i.e. two type arguments), not just QueryOver as a base class.

+6
source

Source: https://habr.com/ru/post/1335239/


All Articles