I'm using DDD and implementing it via ES/CQRS for this project.
Company has multiple offices and will open and close these offices
from time to time.
So I have modeled two aggregates that also represent two streams:
Company Aggregate = Company Stream
Office Aggregate = Office Stream
Company would hold a set of office references and the office would
hold a reference back to company. This reason for a separate Office aggregate
is because I do not want to be updating the Company aggregate every time the office
hours could change since offices are open at different times.
Company Aggregate
ID id
Set<ID> offices
...
Office Aggregate
ID id
ID companyId
....
I know that the Company is responsible for opening and closing offices so I implemented a
aggregate factory method in Company to open an office with a method openOffice. This
method will create an aggregate office for the company. I created a command of
open office with
class OpenOfficeCommand {
ID officeID;
ID companyID;
String name;
String code;
}
In my handler I'm looking up the company aggregate from the company stream, after that I call the openOffice method on the company aggregate to open the office. Inside
the aggregate method an Office aggregate is created and an event is generated ("OfficeOpened") and applied to the Company aggregate and the offices set is updated with the officeID the result of the method is the Office Aggregate. The issue that I'm wrestling with is that I will execute two saves that would need to be under one transaction. The save of the Company aggregate root to persist the new state of the Company and a save of the new Office aggregate to its stream.
Is this a correct process?
Thank you
-kurt