AggregateRoot<TState extends State> class abstract

Base class for aggregate roots in domain-driven design Combines PersistentActor with domain logic and command processing

Inheritance

Constructors

AggregateRoot({required String aggregateId, required String aggregateType, required EventStore eventStore, CommandHandlerRegistry<TState>? commandHandlers, EventHandlerRegistry<TState>? eventHandlers})
Create an aggregate root with the specified aggregate ID and type

Properties

aggregateId String
The aggregate ID for this aggregate root
no setter
aggregateType String
The aggregate type for this aggregate root
no setter
commandHandlers CommandHandlerRegistry<TState>
Command handler registry for this aggregate
no setter
currentState → TState
Current state of the aggregate (non-nullable) Throws if the aggregate hasn't been initialized
no setter
eventHandlers EventHandlerRegistry<TState>
Event handler registry for this aggregate
no setter
eventStore EventStore
Current event store instance
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
isInitialized bool
Whether the aggregate has been initialized with state
no setter
isRecovered bool
Whether this actor has completed recovery
no setterinherited
isRecovering bool
Whether this actor is currently recovering from events
no setterinherited
persistenceId String
Unique identifier for this persistent actor Used to store and retrieve events from the event store
no setterinherited
recoveryComplete Future<void>
Future that completes when recovery finishes.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sequenceNumber int
Current sequence number for this actor Incremented with each persisted event
no setterinherited
state → TState?
Current state of the aggregate Returns null if the aggregate hasn't been initialized yet
no setter

Methods

applyEvent(TState currentState, Event event) → TState
Apply an event to the current state and return the new state This is the core method for event sourcing state transitions
canApplyEvent(Event event) bool
Check if the aggregate can apply a specific event Override in subclasses for custom event routing
canHandleCommand(Command command) bool
Check if the aggregate can handle a specific command Override in subclasses for custom command routing
checkConcurrency(int expectedVersion) Future<void>
Check optimistic concurrency control with expected version Public method for explicit concurrency checking
commandHandler(Command command) Future<void>
Process a command through the full pipeline This is the main entry point for command processing.
override
createInitialState() → TState
Create the initial state for this aggregate Override in subclasses to provide the initial state
createSnapshot() Future<void>
Create a snapshot of current state Override to provide custom snapshot logic
inherited
ensureStateInitialized() → void
Initialize the aggregate state This is a protected method that subclasses can use when overriding eventHandler to ensure state is initialized before processing events (replicating base class behavior)
eventHandler(Event event) → void
Apply an event to update the aggregate state This is called during both command processing and recovery
override
getAggregateInfo() AggregateInfo
Get aggregate information for debugging and monitoring
getBusinessRuleViolations(TState state) List<String>
Get business rule violations for the current state Override in subclasses to provide specific violation messages
getSnapshotState() Future
Get the current state for snapshot creation
override
handleCommand(TState currentState, Command command) Future<List<Event>>
Handle a command and generate events This is the core method for command processing
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onCommandFailure(Command command, dynamic error) Future<void>
Called when command processing fails Override to handle command failures
inherited
onCommandProcessed(Command command, List<Event> events) Future<void>
Called after a command has been successfully processed Override to perform post-command processing actions
onError(Object error, StackTrace stackTrace) → void
Called when an error occurs.
inherited
onEventApplicationFailure(Event event, dynamic error) → void
Called when event application fails Override to handle event application failures
onEventApplied(Event event, TState newState) → void
Called after an event has been applied to the state Override to perform post-event application actions
onMessage(dynamic message) Future<void>
Handle incoming messages with command/event routing
inherited
onMessageDropped(dynamic message) Future<void>
Called when a message is dropped because recovery timed out or failed. Override to send error responses, log, or enqueue for retry.
inherited
onPersist(Event event) Future<void>
Called after successful event persistence Override to perform post-persistence actions
inherited
onPersistBatch(List<Event> events) Future<void>
Called after successful batch event persistence Override to perform post-persistence actions for batches
inherited
onPersistBatchFailure(List<Event> events, dynamic error) Future<void>
Called when batch event persistence fails Override to handle batch persistence failures
inherited
onPersistFailure(Event event, dynamic error) Future<void>
Called when event persistence fails Override to handle persistence failures
inherited
onRecover() Future<void>
Called at the start of recovery process Override to perform custom recovery initialization
inherited
onRecoveryComplete() Future<void>
Called when recovery is complete Override to perform post-recovery initialization
inherited
onRecoveryFailure(dynamic error) Future<void>
Called when recovery fails Override to handle recovery failures
inherited
onReplay(Event event) Future<void>
Called for each event during replay Override to perform custom replay logic
inherited
onSnapshot(dynamic snapshotState, int sequenceNumber) Future<void>
Restore state from snapshot during recovery
override
onSnapshotCreated(dynamic state, int sequenceNumber) Future<void>
Called when a snapshot is created Override to perform post-snapshot actions
inherited
onSnapshotRestorationFailure(dynamic snapshotData, int sequenceNumber, dynamic error) Future<void>
Called when snapshot restoration fails Override to handle snapshot restoration failures
onSnapshotRestored(TState state, int sequenceNumber) Future<void>
Called after state has been restored from a snapshot Override to perform post-snapshot restoration actions
persistEvent(Event event) Future<void>
Persist an event to the event store Events are immutable and stored permanently
inherited
persistEvents(List<Event> events) Future<void>
Persist multiple events atomically All events succeed or all fail together
inherited
postStop() → void
Called when the actor is stopped.
inherited
preStart() → void
Called when the actor is started.
override
queryHandler(dynamic message) Future<void>
Handles incoming queries or other non-persistent messages.
inherited
registerHandlers() → void
Register command and event handlers for this aggregate Override in subclasses to register domain-specific handlers
replay(List<Event> events) → void
Replay events to rebuild aggregate state Used during recovery and testing
restoreFromSnapshot(dynamic snapshotData, int sequenceNumber) Future<TState>
Restore state from snapshot data Override in subclasses to provide custom restoration logic
restoreStateFromMap(Map<String, dynamic> map, int sequenceNumber) Future<TState>
Restore state from a map representation Override in subclasses to provide custom map restoration
toString() String
A string representation of this object.
override
validateBusinessRules(TState state) bool
Validate business rules for the current state Override in subclasses to implement domain-specific validation

Operators

operator ==(Object other) bool
The equality operator.
inherited