Reputation: 5042
Is it okay to get a read-only collection from an aggregate without going through the root to get it? My model does some of this right now and I was wondering if that's an acceptable design. Thanks
Edit:
Here's an example
I have an aggregate root entity called UserAccount and another aggregate root called VideoStore. Users can have multiple stores they are apart of and video stores can have many users. A very basic many-to-many, but it's not because the many-to-many bridge table contains state information so it has to be an entity as well. So, I have an bridge entity called UserVideoStores and its a child of the aggregate root VideStore (one-to-many).
Now when a user logs in I want to lookup which VideoStores they are apart of and display that info to them. I can easily do this by making the UserAccount entity have a direct (one-to-many) reference to the child, UserVideoStores, of the aggregate root VideoStores. It seems easier to do this then have to use an HQL query and search from the bottom of the graph up to find which stores the user is apart of.
Does that make sense?
Edit:
Well I came up with a solution to make my model cleaner. I wasn't thinking straight about some of my designs and I learned how to use nHibernate a little bit better to help me come up with a solution. Thanks
Upvotes: 2
Views: 784
Reputation: 4951
Actually Eric has changed his mind about the strictness of the Aggregate Root Rules.
He recently said something along the lines of
"Chill out dude. Try to stop yourself from breaking the rules man. But, hey, don't beat yourself up over it. DDD rocks dude, but break the Agg rules if ya really need to. Yeah, peace."
See: "Eric Evans: What I've learned about DDD since the book" http://dddcommunity.org/library/evans_2009_1
Upvotes: 2
Reputation: 3062
Evans says "The root is the only member of the AGGREGATE that outside objects are allowed to hold references to..." (p. 127)
My understanding is that the aggregate should appear as a unit to outside objects. Also, the Law of Demeter would seem to apply. Bottom line, I don't think it's acceptable.
Upvotes: 2