Skip to main content
Table of Contents

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

streamId Guid

The stream id.

event object

The event payload.

cancellationToken CancellationToken

Propagated to the write-store transaction.

Returns

Task

Type Parameters

TAggregate

The 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

streamId Guid

The stream id.

event object

The event payload.

subject EventSubject

The 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.

cancellationToken CancellationToken

Propagated to the write-store transaction.

Returns

Task

Type Parameters

TAggregate

The aggregate type.

Exceptions

ArgumentException

subject names 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

streamId Guid

The stream id.

events IEnumerable<object>

The events to append, in order.

cancellationToken CancellationToken

Propagated to the write-store transaction.

Returns

Task

Type Parameters

TAggregate

The 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

streamId Guid

The stream id.

event object

The creation event (typically an IAggregateCreationEvent).

cancellationToken CancellationToken

Propagated to the write-store transaction.

Returns

Task

Type Parameters

TAggregate

The 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

streamId Guid

The stream id.

events IEnumerable<object>

The events to append, in order.

cancellationToken CancellationToken

Propagated to the write-store transaction.

Returns

Task

Type Parameters

TAggregate

The 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

streamId Guid
cancellationToken CancellationToken

Returns

Task<bool>

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

streamId Guid
cancellationToken CancellationToken

Returns

Task<long>

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

cancellationToken CancellationToken

Returns

Task

Exceptions

ConcurrencyException

Another writer beat this one to the stream's head version.