How should I perform updates on nested children of an Aggregate Root?
Should I find the child object by traversal of associations and perform the update on it directly, or should I add a method on the Aggregate Root that takes care of it?
I'm modelling an old project at our company which is a system we use to perform searches of customers against a suspicious person list:
- We run a
Batch Searchevery night
Batch Searchcontains a list of
Searchcontains a list of
Matchesand is associated with one
- We check each
Matchand mark it as confirmed if it was a confirmed match.
- We also tick a checkbox for each
Searchif all it's
Search can also run standalone, as in - not being a part of a
Batch Search. In that case the
Search naturally becomes the Aggregate Root.
How should I be performing the update operations on the child items? The blue book states that all updates to children must go through the Aggregate Root.
There are 2 ways to perform this update:
Traverse the graph and find the child object and perform the operation directly on it.
batchSearch = batchSearchRepo.find(1); search = batchSearch.findSearch(5); match = search.getMatch(3); match.markConfirmed(); batchSearchRepo.save(batchSearch);
Add a method on the Aggregate Root
batchSearch = batchSearchRepo.find(1); search = batchSearch.markSearchMatchAsConfirmed(5, 3); batchSearchRepo.save(batchSearch);
Which of the 2 update methods is more appropriate and why?