Aggregate

Space.eventSourcing.Aggregate

Aggregates are a cluster of domain objects that are treated as a single unit where your business logic lives. How you determine the boundary of an Aggregate takes a deep understanding of the domain you're modelling, and the application of Domain Driven Design. The aggregate encapsulates all Entities and Value Object that are required by it to enact on business rules, and protect against invariants. Commands are sent into the domain layer to instantiate an Aggregate object, then further commands are routed to it from the Router by matching the GUID

πŸ“˜

What's happening under the hood?

An event-sourced Aggregate's state is determined by the events it generates. Commands are handled by the Router, either instantiating a new aggregate or rebuilding an existing one by replaying all the associated events from the Commit Store. After processing a command, the aggregate saves a new event to it's internal array, which is then committed to the Store by the Router, then published into the event bus for subscribers such as Process Managers, Projections for managing a View Cache in the application layer, or integration with other bounded contexts.

Initializing command

Command processing configuration

Map Commands passed into the aggregate by the Router to a private method.

Business logic methods

This is where the business rules and logic is contained. By referencing it's own state, the aggregate makes decisions, and then saves an Events into the internal events array with the outcome.

🚧

External dependencies

There should be no need to rely on external dependencies within an Aggregate, as this would indicate you have an incorrect boundary defined and should consider revising your design.

πŸ“˜

Why are these methods private?

Commands in a CQRS system are the only public interface. The aggregate holds the configuration for mapping an incoming command to a method.

πŸ“˜

If the aggregate isn't publishing events, who is?

The aggregate knows nothing about the infrastructure, it just knows that it receives Commands and creates Events. These events are published at a later stage after the changes are persisted.

Mapping events to state change

Each Event the aggregate generates needs to be mapped to a state translation function.