Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Aggregate Root Properly

I have some problems designing the aggregate root. Here is how I see it in my mind :)

Store (the aggregate root)
-> Sales - A store create a sale every day
 -> Zones - A store is divided into zones
    -> Styles - A zone has x number of styles
       --> Colors - A style has x number of colors
    etc..

Now based on this my aggregate root would be the store. However if I were now to create a Repository around that, would it look something like this?

public class StoreRepository()
{
  Store GetById() {...}
  StoreZone GetZone() {...}
  List<StoreZoneStyle> GetStylesByZone() {...}
  List<Color> GetColorsByStyle() {...}
}

Is that a good way to continue? Needless to say that I am new to DDD.

like image 713
vikasde Avatar asked Oct 28 '09 20:10

vikasde


People also ask

How do you choose the aggregate root?

When choosing an aggregate root you choose between Transactional Consistency and Eventual Consistency. When your business rules allow you would rather favour Eventual Consistency.

What's an aggregate root?

An Aggregate is the clump of related entities to treat as a unit for data changes. The Aggregate Root is the main entity that holds references to the other ones. It's the only entity in the clump that is used for direct lookup.

What is aggregate design pattern?

Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order and its line-items, these will be separate objects, but it's useful to treat the order (together with its line items) as a single aggregate.

What is aggregate root in C#?

The Aggregate Root is the parent Entity to all other Entities and Value Objects within the Aggregate.


2 Answers

I think you need to look at the aggregate boundaries and the entities as something more than just a hierarchy. Chances are, you will have a richer model than that.

The first way to tell if your aggregate is right, is that you can look at each of the entities within it and ask "Does this need to be directly accessed?" If you answer yes, then that entity is likely not a part of the aggregate.

Without knowing more about your domain, I might assume that Store is indeed an aggregate. Sales, on the other hand, are more complex. Yes, sales occur in a store, but do you have a need to look use a sale independently? If you need them outside of the scope of just working with a store, Sales is probably outside of that aggregate.

I am imagining that both Styles and Colors are immutable and repeatable, so they would likely be Value Objects in this case. Are Zones unique to a store, or do they vary?

Personally, I find value in identifying all of the items in the domain on paper (or whiteboard). I will go through a discovery phase with the stakeholder and just get them out there. Then, use these words as leaders in the conversation, trying to understand how they relate. If you interview the stakeholder well enough, the description he/she gives will actually define most of what you are looking for.

Not to beat a dead horse, but the Evans book is definitely worth getting/reading. It is a little dry, but very insightful. For a quick jumpstart, you can read the free book up on InfoQ that is basically a summary of the Evans book.

like image 162
Joseph Ferris Avatar answered Sep 23 '22 19:09

Joseph Ferris


Aggregate Roots are consistency boundaries for transactions, distributions and concurrency (Eric Evans via Gojko Adzic).

When two people modify different Zones in the same Store, should this cause a concurrency conflict? If not, then perhaps Zones should be their own Aggregate Root separate from Stores.

like image 22
Brian Low Avatar answered Sep 23 '22 19:09

Brian Low