class AggregateRoot
Base class for implementing event-sourced aggregate roots.
Aggregate roots are the core building blocks of domain-driven design and event sourcing. They encapsulate business logic, maintain consistency boundaries, and generate events that represent state changes. The aggregate’s current state is rebuilt by replaying its historical events through reducer functions.
Key Features: - *\*Event Sourcing\**: State is derived from events, not stored directly - \**Business Logic\**: Encapsulates domain rules and invariants - \**Event Generation\**: Emits events when state changes occur - \**Immutable History\**: Complete audit trail of all changes - \**Checkpointing\*\*: Optional performance optimization for large event histories
Properties
Methods
currentCheckpoint()
Returns the current state as a checkpoint for performance optimization.
Checkpoints allow aggregates with long event histories to snapshot their current state, reducing the time needed to rebuild from events. This method should be implemented when storeCheckpoint is enabled in the config.
currentCheckpoint(): object;
handleEvent(event)
Applies an event to the aggregate state using the appropriate reducer.
This method is called automatically by raiseEvent and during event replay. It looks up the reducer function for the event type and applies the event to update the aggregate’s state.
| Name | Type | Description |
|---|---|---|
|
The event to apply to the aggregate state |
|
Returns |
|
handleEvent(event: EventicleEvent): void;
raiseEvent(event)
Raises a new domain event and applies it to the aggregate state.
This is the primary method for recording state changes in event-sourced aggregates. The event is automatically assigned metadata (ID, timestamp, source) and applied through the appropriate reducer function.
| Name | Type | Description |
|---|---|---|
|
The domain event to raise |
|
Returns |
|
The event with populated metadata |
raiseEvent(event: EventicleEvent): EventicleEvent<any>;