Saving complex aggregates using Repository Pattern
Posted
by Kevin Lawrence
on Stack Overflow
See other posts from Stack Overflow
or by Kevin Lawrence
Published on 2010-03-31T23:47:27Z
Indexed on
2010/03/31
23:53 UTC
Read the original article
Hit count: 339
domain-driven-design
|repository-pattern
We have a complex aggregate (sensitive names obfuscated for confidentiality reasons).
The root, R, is composed of collections of Ms, As, Cs, Ss. Ms have collections of other low-level details. etc etc
R really is an aggregate (no fair suggesting we split it!)
We use lazy loading to retrieve the details. No problem there.
But we are struggling a little with how to save such a complex aggregate.
From the caller's point of view:
r = repository.find(id); r.Ps.add(factory.createP()); r.Cs[5].updateX(123); r.Ms.removeAt(5); repository.save(r);
Our competing solutions are:
Dirty flags Each entity in the aggregate in the aggregate has a dirty flag. The save() method in the repository walks the tree looking for dirty objects and saves them. Deletes and adds are a little trickier - especially with lazy-loading - but doable.
Event listener accumulates changes. Repository subscribes a listener to changes and accumulates events. When save is called, the repository grabs all the change events and writes them to the DB.
Give up on repository pattern. Implement overloaded save methods to save the parts of the aggregate separately. The original example would become:
r = repository.find(id); r.Ps.add(factory.createP()); r.Cs[5].updateX(123); r.Ms.removeAt(5); repository.save(r.Ps); repository.save(r.Cs); repository.save(r.Ms);
(or worse)
Advice please! What should we do?
© Stack Overflow or respective owner