Table of Contents

Class EventStoreExtensions

Namespace
Opossum.Extensions
Assembly
Opossum.dll

Extension methods for IEventStore to provide convenient overloads and helpers

public static class EventStoreExtensions
Inheritance
EventStoreExtensions
Inherited Members

Methods

AppendAsync(IEventStore, NewEvent, AppendCondition?, CancellationToken)

Appends a single event to the event store

public static Task AppendAsync(this IEventStore eventStore, NewEvent @event, AppendCondition? condition = null, CancellationToken cancellationToken = default)

Parameters

eventStore IEventStore

The event store

event NewEvent

The new event to append

condition AppendCondition

Optional append condition for optimistic concurrency control

cancellationToken CancellationToken

A token to cancel the asynchronous operation

Returns

Task

A task representing the asynchronous operation

AppendAsync(IEventStore, NewEvent[], CancellationToken)

Appends multiple events to the event store without an append condition

public static Task AppendAsync(this IEventStore eventStore, NewEvent[] events, CancellationToken cancellationToken = default)

Parameters

eventStore IEventStore

The event store

events NewEvent[]

The new events to append

cancellationToken CancellationToken

A token to cancel the asynchronous operation

Returns

Task

A task representing the asynchronous operation

AppendEventAsync(IEventStore, IEvent, IEnumerable<Tag>?, Metadata?, AppendCondition?, CancellationToken)

Appends a single domain event to the event store with simplified syntax

public static Task AppendEventAsync(this IEventStore eventStore, IEvent @event, IEnumerable<Tag>? tags = null, Metadata? metadata = null, AppendCondition? condition = null, CancellationToken cancellationToken = default)

Parameters

eventStore IEventStore

The event store

event IEvent

The domain event to append

tags IEnumerable<Tag>

Optional tags to attach to the event

metadata Metadata

Optional metadata (timestamp and correlation ID auto-generated if not provided)

condition AppendCondition

Optional append condition for optimistic concurrency control

cancellationToken CancellationToken

A token to cancel the asynchronous operation

Returns

Task

A task representing the asynchronous operation

AppendEventsAsync(IEventStore, IEvent[], IEnumerable<Tag>?, Metadata?, AppendCondition?, CancellationToken)

Appends multiple domain events to the event store with simplified syntax

public static Task AppendEventsAsync(this IEventStore eventStore, IEvent[] events, IEnumerable<Tag>? tags = null, Metadata? metadata = null, AppendCondition? condition = null, CancellationToken cancellationToken = default)

Parameters

eventStore IEventStore

The event store

events IEvent[]

The domain events to append

tags IEnumerable<Tag>

Optional tags to attach to all events

metadata Metadata

Optional metadata (timestamp and correlation ID auto-generated if not provided)

condition AppendCondition

Optional append condition for optimistic concurrency control

cancellationToken CancellationToken

A token to cancel the asynchronous operation

Returns

Task

A task representing the asynchronous operation

BuildProjections<TProjection>(SequencedEvent[], Func<SequencedEvent, string>, Func<IEvent, TProjection?, TProjection?>)

Builds projection objects by grouping events by a shared key and folding them sequentially. Each unique key value produces one projection instance — equivalent to a GroupBy followed by a left-fold.

public static IEnumerable<TProjection> BuildProjections<TProjection>(this SequencedEvent[] events, Func<SequencedEvent, string> keySelector, Func<IEvent, TProjection?, TProjection?> applyEvent) where TProjection : class

Parameters

events SequencedEvent[]

Events to process

keySelector Func<SequencedEvent, string>

Function that extracts the grouping key from each event. All events returning the same key are folded into a single projection instance. In DCB terms this is typically the value of a domain identity tag (e.g. the value of a studentId tag).

applyEvent Func<IEvent, TProjection, TProjection>

Function to apply an event to the current projection state (null on the first event for each key)

Returns

IEnumerable<TProjection>

Enumerable of built projections, one per unique key value

Type Parameters

TProjection

The projection type to build

Examples

var students = events.BuildProjections<StudentShortInfo>(
    keySelector: e => e.Event.Tags.First(t => t.Key == "studentId").Value,
    applyEvent: (evt, current) => evt switch
    {
        StudentRegisteredEvent registered => new StudentShortInfo(...),
        StudentUpdatedEvent updated when current != null => current with { ... },
        _ => current
    }
);

ReadAsync(IEventStore, Query)

Reads events from the event store without any read options

public static Task<SequencedEvent[]> ReadAsync(this IEventStore eventStore, Query query)

Parameters

eventStore IEventStore

The event store

query Query

The query to filter events

Returns

Task<SequencedEvent[]>

A task representing the asynchronous operation returning the matching events

ReadAsync(IEventStore, Query, ReadOption)

Reads events from the event store with a single read option

public static Task<SequencedEvent[]> ReadAsync(this IEventStore eventStore, Query query, ReadOption readOption)

Parameters

eventStore IEventStore

The event store

query Query

The query to filter events

readOption ReadOption

Optional read option (e.g., Descending)

Returns

Task<SequencedEvent[]>

A task representing the asynchronous operation returning the matching events

ReadAsync(IEventStore, Query, long)

Reads events from the event store that were appended after fromPosition, without any read options. Useful for incrementally polling new events from a known checkpoint.

public static Task<SequencedEvent[]> ReadAsync(this IEventStore eventStore, Query query, long fromPosition)

Parameters

eventStore IEventStore

The event store

query Query

The query to filter events

fromPosition long

Only events with Position > fromPosition are returned. Pass the last processed sequence position to receive only new events.

Returns

Task<SequencedEvent[]>

A task representing the asynchronous operation returning the matching events