Dealing with nested aggregates in DDD
Asked Answered
H

1

18

I'm just getting started in DDD, and I'm having some trouble figuring out how to accommodate the relational nature of my data. I have what I believe would be considered my aggregate root, but the aggregate also has an aggregate of its own. Not wanting to violate the Law of Demeter, I'm wondering if I'm thinking about this wrong and am hoping that some DDD expert can offer some insight.

My aggregate root is my Account object, which has an aggregate of numerous AccountElement entities, which are themselves logical groupings of individual ProductComponent entities.

An AccountElement outside of the context of an Account has no meaning, so I'm comfortable with my conclusion that the Account object is my aggregate root, and I anticipate that entity having an aggregate Elements property. It's the ProductComponent collection that has me confused. This aggregate has no meaning outside of an AccountElement, and really has no meaning outside of an Account.

I don't think I should be accessing individual ProductComponent objects by dotting my way to it, like:

var reference = account.Elements(0).ProductComponents(0).ReferenceCode;

But at the same time it doesn't make sense (from a domain perspective) to access a ProductComponent directly from an Account entity.

I'm sure that this is all a little difficult to comprehend without knowledge of my domain, but I'm hoping it's enough to get some good feedback.

Huneycutt answered 5/2, 2010 at 20:39 Comment(4)
If you require this level of nesting objects to make your object model work, then I wouldn't worry too much about the Law of Demeter. See haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspxBluecollar
See also dcmanges.com/blog/37Bluecollar
Thanks, Phil's article was helpful, and the question of how inextricably related the children of the children are to the aggregate root is really key here. Looking at that, it only makes sense to nest the entities. I'm more comfortable this is correct because after making this decision the direction of the code seems to be a lot more intuitive. Or in the spirit of DDD, I might be better off saying it is a truer abstraction of the ubiquitous language ;)Huneycutt
Have you considered modeling the second aggregate rather as a composite?Cater
S
2

The article Robert linked to is a good one. I would add that if ProductComponent exists only in the context of AccountElement and AccountElement exists only in the context of Account, then by extension ProductComponent is in the context of Account.

Supertanker answered 5/2, 2010 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.