The aggregate pattern
Aggregates are probably one of the hardest patterns of domain-driven design and are, therefore, often implemented incorrectly. This isn’t necessarily bad if it helps you organize your code, but in the worst case, it may hinder your development speed and cause inconsistencies.
In domain-driven design, the aggregate pattern refers to a group of domain objects that can be treated as one for some behaviors. Some examples of aggregate patterns are:
- An order: Typically, an order consists of individual items, but it is helpful to treat them as a single thing (an order) for some purposes within our system.
- A team: A team consists of many employees. In our system, we would likely have a domain object for employees, but grouping them and applying behaviors to them as a team would be helpful in situations such as organizing departments.
- A wallet: Typically, a wallet (even a virtual one) contains many cards and potential currencies for many countries...