What method do you use to identify the Aggregate R

2019-05-16 17:00发布

问题:

When applying Domain Driven Design to a project, how do you identify the Aggregate Roots?

For example, in a standard E-Commerce website, you might say that the Order is one, and the User is the other. But what if your Users belong to a Company? Does that make your Company the aggregate root?

I'm interested in hearing people's approaches to working out the Aggregate roots, and how to identify poorly chosen aggregate roots.

回答1:

One good way of identifying the aggregate root is to use the "delete" test. In your domain if you delete the root, what is deleted with it? This way you can identify domain object ownership, which is a trait of Aggregates.

Also Aggregates create consistency boundaries, so your root, should "hide" aggregated elements from the rest of the object graph and also check their consistency and invariants which should hold. Object inside the Aggregate hold references only to the root (not each other). So if you find someting like this in your domain model, it might suggest you have an Aggregate root.