Aggregates

Defining Aggregates

Aggregates are domain objects that encapsulate the state and behavior of a particular entity. They are reconstructed from a stream of events and are used to enforce business rules.

Example Aggregate:
@AggregateRoot
public class Customer {
  @AggregateId
  private String id;
  private String firstName;
  private String lastName;
  private String address;
  // getters and setters
}

Event Sourcing

Event sourcing handlers are responsible for applying events to reconstruct the state of an aggregate. They ensure that the current state can always be derived from the event history.

Snapshotting

Snapshotting allows you to take periodic snapshots of an aggregate's state, reducing the need to replay the entire event history. This can improve performance, especially for aggregates with a long history. Snapshots can be configured to be taken after a certain number of events.