NHibernate- QueryOver 使用基类?
现在我正在使用 Criteria API 并且很喜欢它,但如果我可以切换到 QueryOver API 那就更好了。但是,我的设置有点奇怪。为了将数据分区到表中,我有一个基本抽象类:
Listing
以及从该抽象类继承的许多类:
Listing_UK
Listing_US
etc.
使用条件 API,我可以执行以下操作:
Type t = typeof(Listing_UK);
if (condition) t = typeof(Listing_US);
DbSession.CreateCriteria(t)
.Add(Restriction.Eq("field",value)
.List<Listing>());
基本上,对不同的类使用相同的查询。似乎 QueryOver 的强类型本质阻止我这样做 - 基本问题是:
DBSession.QueryOver<Listing_UK>()
不转换为
DBSession.QueryOver<Listing>
虽然我明白为什么,但我想知道是否有人有任何可以用来创建通用 QueryOver 的技巧仍然会针对正确的表吗?
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.
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>());
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>()
doesn't cast to
DBSession.QueryOver<Listing>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会发现可以使用以下重载:
You might find you could use the following overload:
从技术上讲,这在某些情况下应该是允许的。即 c#4 中协方差的使用和一些反射。我相信所需的所有更改都是使 IQueryOver 接口协变。
我一直在思考这个想法,只要我们能够针对 .Net 4 进行构建,它就应该很容易工作。
Technically this should be allowable under certain circumstances. Namely the use of covariance in c#4 and some reflection. I believe that all the changes that would be needed would be to make the IQueryOver interfaces covariant.
I've wrestled with this idea and as long as we can build against .Net 4 it should work pretty easily.