这是在NHibernate中渴望加载子集合的正确方法吗 [英] Is this the right way to eager load child collections in NHibernate

查看:83
本文介绍了这是在NHibernate中渴望加载子集合的正确方法吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了太多时间来寻找一个渴望加载子集合的好方法.到目前为止,这就是我所得到的.它正在工作,但我很难相信这是编写此查询的最佳方法

I have used too much time to find a nice way to eager load child collections. so far this is what I got. It's working but I find it hard to believe that this is the best way to write this query

 [Fact]
    public void EagerLoadQueryOverWithFutureTest()
    {
        const long journalNr = 1470;

        var query = _repo.QueryOver().Where(s => s.JournalNr == journalNr).Future<Sag>();
        var sagsansoegning = _repo.QueryOver().Where(an => an.JournalNr == journalNr).Fetch(x => x.Sagsansoegning).Eager.Future<Sag>();
        var noter = _repo.QueryOver().Where(n => n.JournalNr == journalNr).Fetch(x => x.Noter).Eager.Future<Sag>();
        var filer = _repo.QueryOver().Where(f => f.JournalNr == journalNr).Fetch(x => x.Filer).Eager.Future<Sag>();
        var emails = _repo.QueryOver().Where(e => e.JournalNr == journalNr).Fetch(x => x.Emails).Eager.Future<Sag>();
        var journal = _repo.QueryOver().Where(j => j.JournalNr == journalNr).Fetch(x => x.Journal).Eager.Future<Sag>();
        var sagsTilstand = _repo.QueryOver().Where(t => t.JournalNr == journalNr).Fetch(x => x.Tilstand).Eager.Future<Sag>();
        var boligsocialEvalueringer = _repo.QueryOver().Where(b => b.JournalNr == journalNr).Fetch(x => x.BoligsocialEvaluering).Eager.Future<Sag>();
        var result = query.SingleOrDefault();
        result.JournalNr.Should().Be(journalNr);
        result.Emails.Should().HaveCount(c => c > 20);
        result.Filer.Should().HaveCount(c => c > 20);
    }

推荐答案

我想补充一点不同的观点,而不是回答如何使用未来"仅一次往返数据库的问题.

I would like to append different point of view, not the answer how to use "future" to make only one round-trip to DB.

不确定为什么需要获取所有属性.我猜可能是1)由您的代码使用,例如呈现UI或2)作为API,以某种方式对其进行序列化并将其传递给客户端.

Not sure why you need to fetch all the properties. I guess it could be 1) to be used by your code, e.g. to render UI or 2) as the API which somehow serializes that and passes it to the client.

在第一种情况下,我建议使用每个请求的会话并使用延迟加载.在第二步中,我建议介绍一个步骤:在将DTO传递给客户端之前,先构建DTO.可以在会话仍处于打开状态时执行此操作,因此请使用延迟加载.

In the first scenario, I would suggest to use the session per request and use the lazy load. In the second, I would suggest to introduce one step: building the DTO before passing it to client. That could be done while session is still open, therefore use the lazy load.

两种情况下的延迟加载.因此,在两种情况下,我都建议使用与显式提取不同的方法.它将使用或多或少相同数量的选择和往返数据库.但最后可能会更有效.因为所有这些工作都将留在NHibernate上

Lazy load in both scenarios. So, in both cases I would suggest to use different approach then explicit fetching. It will use more or less the same amount of selects and round-trips to DB. But at the end it could be more effective. Because all that job would be left on the NHibernate

要做的是将批处理大小(本机)加载更改为预先获取(手动)加载.在此处查看更多信息 19.1.5.使用批量提取.在这种情况下,NHibernate将处理您的查询,然后再次执行少量查询以加载剩余数据.但只有在确实需要时

The thing is to change the eager-fetch (manual) loading with the batch-size (native) loading. See more here 19.1.5. Using batch fetching. In this case, NHibernate will process your query, and then will go again with few more queries to load the remaining data. But only if really needed

好处是,您不会成为查询的囚犯.在会话打开之前(即在View渲染或序列化期间),您可以使用实体上可能/应该在实体上可用的任何属性.您不必去通过join附加另一个显式查询即可获得渴望的数据.

The advantage is, that you are not a prisoner of your queries. You can consume any property which could/should be available on the entity, until the session is open (I.e. during View rendering or serializing). You do not have to go and append another explicit query over join to get eager data.

因此,尽管Future可能是世界的解决方案,但您从APP到DB的连接速度非常慢...使用NHibernate(ORM)并不是世界.从维护的角度来看,延迟映射和临时加载(具有批处理功能)的优点是正确的方法……我会说

So, while Future could be solution to the world were your connectivity to DB from APP is very slow... it is not the world were to use NHibernate (ORM). The advantage of the lazy mapping and ad hoc loading (with power of batching) is from maintenance perspective the right way... I'd say

这篇关于这是在NHibernate中渴望加载子集合的正确方法吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆