In my domain there's a part where I have a root entity, with some fields, and then some other entity inside that aggregate, like this:
class AggregateRootEntity {
... some fields ...
Entity listOfInstancesOfThatEntity = [];
}
Given that the aggregate can have, for instance, 200 instances of Entity
in that list, how would you update the aggregate with a repository? I mean, if I update only an element of that list, I need to call the repository like repository.update(AggregateRootEntity)
, but if I do it like then I do not have a way to know what inner entity has changed, so I would need to either:
- Retrieve all the inner entities from storage and compute which of them have changed.
- Update all of them.
I dislike option 1 because that would make the repository code quite complicated. I also don't like option 2 because that would make every update very costly.
I'm also the same problem when I want to add a new Entity
to the list, or remove one.
Any idea how can this be implemented to solve the listed problems? Am I approaching the problem in the wrong way?