Suppose I have one aggregate, Ticket
. A Ticket
will have one assigned Department
and one or more assigned Employee
.
When instantiating a Ticket
, should a TicketFactory
be responsible for ensuring that a Ticket
is created with a valid/existent Department
and Employee
?
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?
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.
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.