What should the Aggregate be in this situation (DDD)?

Viewed 25

I am trying to use Domain-Driven Design in my project. In the project, a seller can create a Post to sell Items. A seller can include multiple items in a post; each item includes details such as quantity, size, and price. A buyer can buy one or many item of the same post.

I am not sure if Post or Item should be the aggregate. I personally feel that it would be more correct for Item to be the aggregate, but I am not entirely sure.

A book I am reading states: "An aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes". Say, post X includes two items, a and b. When a buyer buys item a, we should not lock item b even though it also belongs to Post X, so it makes sense to choose Item to be the aggregate.

However, how about entity Post? If I make Item to be the aggregate, Post would not belong to any aggregate? What would the entrypoint be if a seller wants to create/delete a post or change items in a post? so it kinda makes sense to choose Post to be the aggregate too.

Thank you sooo much in advance for your help!

1 Answers

It seems like Post and Item are each good aggregates.

As a meme on social media asks: "Why not both?"

Note that doing this implies that a Post won't contain Items; instead, a Post will refer to Items by some identifier (which identifier would effectively be a value object).

This also suggests that one shouldn't depend on updates to Posts and Items being mutually consistent: you may need to resort to modeling an explicit process in your domain for disabling/deleting an Item and removing that Item from associated Posts, for instance.

Related