Skip to content

Aggregate store

Eventuous provides a number of extensions for IEventReader and IEventWriter interfaces for aggregate persistence.

Storing an aggregate instance implies appending new events from the list of changes to the aggregate stream. Functions that allow that are listed below. All those are extensions to IEventWriter interface.

This function requires a pre-calculated stream name to be provided, so it doesn’t use any convention for resolving stream names. It uses the provided stream name as-is.

Task<AppendEventsResult> IEventWriter.StoreAggregate<TAggregate, TState>(
StreamName streamName,
TAggregate aggregate,
AmendEvent? amendEvent = null,
CancellationToken cancellationToken = default
)
ParameterTypeDescription
streamNameStreamNameAggregate stream name
aggregateTAggregate<TState>Aggregate instance
amendEventFunc<StreamEvent, StreamEvent>Function that allows adding things like custom metadata
cancellationTokenCancellationTokenCancellation token

This function uses the stream name map to convert the provided aggregate identity to a stream name. If the stream name map is not supplied, it will use the default, convention-based calculation (TypeName-Id).

Task<AppendEventsResult> IEventWriter.StoreAggregate<TAggregate, TState, TId>(
TAggregate aggregate,
TId id,
StreamNameMap? streamNameMap = null,
AmendEvent? amendEvent = null,
CancellationToken cancellationToken = default
)
ParameterTypeDescription
aggregateTAggregate<TState>Aggregate instance
idTIdAggregate identity
streamNameMapStreamNameMapMap between identities and stream names
amendEventFunc<StreamEvent, StreamEvent>Function that allows adding things like custom metadata
cancellationTokenCancellationTokenCancellation token

This function supports storing aggregates with identity-aware state (State<TId>) and uses the aggregate State.Id property combined with the stream name map to resolve the stream name. If the stream name map is not supplied, it will use convention-based stream name calculation.

Task<AppendEventsResult> IEventWriter.StoreAggregate<TAggregate, TState, TId>(
TAggregate aggregate,
StreamNameMap? streamNameMap = null,
AmendEvent? amendEvent = null,
CancellationToken cancellationToken = default
)
ParameterTypeDescription
aggregateTAggregate<TState, TId>Aggregate instance
streamNameMapStreamNameMapMap between identities and stream names
amendEventFunc<StreamEvent, StreamEvent>Function that allows adding things like custom metadata
cancellationTokenCancellationTokenCancellation token

Several extensions for IEventReader interface allow loading aggregates from an event store.

This function requires a pre-calculated stream name to be provided, so it doesn’t use any convention for resolving stream names. It uses the provided stream name as-is.

Task<TAggregate> LoadAggregate<TAggregate, TState>(
StreamName streamName,
bool failIfNotFound = true,
AggregateFactoryRegistry? factoryRegistry = null,
CancellationToken cancellationToken = default
)
ParameterTypeDescription
streamNameStreamNameAggregate stream name
failIfNotFoundboolWhen set to true, the function will throw an AggregateNotFound exception if the stream cannot be found. Otherwise, it will return a new aggregate instance.
factoryRegistryAggregateFactoryRegistryOptional aggregate factory registry, which is used to create new aggregate instances. If not supplied, the default registry is used.
cancellationTokenCancellationTokenCancellation token

This function uses the stream name map to convert the provided aggregate identity to a stream name. If the stream name map is not supplied, it will use the default, convention-based calculation (TypeName-Id).

Task<TAggregate> IEventWriter.LoadAggregate<TAggregate, TState, TId>(
TId aggregateId,
StreamNameMap? streamNameMap = null,
bool failIfNotFound = true,
AggregateFactoryRegistry? factoryRegistry = null,
CancellationToken cancellationToken = default
)
ParameterTypeDescription
idTIdAggregate identity
streamNameMapStreamNameMapMap between identities and stream names
failIfNotFoundboolWhen set to true, the function will throw an AggregateNotFound exception if the stream cannot be found. Otherwise, it will return a new aggregate instance.
factoryRegistryAggregateFactoryRegistryOptional aggregate factory registry, which is used to create new aggregate instances. If not supplied, the default registry is used.
cancellationTokenCancellationTokenCancellation token