当存在许多关联时,使用诸如NHibernate之类的ORM-性能问题 [英] Usage of ORMs like NHibernate when there are many associations - performance concerns

查看:77
本文介绍了当存在许多关联时,使用诸如NHibernate之类的ORM-性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序(基于Web的应用程序),该应用程序现在具有大量的关联.简而言之,一个帐户:

I have created an application (a web-based application) which now has a large number of associations. Simply put, an Account:

  • 有很多用户
  • 具有设置
  • 有很多项目

类似一个项目:

  • 有很多物品

用户:

  • 有很多任务

以此类推,并加载更多关联.我希望这没什么特别的.我选择使用NHibernate为我提供了一组不错的持久性类,其中映射器定义了所有关联.

And so on, with loads more associations. Nothing particularly unusual about that I hope. I chose to use NHibernate to give me a nice set of persistent classes with mappers defining all of the associations.

但是这种方法正确吗?在对应用程序的每个请求中,都将加载帐户(因为它是必需的),然后这会从数据库中请求大量不需要的数据.延迟加载是一种选择,但是我不知道我最初的方法是否会更好.在那个阶段我只需要帐户和相关的设置,因此映射应该反映这一点吗?问题是我希望在其他时间获得类似帐户的所有项目"之类的东西,因此我需要映射器来反映所有关联.

But is this approach right? On every request to the application the Account is loaded (because it is needed) and this then requests a large amount of data from the DB that is not required. Lazy loading is an option, but I don't know if my initial approach couldn't be better. All I want at that stage is the Account and associated Settings so should the mapping reflect this? Trouble is I want things like all the Projects for an Account at other points so I need the mappers to reflect all of the associations.

我担心的是,延迟加载可能只是在弥补我最初的不良体系结构.我很清楚,这可能是由于我对NHibernate的内部工作原理还缺乏了解而导致的.因此,对于任何有关良好做法的建议,我将不胜感激.

My concern is that lazy-loading may just be compensating for a bad initial architecture on my part. I am well aware that this may just be a result of my as-yet poor knowledge of the inner workings of NHibernate. As a result I'd be grateful for any suggestions on good practice.

推荐答案

在了解这种情况时,域驱动设计对我有很大帮助. DDD中的关联

Domain Driven Design has helped me a lot when understanding those kinds of situation. Association in DDD

在您遇到的情况下应该问自己的问题:您真的需要双向关联吗?或者在某些情况下,单向关联可能就足够了?这个决定是架构的一部分.所以你是对的.延迟加载是一个很大的帮助,在默认情况下选择双向关联时.但这可以被认为是设计缺陷.

The question you should ask yourself in your situation: Do you really need a bi-directional association - or might in some cases an uni-directional association be enough? And that decision is part of the architecture. So you were right. Lazy Loading is a big help, when choosing bi-directional associations by default. But it could be considered a design flaw.

这篇关于当存在许多关联时,使用诸如NHibernate之类的ORM-性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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