I'm reading the DDD blue book and there is one thing that's pretty unclear to me as of yet and that is how to efficiently perform simple operations on Aggregates that have a lot of child Entities in them.
An example
- Considering I have an
Order
, (Aggregate Root) that hasLine Item
s. - Each
Order
can only have 1000Line Item
s. (Business Rule).
Steps
In order for me to add a Line Item
to the Order
I'd have to:
- Reconstitute the
Order
with all it'sLine Item
s from it's repository.orderRepo.getById(1)
- Add the
Line Item
to theOrder
,order.addLineItem(lineItem)
- Save the
Order
via the repository again.orderRepo.save(order)
Unless I'm missing something, this strikes me as terribly innefficient. What if I have 900 Line Items
on that Order
? I'd have to load a lot of data from the DB to perform this simple operation.
Yes, I could lazy load the LineItems
but even then I won't be able to check the business rule that requires that <1000 Line Items
must exist in an Order
, unless all the Line Items
are loaded.
Also, lazy loading implementations strike me as an anti-pattern because they usually tie the property accessors with repositories.