citerus/dddsample-core

Why does the Cargo.java hold a direct reference to the Location.java class

Closed this issue · 4 comments

Why does the Cargo.java hold a direct reference to the Location.java class, isn't the whole idea aggregate roots to avoid references outside of the aggregate root ?

Idea behind aggregates is not to avoid references to roots outside aggregate. But avoid references to non-root entities/value-objects outside aggregate.

Cargo and Location are roots in their aggregates, so they used for references. But for example you don't find references for Delivery in any other aggregates beside Cargo.

Look at domain folder structure where are four directories representing aggregates: cargo, handling, location, voyage.

I think you just miss that Location is aggregate root.

Thanks for the quick reply. One of Vaughn's rules/suggestions is

Rule: Reference Other Aggregates By Identity

Check page 2 of Part 2 Aggregate Design

I believe in the book it is recommended to reference other aggregates by identity and do retrieval through a repository.

This prevents you from say accidentally saving a new location entity while saving a cargo object by an implication of how your ORM is configured.

IMO the advice was not in the context of using an ORM, it was about not having a endlessly inter-connected domain model. By holding entire reference I can potentially traverse the entire object graph which is not great. Nevertheless thanks for sharing this implementation.