NHibernate可重用的QueryOver

2019-06-12
为了使我的查询保持独立并可能重复使用,我倾向于在NH2中执行此操作:

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

我现在想使用QueryOver,我已经转移到了NH3,但我不确定如何使用QueryOver进行上述操作?

NH用户名单上的某人给了我答案:

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) 
            //... 
            ); 
    } 
}

与使用DetachedCriteria作为基类非常相似,但请注意使用QueryOver(即两个类型参数版本)而不仅仅是QueryOver作为基类.