I have two "big" entites or aggregates, which have their own business logic - they are saved, updated and destroyed in separate transactions. They have their own child entities, which are manipulated through these aggregate roots. But the problem is, these two aggregates must be in many-to-many relationship to each other. From user interface point of view, there is a kind of UI, where one already existing instance of the second aggregate is added to the first aggregate. In terms of database, there is a table which holds foreign keys to tables of the first and the second aggregate
entity_one_id | entity_two_id
1 | 2
1 | 3
1 | 4
In the example above, an instance of first aggregate holds references to the second aggregate.
And my question is, is it ok from Domain Driven Design perspective, if when saving the first aggregate I load an instance of the second aggregate and add it to the first aggregate. In pseudo-code it may look like:
aggregateOne = aggregateOneRepository->getById(1);
....
aggregate2 = aggregateTwoRepository->getById(2);
aggregate3 = aggregateTwoRepository->getById(3);
aggregate4 = aggregateTwoRepository->getById(4);
aggregateOne->addChildAggregate(aggregate2);
aggregateOne->addChildAggregate(aggregate3);
aggregateOne->addChildAggregate(aggregate4);
aggregateOneRepository->update(aggregateOne);
It seems like in this transaction I do not change the second aggregate and change just one single aggregate. But I'm not sure if DDD theory allows to load multiple different aggregates, when saving one aggregate. So, does this kind of code break the theory or not?
OrderItem
which holds references to both aggregates? – Hausa