Interface IEventSource
- Namespace
- Stratara.Abstractions.EventSourcing
- Assembly
- Stratara.Abstractions.dll
Write-side façade for the event store. Command handlers append events through this service; the implementation tracks pending writes and flushes them on SaveChangesAsync(CancellationToken).
public interface IEventSource
- Extension Methods
Examples
Append events from a command handler and commit the unit-of-work:
public sealed class CreateOrderHandler(IEventSource events) : ICommandHandler<CreateOrder>
{
public async Task HandleAsync(CreateOrder command, CancellationToken cancellationToken)
{
await events.CreateAsync<Order>(command.OrderId,
new OrderCreated(command.OrderId, command.CustomerId, command.Amount),
cancellationToken);
await events.SaveChangesAsync(cancellationToken);
}
}
Remarks
All AppendAsync / CreateAsync calls infer the Subject (data-owner) from
the ambient ISessionContextProvider. Use
AppendOnBehalfOfAsync<TAggregate>(Guid, object, EventSubject, CancellationToken) when the Subject must be overridden
(PlatformAdmin cross-tenant flows, EventStoreMigration regeneration).
Methods
AppendAsync<TAggregate>(Guid, object, CancellationToken)
Append an event to an existing stream.
Task AppendAsync<TAggregate>(Guid streamId, object @event, CancellationToken cancellationToken = default) where TAggregate : notnull, new()
Parameters
streamIdGuidThe stream id.
eventobjectThe event payload.
cancellationTokenCancellationTokenPropagated to the write-store transaction.
Returns
Type Parameters
TAggregateThe aggregate type.
AppendOnBehalfOfAsync<TAggregate>(Guid, object, EventSubject, CancellationToken)
Append an event with an explicit Subject (data owner), overriding the SessionContext-derived Subject. Used by PlatformAdmin cross-tenant flows and EventStoreMigration regeneration. The Actor stays the calling SessionContext's ActorTenantId/ActorUserId.
Task AppendOnBehalfOfAsync<TAggregate>(Guid streamId, object @event, EventSubject subject, CancellationToken cancellationToken = default) where TAggregate : notnull, new()
Parameters
streamIdGuidThe stream id.
eventobjectThe event payload.
subjectEventSubjectThe data owner to record. Its tenant id must be non-empty: stating the Subject also states that the stream, the event and the session are not to be consulted, so an empty one fails the append rather than falling back to them.
cancellationTokenCancellationTokenPropagated to the write-store transaction.
Returns
Type Parameters
TAggregateThe aggregate type.
Exceptions
- ArgumentException
subjectnames no tenant.
AppendRangeAsync<TAggregate>(Guid, IEnumerable<object>, CancellationToken)
Append multiple events to an existing stream in order.
Task AppendRangeAsync<TAggregate>(Guid streamId, IEnumerable<object> events, CancellationToken cancellationToken = default) where TAggregate : notnull, new()
Parameters
streamIdGuidThe stream id.
eventsIEnumerable<object>The events to append, in order.
cancellationTokenCancellationTokenPropagated to the write-store transaction.
Returns
Type Parameters
TAggregateThe aggregate type.
CreateAsync<TAggregate>(Guid, object, CancellationToken)
Create a new stream with the first event. Fails if the stream already exists.
Task CreateAsync<TAggregate>(Guid streamId, object @event, CancellationToken cancellationToken = default) where TAggregate : notnull, new()
Parameters
streamIdGuidThe stream id.
eventobjectThe creation event (typically an IAggregateCreationEvent).
cancellationTokenCancellationTokenPropagated to the write-store transaction.
Returns
Type Parameters
TAggregateThe aggregate type the stream represents.
CreateRangeAsync<TAggregate>(Guid, IEnumerable<object>, CancellationToken)
Create a new stream with multiple events in order.
Task CreateRangeAsync<TAggregate>(Guid streamId, IEnumerable<object> events, CancellationToken cancellationToken = default) where TAggregate : notnull, new()
Parameters
streamIdGuidThe stream id.
eventsIEnumerable<object>The events to append, in order.
cancellationTokenCancellationTokenPropagated to the write-store transaction.
Returns
Type Parameters
TAggregateThe aggregate type the stream represents.
ExistsAsync(Guid, CancellationToken)
Returns true if the stream exists in the event store.
Task<bool> ExistsAsync(Guid streamId, CancellationToken cancellationToken = default)
Parameters
streamIdGuidcancellationTokenCancellationToken
Returns
GetCurrentVersionAsync(Guid, CancellationToken)
Returns the head version of the stream, or 0 if it does not exist.
Task<long> GetCurrentVersionAsync(Guid streamId, CancellationToken cancellationToken = default)
Parameters
streamIdGuidcancellationTokenCancellationToken
Returns
SaveChangesAsync(CancellationToken)
Flush every pending append/create to the underlying write store. Throws ConcurrencyException if another writer committed first.
Task SaveChangesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
Exceptions
- ConcurrencyException
Another writer beat this one to the stream's head version.