What are Aggregates?
What is an Aggregate?
Aggregate is a confusing word. It sounds like you are "aggregating" information but really it is about aggregating behavior. The best way to visualize an Aggregate is to think of it as a boundary within your Domain Model.
- A high-level API, implemented by a single entry point (Aggregate Root)
- Each Aggregate has a unique identifier
- Creates Value Objects, Domain Events and Entities
- An Aggregate is a consistency boundary, meaning it is always consistent for the external viewer at any point in time.
- All code within an Aggregate should be synchronous
The Aggregate Root
The central piece of an Aggregate is the Aggregate Root. It is the high-level API that provides access to the functionality within the Aggregate. In object-oriented languages we usually implement the Aggregate Root as a class but since it basically is a collection of functions it is as easy to implement in functional languages as well. We have used these concepts successfully in languages with different paradigms and programming models.
How to call the Aggregate?
An aggregate is a boundary within your Domain Model. It is where the core business rules of your application should be implemented. In order to access this functionality the Aggregate provide a higher-level API following intention-revealing interfaces and preferably CQS-principle.
How Aggregates relate to CQRS?
When developing applications using CQRS the Aggregates provide the C-part of the equation. Their role is to uphold the business rules and to store the changes that the users make to the system.
Historically, it has been common to mix business rules with query support, causing complex implementations of Domain Models. We nowadays separate the query models from the Aggregates so that we can both scale the query support and simplify the implementation of both these models.
There is no need for a technical framework to start working with Domain-Driven Design and implementing these concepts. In fact, we strongly recommend that you keep your Aggregate, and your Domain Model infrastructure free. This has a number of good side effects:
- Easier to test
- Easier to maintain over time
- Easier to read and understand
More useful links about Aggregates: