Table of Contents

Interface IDecisionProjection<TState>

Namespace
Opossum.DecisionModel
Assembly
Opossum.dll

Defines an in-memory, ephemeral projection used to build a Decision Model for enforcing consistency during command handling — the DCB write-side pattern.

public interface IDecisionProjection<TState>

Type Parameters

TState

The type that represents the decision model state. May be a value type (e.g. bool, int) or a reference type.

Remarks

Decision Model projections are fundamentally different from Read Model projections (IProjectionDefinition<TState>):

AspectDecision Model vs Read Model
ConsistencyStrong — built from the live event store at command handling time.
PersistenceNever — in-memory only, discarded after the command completes.
LifecycleBorn and dies within a single command handler invocation.
PurposeEnforce business invariants before appending new events.

Use the factory function pattern to create parameterised projection instances:

public static IDecisionProjection<bool> CourseExists(Guid courseId) =>
    new DecisionProjection<bool>(
        initialState: false,
        query: Query.FromItems(new QueryItem
        {
            EventTypes = [nameof(CourseCreatedEvent)],
            Tags = [new Tag("courseId", courseId.ToString())]
        }),
        apply: (state, evt) => evt.Event.Event is CourseCreatedEvent ? true : state);

The Query declared here serves a dual role: it selects which events to load from the store, and it becomes the FailIfEventsMatch query that guards the decision against concurrent writes.

Properties

InitialState

The starting state before any events are applied.

TState InitialState { get; }

Property Value

TState

Query

The query that selects events relevant to this projection from the event store. This same query is used as FailIfEventsMatch to guard the decision against concurrent writes — ensuring the decision model is still valid at the moment of appending.

Query Query { get; }

Property Value

Query

Methods

Apply(TState, SequencedEvent)

Folds a single event into the current state. Must be a pure function with no side effects.

TState Apply(TState state, SequencedEvent evt)

Parameters

state TState

The current accumulated state.

evt SequencedEvent

The next sequenced event to apply.

Returns

TState

The new state after applying the event.