简体   繁体   English

聚合根引用位于另一个聚合根中

[英]Aggregate root reference in another aggregate root

I currently have two aggregate roots - Customer and AddressBook . 我目前有两个总根源- CustomerAddressBook Both have some invariants that need to be protected. 两者都有一些需要保护的不变式。 Customer has reference to AddressBook and I am not sure whether that is the correct way to model my domain because one cannot live without the other and since domain objects should be created using factories I feel like I should not allow creation of Customer without AddressBook and vice versa but obviously one needs to be created before the other. Customer参考了AddressBook ,但我不确定这是否是对我的域建模的正确方法,因为一个域不能没有另一个生存,并且域对象应该使用工厂创建,我觉得我不应该允许没有AddressBook和Vice来创建Customer反之亦然,但显然必须先创建一个。 Hope it makes sense. 希望有道理。

How should I address my problem? 我应该如何解决我的问题?

Other question would be: can we create multiple aggregate roots in a single transaction? 另一个问题是:我们可以在单个交易中创建多个聚合根吗? I've red that it should not be done in case of update. 我已经冒充它,如果有更新就不应该这样做。

I currently have two aggregate roots - Customer and AddressBook. 我目前有两个聚合根-Customer和AddressBook。 Both have some invariants that need to be protected. 两者都有一些需要保护的不变式。 Customer has reference to AddressBook and I am not sure whether that is the correct way to model my domain because one cannot live without the other 客户参考了AddressBook,但我不确定这是否是对我的域进行建模的正确方法,因为一个人离不开另一个

If they really don't make sense without the other, you may want to review the design to see if they are really part of the same consistency boundary. 如果它们真的没有其他意义,那么您可能需要查看设计以查看它们是否确实属于同一一致性边界。

Can we create multiple aggregate roots in a single transaction? 我们可以在单个交易中创建多个聚合根吗?

Technically, yes. 从技术上讲,是的。 It may not be a good idea. 这可能不是一个好主意。

When all of the logically distinct aggregates are stored together, then creating them in a single transaction is straightforward. 当所有逻辑上不同的聚合存储在一起时,在单个事务中创建它们就很简单。

But that also introduces a constraint: that those aggregates need to be stored "together". 但是,这也带来了一个约束:这些聚合需要“一起”存储。 If all of your aggregates are in the same relational database, an all or nothing transaction is not going to be a problem. 如果您所有的聚合都在同一个关系数据库中,那么全有或全无交易将不是问题。 On the other hand, if each aggregate is persisted into a document store, then you need a store that allows you to insert multiple documents in the same write. 另一方面,如果每个聚合都保存在文档存储中,则需要一个存储,该存储允许您在同一写入中插入多个文档。

And if your aggregates should happen to be stored in different document stores, then coordinating the writes becomes even more difficult. 而且,如果您的汇总应该恰好存储在不同的文档存储中,那么协调写入就变得更加困难。

I would like to create closely associated AddressBook with him.... Maybe a domain event would be a more suitable option? 我想与他建立紧密联系的地址簿。...也许域事件会是一个更合适的选择?

Perhaps; 也许; using a domain event to signal a handler to invoke another transaction is a common pattern for automating work. 使用域事件来向处理程序发出信号以调用另一个事务是自动化工作的常见模式。 See Evolving Business Processes a la Lokad for a good introduction to process managers. 请参阅《 不断发展的业务流程》,以获取对流程管理者的良好介绍。

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

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