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
TStateThe 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>):
| Aspect | Decision Model vs Read Model |
|---|---|
| Consistency | Strong — built from the live event store at command handling time. |
| Persistence | Never — in-memory only, discarded after the command completes. |
| Lifecycle | Born and dies within a single command handler invocation. |
| Purpose | Enforce 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
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
stateTStateThe current accumulated state.
evtSequencedEventThe next sequenced event to apply.
Returns
- TState
The new state after applying the event.