[英]Should entity hold reference to repository?
Suppose we have class Home
and we want to have collection of all Cats
inside this home, but also we want to have general repository of Cats
that has all the cats available in the world. 假设我们有
class Home
并且我们希望收集该家中所有Cats
,但是我们也希望拥有Cats
常规存储库,其中包含世界上所有可用的cats。 Should Home
hold the reference to specific repository (or maybe collection) of Cats
, or should I just make another lookup in general repository? Home
应该保留对Cats
特定存储库(或集合)的引用,还是应该在常规存储库中进行其他查找?
From a domain-driven design perspective you shouldn't have one aggregate root (AR) instance contained in another AR instance and typically one also would not have a reference to a repository in any entity. 从域驱动的设计角度来看,您不应在另一个AR实例中包含一个聚合根(AR)实例,并且通常情况下,该实例也不会引用任何实体中的存储库。
So if Home
and Cat
are both ARs then Home
should contain only a list of Cat
Ids or a list of value objects (VO) that each represent a cat, eg HomeCat
that contains the Id
and, perhaps, the Name
. 因此,如果
Home
和Cat
都是AR,则Home
应该只包含Cat
ID列表或分别代表一只猫的值对象(VO)列表,例如,包含Id
以及Name
HomeCat
。 This also facilitates the persistence around the Home
AR since the HomeRepository
will be responsible for persistence of both Home
and HomeCat
. 由于
HomeRepository
将负责Home
和HomeCat
持久性,因此这也有利于Home
AR的持久性。
I must admit that this is where an Entity
such as Cat
becomes somewhat of a weird thing when it is contained in more than one AR. 我必须承认,这是当诸如
Cat
的Entity
包含在多个AR中时有点奇怪的地方。 However, you would still not have the cat repository in the home object but rather have the HomeRepository
make use of the CatRepository
when retrieving the relevant home instance. 但是,你仍然没有在家里对象猫仓库,而是有
HomeRepository
利用的CatRepository
检索有关家庭实例时。
As Java is based on references, you could keep two collections without any serious harm. 由于Java基于引用,因此您可以保留两个集合而不会造成严重损害。
What you need is to assure is that every cat on your home is also in the "world". 您需要确保您家中的每只猫也都在“世界”中。 The problem here is if the cats on your home change a lot, you would need to make several lookups, so you should choose a data structure data enables this as fast as you need (i am thinking hashMaps..)
这里的问题是,如果您家中的猫变化很大,则需要进行几次查找,因此您应该选择一种数据结构,数据可以根据需要尽快启用(我在考虑hashMaps ..)
using two collections will enable you to find cats in your home as fast as you can do. 使用两个收藏夹可以使您尽快找到家中的猫。 however sync-ing the collections might be a problem.
但是同步集合可能是个问题。 in this case you can think about the observer pattern: home observes the world, if a cat dies, it check if it was inside home, and deletes...
在这种情况下,您可以考虑观察者模式:家庭观察世界,如果猫死了,它会检查它是否在家庭内部,然后删除...
there is a lot of way to do what you asked, all you need to do is think about what is the operations with higher frequency, and your need in general. 有很多方法可以执行您所要求的操作,您需要做的只是考虑什么是频率更高的操作,以及您的一般需求。 if the collections are small, so no problem on having one collection, with lookups to find the cats home...
如果收藏很小,那么拥有一个收藏并通过查找来找到猫的家就没有问题...
It's hard to answer DDD questions with fictional domains (or at least very little information about it), since DDD is all about modeling the domain just the way it is and maintaining the integrity of it's invariants. 很难用虚构域来回答DDD问题(或至少很少有关它的信息),因为DDD只是按照其原样对域进行建模并保持其不变性的完整性。
As far as I can tell, you do not have any invariant that applies to the relationship between Home
and Cat
, therefore you do not need a collection of Cat
objects within Home
's consistency boundary. 据我所知,您没有适用于
Home
和Cat
之间关系的任何不变式,因此您不需要Home
的一致性边界内的Cat
对象集合。 You could simply have two aggregate roots ( Home
and Cat
) and Cat
would reference it's Home
by identity . 您可以简单地拥有两个聚合根(
Home
和Cat
),而Cat
可以通过identity引用它的Home
。
You can use the CatRepository
to query all cats from a specific home or all cats independently of their home. 您可以使用
CatRepository
查询特定家中的所有猫,也可以独立于家而查询所有猫。
It's important not to put artificial constraints in the model. 重要的是不要在模型中放置人为约束。 For instance, if you hold a collection of
Cat
identities inside Home
for no other reason than maintaining the relationship, then you are limiting the scalability of your system: two simultaneous transactions trying to associate a new Cat
to the same Home
will fail with a concurrency exception for no reason (assuming optimistic concurrency). 例如,如果您仅出于保持关系的原因而在
Home
内保留Cat
身份的集合,那么您就在限制系统的可伸缩性:尝试将新的Cat
关联到同一Home
两个同时事务将失败,并发无缘无故的异常(假设乐观并发)。
If Cat
needs to be an aggregate root of its own aggregate accessible by a CatRepository
, then it should not be included in the Home
aggregate. 如果
Cat
必须由自己的总访问的聚合根CatRepository
,那么它不应该被包括在Home
总。 Your Home
entity should reference the associated Cat
entities by identity and not by reference. 您的“
Home
实体应按身份而不是参考来引用关联的Cat
实体。
This is a question of aggregate design and requires a closer look at your domain and how you need to use your entities. 这是一个总体设计问题,需要仔细查看您的域以及如何使用实体。 One question you could ask yourself is "if I delete
Home
, should all Cat
entities be deleted as well?" 您可能会问自己一个问题:“如果我删除
Home
,是否还应删除所有Cat
实体?” Do not put too much emphasis on this question. 不要过分强调这个问题。 There are other important factors that need to be considered.
还有其他重要因素需要考虑。
Vaughn Vernon covers this topic in his three-part PDF series Effective Aggregate Design . 沃恩·弗农(Vaughn Vernon)在其分为三部分的PDF系列有效聚合设计中介绍了此主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.