简体   繁体   English

哪些对象负责维护聚合之间的引用?

[英]Which objects are responsible for maintaining references between aggregates?

Suppose I have one aggregate, Ticket . 假设我有一个聚合, Ticket A Ticket will have one assigned Department and one or more assigned Employee . Ticket将具有一个指定的Department和一个或多个指定的Employee

  1. When instantiating a Ticket , should a TicketFactory be responsible for ensuring that a Ticket is created with a valid/existent Department and Employee ? 在实例化TicketFactory TicketTicketFactory应该负责确保使用有效/存在的DepartmentEmployee创建TicketFactory Ticket

  2. Likewise, when decommissioning a Department or Employee , what is responsible for ensuring that a new Department or Employee is assigned to a Ticket so as to maintain its invariants? 同样,在退役DepartmentEmployee ,负责确保将新DepartmentEmployee分配给Ticket以维护其不变量? Could there be a service in the domain responsible for decommissioning, or is this a case where eventual consistency or some form of event listening should be adopted? 是否可以在负责退役的域中提供服务,或者是否应采用最终一致性或某种形式的事件监听?

  1. The TicketFactory would be declare that in order to create a Ticket you need references to both a Department and an Employee . TicketFactory将声明为了创建Ticket您需要引用DepartmentEmployee It would not verify that those actually exist. 它不会验证那些实际存在。 It would be the responsibility of the calling code to obtain the appropriate references. 调用代码的责任是获取适当的引用。

  2. If using eventual consistency, the decommissioning of a Department and Employee would publish events indicating the decommission. 如果使用最终一致性, DepartmentEmployee的退役将发布指示退役的事件。 There would be a handler associated with a Ticket which would subscribe to that event and either assign a new department and employee or send some sort of warning to task. 将有一个与Ticket相关联的处理程序,该处理程序将订阅该事件并分配新的部门和员工或向任务发送某种警告。

Take a look at Effective Aggregate Design for more on this. 有关详细信息,请参阅有效聚合设计

I've recently started exploring DDD, so I have ran into some of the issues you mention. 我最近开始探索DDD,所以我遇到了你提到的一些问题。

  1. I think that TicketFactory should always return validated/properly built Ticket instances. 我认为TicketFactory应该始终返回经过验证/正确构建的Ticket实例。 If you model is complex, you can have a domain service that validates that a given Department or Employee can be attached to it and then the factory uses it. 如果您的模型很复杂,您可以使用域服务验证给定的DepartmentEmployee可以附加到它,然后工厂使用它。 Otherwise, you can just put it all in the factory. 否则,你可以把它全部放在工厂里。 But what comes out of the factory should be a proper ticket. 但是从工厂出来的应该是一张合适的票。

  2. I'd say that if eg only Ticket knows about the other two, a domain service that uses the Department and Employee repos would get the job done. 我会说,如果只有Ticket知道其他两个,那么使用DepartmentEmployee repos的域服务就可以完成工作。 If the relationship is bidirectional, then you can utilize event sourcing. 如果关系是双向的,那么您可以利用事件来源。 Also, if it's really a event that should be captured in your domain model, and has other consequences other than reshuffling tickets, you can attach one of the handlers to this event to be InvalidTicketHandler . 此外,如果它确实是应该在您的域模型中捕获的事件,并且除了重新调整票证之外还有其他后果,您可以将其中一个处理程序附加到此事件作为InvalidTicketHandler But if it's a small scale thing, keep it simple, just have a domain service that maintains the invariants. 但是如果它是一个小规模的东西,保持简单,只需要一个维护不变量的域服务。

Sidenote: If the Department and/or Employee are aggregates themselves, then you can reference them within Ticket via their identifier (eg employee's company ID or ID-code of the department). 旁注:如果Department和/或Employee本身是聚合,那么您可以通过其标识符(例如员工的公司ID或部门的ID代码)在Ticket引用它们。 In that way you'll achieve consistency easier as you will not cross consistency boundaries between different aggregates. 通过这种方式,您将更容易实现一致性,因为您不会跨越不同聚合之间的一致性边界。

  1. A FACTORY is responsible for ensuring that all invariants are met for the object or AGGREGATE it creates; FACTORY负责确保对象或其创建的AGGREGATE满足所有不变量; yet you should always think twice before removing the rules applying to an object outside that object. 但在删除应用于该对象外部的对象的规则之前,您应该始终三思而后行。 The FACTORY can delegate invariant checking to the product, and this is often best. FACTORY可以将不变检查委托给产品,这通常是最好的。 [Domain-Driven Design: Tackling Complexity at the Heart of Software] [领域驱动设计:解决软件核心的复杂性]

  2. A depends on question type, but from the look of it it seems like a great candidate for an application layer functionality, i wouldn't go for the event solution though cause i find it only suitable in between layers and not between objects in the same layer. A取决于问题类型,但从它的外观来看,它似乎是应用程序层功能的一个很好的候选者,我不会去寻找事件解决方案,因为我发现它只适用于层之间而不是相同的对象之间层。

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

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