Table of Contents

Interface IMaterializedViewBuilder<TView>

Namespace
Excalibur.EventSourcing
Assembly
Excalibur.EventSourcing.Abstractions.dll

Defines the contract for building materialized views from events.

public interface IMaterializedViewBuilder<TView> where TView : class, new()

Type Parameters

TView

The type of the materialized view.

Remarks

Materialized view builders transform event streams into read-optimized projections. Each builder is responsible for:

  • Subscribing to relevant events
  • Updating the view state based on events
  • Managing idempotent event processing

Implementations should be registered in the DI container and will be automatically discovered for subscription management.

Idempotency: Builders should handle duplicate event deliveries gracefully. Use position tracking via GetPositionAsync(string, CancellationToken) and SavePositionAsync(string, long, CancellationToken) to ensure exactly-once processing.

Properties

DeliverySemantics

Gets the delivery guarantee this projection requires from the view store.

ViewDeliverySemantics DeliverySemantics { get; }

Property Value

ViewDeliverySemantics

Defaults to ExactlyOnce — an accumulating projection that must not double-count on crash and therefore requires an atomic view store. Override to AtLeastOnceIdempotent when Apply(TView, IDomainEvent) is idempotent (upsert-by-view-id), which tolerates at-least-once replay and permits a non-atomic store such as Elasticsearch or OpenSearch.

HandledEventTypes

Gets the types of events this builder handles.

IReadOnlyList<Type> HandledEventTypes { get; }

Property Value

IReadOnlyList<Type>

The event types this builder subscribes to.

ViewName

Gets the name of this view builder for position tracking.

string ViewName { get; }

Property Value

string

A unique name identifying this view builder.

Methods

Apply(TView, IDomainEvent)

Applies an event to the view, updating its state.

TView Apply(TView view, IDomainEvent @event)

Parameters

view TView

The current view state.

event IDomainEvent

The event to apply.

Returns

TView

The updated view state.

Remarks

This method is called for each event that matches HandledEventTypes. The view passed in may be a newly created instance (if the view doesn't exist yet) or an existing view from the store.

CreateNew()

Creates a new instance of the view.

TView CreateNew()

Returns

TView

A new view instance.

GetViewId(IDomainEvent)

Determines the view ID for an event.

string? GetViewId(IDomainEvent @event)

Parameters

event IDomainEvent

The event to get the view ID for.

Returns

string

The view ID, or null if the event should not update any view.

Remarks

This method extracts the view identifier from an event. For example, an OrderCreated event might return the OrderId as the view ID.