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?
An example
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:
The model
- We run a
Batch Search
every night - The
Batch Search
contains a list ofSearches
for it. - Each
Search
contains a list ofMatches
and is associated with oneCustomer
The process
- We check each
Match
and mark it as confirmed if it was a confirmed match. - We also tick a checkbox for each
Search
if all it'sMatches
where inspected
The caveat
A 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.
Question
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:
Option 1
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);
Option 2
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?