问题描述
我正在设计域模型,但似乎有些不妥。
I am designing the domain model, but there is something that doesn't seem to be ok.
我从一个主要的集合开始。它引用了其他聚合,这些其他聚合也引用了更多聚合。我可以从主聚集开始旅行空域模型。
I start with a main aggregate. It has references to other aggregates and those other aggregates reference more aggregates too. I can travel the hole domain model starting from the main aggregate.
我看到的问题是我将所有聚集实例保存在内存中。
The problem I see is that I will be holding all instances of aggregates in memory.
这是一个好的设计吗?我可以通过延迟加载来解决内存问题,但是我认为我有一个更深层次的问题。
Is that a good design? I can solve the memory problem with lazy loading but I think that I have a deeper problem.
我还有一个关于聚合引用的问题。我是否应该延迟将引用加载到其他聚合?如果是这种情况,我几乎永远不会使用他们的存储库。可以吗?
I have another question regarding aggregate references. Should I load the references to other aggregates lazily? If that is the case I would almost never user their repositories. Is that ok?
推荐答案
在聚合根(AR)之间直接引用会导致无法通过延迟加载解决的问题。而且,它迫使所有连接的AR都位于同一个数据库中,这使得推理和强制执行不变式变得更加困难,这首先是AR的主要目的。最好限制或消除AR之间的直接引用。 的一个很好的资源来学习聚合设计。 twitter.com/vaughnvernon rel = nofollow>沃恩·弗农。基本思想是使您的AR精简而集中,同时牢记其功能-实施业务约束并在根实体周围建立边界。如果一个AR需要来自另一个AR的数据来执行其工作,则可以由应用程序服务通过存储库将此数据提供给它。另外,如果仅需要引用即可满足UI要求,那么请考虑使用。
Having direct referenced between aggregate roots (ARs) can lead to problems that cannot be solved by lazy loading. Moreover, it forces all connected ARs to be in the same database and makes it more difficult to reason about and enforce invariants which is the primary purpose of an AR in the first place. It is better to limit or eliminate direct references between ARs. A great resource to learn about aggregate design is a series of articles by Vaughn Vernon. The basic idea is to make your ARs lean and focused all while keeping in mind their function - enforcing business constraints and forging a boundary around the root entity. If an AR needs data from another AR to perform its work, this data can be provided to it by an application service via repository. Also, if references are only needed to fulfill UI requirements, then consider using the read-model pattern.
这篇关于域驱动的设计和汇总参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!