Search code examples
eventsdomain-driven-designseparation-of-concerns

Which objects are responsible for maintaining references between aggregates?


Suppose I have one aggregate, Ticket. A Ticket will have one assigned Department and one or more assigned 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?

  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? 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?


Solution

    1. The TicketFactory would be declare that in order to create a Ticket you need references to both a Department and an Employee. 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. 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.

    Take a look at Effective Aggregate Design for more on this.