NHibernate-QueryOver使用基类? [英] NHibernate- QueryOver using base classes?
问题描述
现在我正在使用Criteria API并喜欢它,但是如果我可以切换到QueryOver API,那就更好了.但是,我的设置有些奇怪.为了将数据分区到表中,我有一个基本的抽象类:
Right now I'm using the Criteria API and loving it, but it would be even better if I could make the switch to the QueryOver API. However, my setup is a little strange. In order to partition data into tables, I have one base abstract class:
Listing
以及从其继承的许多类:
and a number of classes which inherit from that:
Listing_UK
Listing_US
etc.
使用标准API,我可以执行以下操作:
With the criteria API, I can do something like:
Type t = typeof(Listing_UK);
if (condition) t = typeof(Listing_US);
DbSession.CreateCriteria(t)
.Add(Restriction.Eq("field",value)
.List<Listing>());
基本上,对不同的类使用相同的查询.似乎QueryOver的强类型性质使我无法执行此操作-基本问题是:
Basically, using the same query on different classes. It seems like the strongly-typed nature of QueryOver prevents me from doing this- the basic problem being that:
DBSession.QueryOver<Listing_UK>()
不强制转换为
DBSession.QueryOver<Listing>
虽然我理解为什么,但是我想知道是否有人可以使用任何技巧创建通用的QueryOver,而该OverOver仍然会定位正确的表?
While I understand why, I'm wondering if anyone has any trick I could use to create a generic QueryOver that will still target the right table?
推荐答案
您可能会发现可以使用以下重载:
You might find you could use the following overload:
DbSession.CreateCriteria(myDynamicallyDeterminedType)
.Add(Restrictions.On<Listing>(l => l.field == value))
.List<Listing>();
这篇关于NHibernate-QueryOver使用基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!