In a DDD designed application with CQRS principle how do we handle the case when a command needs to be validated before it is executed on an aggregate and the validation needs to query the existing data?
For instance, in my system I have Customer
, Project
and Ticket
aggregates. A Project
belongs to a specific Customer
and a Ticket
is opened for a specific Project
.
A CreateTicket
command is issued by the current customer. The system populates the command's payload with the customer_id
and project_id
.
Before the commands is executed on the Ticket
aggregate, I need to validate that the command's project indeed belongs to the command's customer.
Another case is this. A command to create a new Department
in the system is issued. A Department has a title
property. Before the department is created I need to verify that there is no other department with the same title in the system. The aggregate repository does not query data. It only fetches by id, and stores aggregates.
How do we handle such cases?