简体   繁体   English

DDD:用户聚合根与其他聚合中几乎所有实体之间的一对多关系

[英]DDD: one-to-many relationship between user aggregate root and almost all entities in other aggregates

I have the following DDD scenario, grouped into the following aggregates:我有以下 DDD 场景,分为以下聚合:

User,用户,
Friends (User Associations),朋友(用户协会),
File (for user uploading),文件(供用户上传),
Galleries (grouping of files),画廊(文件分组),
Messages (user communication),消息(用户通信),
Groups (users can create and other members can join),组(用户可以创建,其他成员可以加入),
GroupMessages (messages sent to all members of a group), GroupMessages(发送给组内所有成员的消息),
GroupForums (group members can discuss various topics) GroupForums(小组成员可以讨论各种主题)

This is where it gets confusing.这就是令人困惑的地方。 A user is associated with everything down to GroupForums.用户与 GroupForums 的所有内容相关联。 It seems illogical to have to go through the User repository to access the other aggregates although, from a cascading standpoint, if I removed the user, technically, the records associated with the user should go away as well.必须通过用户存储库才能访问其他聚合似乎不合逻辑,尽管从级联的角度来看,如果我删除了用户,从技术上讲,与用户关联的记录也应该消失。

It seems as if I should not add all of the one-to-many associations that exist here to the user entity either, as hydrating from the database seems to be ridiculous, especially if I try pulling every record associated with the user.似乎我也不应该将此处存在的所有一对多关联都添加到用户实体中,因为从数据库中补水似乎很荒谬,尤其是当我尝试提取与用户关联的每条记录时。 What is the recommended strategy for organizing your aggregates, and repositories as well as proper way of dealing with a lot of one-to-many relationships for a given entity?组织聚合和存储库的推荐策略是什么,以及处理给定实体的大量一对多关系的正确方法是什么?

The fact that you used the word 'associated' in your sentence "A user is associated with everything..." is quite a clue.您在句子“A user is associated with everything...”中使用了“关联”一词,这一事实非常有线索。 It is absolutely fine for aggregate roots to be associated or even for one to 'belong' to another.聚合根被关联甚至一个“属于”另一个是绝对好的。 However, you need to look at whether an entity can exist without the AR.但是,您需要查看一个实体是否可以在没有 AR 的情况下存在。 If it can it probably has its own life-cycle and should be an AR.如果可以的话,它可能有自己的生命周期,应该是一个 AR。 If it can not it is part of the aggregate.如果不能,则它是聚合的一部分。 This can be tricky to distill.这可能很难蒸馏。

You need to have a very clear boundary around your ARs.你需要在你的 AR 周围有一个非常清晰的界限。 For example, even though a Forum may require a User to create it this does not mean that the Forum needs to (or even can) be deleted when the user is deleted.例如,即使论坛可能需要用户创建它,但这并不意味着在删除用户时需要(甚至可以)删除论坛。 So the User in the Forum may become, say, the ForumCreator (a value object) that contains the user name and id only.因此,论坛中的用户可能会成为仅包含用户名和 id 的ForumCreator (一个值对象)。 When the User is deleted then the forum can continue its existence.删除用户后,论坛可以继续存在。

In the Order/OrderLine/Product scenario it would not make much sense to delete all order lines that contain a specific product if you choose to delete it.在 Order/OrderLine/Product 场景中,如果您选择删除包含特定产品的所有订单行,则删除它没有多大意义。 I know that a product probably should never be deleted but we'll use it as an example.我知道一个产品可能永远不应该被删除,但我们会以它为例。 You would simply have the relevant product data 'denormalized' into the order line, eg: product id, product name.您只需将相关产品数据“非规范化”到订单行中,例如:产品 ID、产品名称。 So even if the product name happens to change it does not mean that all order lines need updating, or even should be updated.因此,即使产品名称发生更改,也不意味着所有订单行都需要更新,甚至应该更新。 In fact, the order line represents a point in time and the 'original' product name should be retained.事实上,订单行代表一个时间点,应该保留“原始”产品名称。 The purchaser may have ordered 'Some lirril product' and then the name changed to 'Little product'.购买者可能订购了“Some lirril product”,然后名称更改为“Little product”。 Not the same thing although it is the exact same product.不一样的东西,虽然它是完全相同的产品。 The purchaser only remembers the original.购买者只记得原件。

I hope that makes sense and helps in some way.我希望这是有道理的,并在某种程度上有所帮助。 You definitely need to find those hard edges to your object graph to get to the real aggregates.你肯定需要找到你的对象图的那些硬边才能得到真正的聚合。

In IDDD book, Aggregate is the consistency boundary, if these entites have no transactional consistency needs, they may be different Aggregate.在 IDDD 书中,Aggregate 是一致性边界,如果这些实体没有事务一致性需求,它们可能是不同的 Aggregate。 We should not design aggregate by the dependencies.我们不应该通过依赖项来设计聚合。 If so, you will have large-cluster aggregate.如果是这样,您将拥有大型集群聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM