NHibernate- QueryOver 使用基类?

发布于 2024-10-14 18:45:53 字数 705 浏览 3 评论 0原文

现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-10-21 18:45:53

您可能会发现可以使用以下重载:

DbSession.CreateCriteria(myDynamicallyDeterminedType)
    .Add(Restrictions.On<Listing>(l => l.field == value))
    .List<Listing>();

You might find you could use the following overload:

DbSession.CreateCriteria(myDynamicallyDeterminedType)
    .Add(Restrictions.On<Listing>(l => l.field == value))
    .List<Listing>();
七月上 2024-10-21 18:45:53

从技术上讲,这在某些情况下应该是允许的。即 c#4 中协方差的使用和一些反射。我相信所需的所有更改都是使 IQueryOver 接口协变。

subType = typeof(ParentClass).Assembly.GetType(subTypeFullName);

var mi = typeof(QueryOver).GetMethod("Of", new Type[]{});
var gmi = mi.MakeGenericMethod(subType);
var qo = (QueryOver<ParentClass, ParentClass>)gmi.Invoke(null, null);
var queryOver = qo.GetExecutableQueryOver(session);

// your comment restrictions/projections here.

我一直在思考这个想法,只要我们能够针对 .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.

subType = typeof(ParentClass).Assembly.GetType(subTypeFullName);

var mi = typeof(QueryOver).GetMethod("Of", new Type[]{});
var gmi = mi.MakeGenericMethod(subType);
var qo = (QueryOver<ParentClass, ParentClass>)gmi.Invoke(null, null);
var queryOver = qo.GetExecutableQueryOver(session);

// your comment restrictions/projections here.

I've wrestled with this idea and as long as we can build against .Net 4 it should work pretty easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文