Skip to content

Cratis Chronicle reducer event sequence selection

A Cratis Chronicle reducer reads one event sequence: an ordered stream such as the event log or an inbox. Select a sequence explicitly when the reducer should read somewhere other than its default or inferred source.

In .NET, reducers read the default event log unless event-store metadata points their handled events to another store’s inbox. Use [EventSequence] or the eventSequence parameter on [Reducer] to override that selection.

For a self-reducing read model, place sequence configuration on the model itself. The .NET client applies the same selection and external-store routing rules as it does for a separate reducer. A sequence attribute chooses the input; it does not turn the model into a projection.

Apply [EventSequence] directly to the reducer class when the sequence is the only option you need to set:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersEventSequenceShipmentDispatched(string TrackingNumber, string Carrier);
public record ReducersEventSequenceShipmentSummary(string TrackingNumber, string Carrier, DateTimeOffset DispatchedAt);
[EventSequence("fulfillment-events")]
public class ReducersEventSequenceShipmentSummaryReducer : IReducerFor<ReducersEventSequenceShipmentSummary>
{
public ReducersEventSequenceShipmentSummary Dispatched(ReducersEventSequenceShipmentDispatched @event, ReducersEventSequenceShipmentSummary? current, EventContext context) =>
new(@event.TrackingNumber, @event.Carrier, context.Occurred);
}

View C# snippet source on GitHub

TypeScript uses the eventSequenceId parameter on reducer(). Kotlin and Java use either @EventSequence or the eventSequence parameter on @Reducer. Follow the configuration form in your client tab; the next example shows the parameter-based form.

When you are already using the [Reducer] attribute to set a custom identifier or other options, use its eventSequence parameter instead of adding a separate [EventSequence] attribute:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersEventSequenceReducerAttributeShipmentDispatched(string TrackingNumber, string Carrier);
public record ReducersEventSequenceReducerAttributeShipmentSummary(string TrackingNumber, string Carrier, DateTimeOffset DispatchedAt);
[Reducer(id: "shipment-summary", eventSequence: "fulfillment-events")]
public class ReducersEventSequenceReducerAttributeShipmentSummaryReducer : IReducerFor<ReducersEventSequenceReducerAttributeShipmentSummary>
{
public ReducersEventSequenceReducerAttributeShipmentSummary Dispatched(ReducersEventSequenceReducerAttributeShipmentDispatched @event, ReducersEventSequenceReducerAttributeShipmentSummary? current, EventContext context) =>
new(@event.TrackingNumber, @event.Carrier, context.Occurred);
}

View C# snippet source on GitHub

Either form selects the sequence. If both are present in .NET, [EventSequence] takes precedence. Keep the selection in one place so a later change does not leave a conflicting value behind.

Use [EventLog] when you want to be explicit that the reducer reads from the default event log, even when event types in the assembly carry a [EventStore] attribute pointing elsewhere:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersEventSequenceLogOrderPlaced(Guid OrderId);
public record ReducersEventSequenceLocalOrderSummary(int OrderCount, DateTimeOffset LastOrderAt);
[EventLog]
public class ReducersEventSequenceLocalOrderSummaryReducer : IReducerFor<ReducersEventSequenceLocalOrderSummary>
{
public ReducersEventSequenceLocalOrderSummary Placed(ReducersEventSequenceLogOrderPlaced @event, ReducersEventSequenceLocalOrderSummary? current, EventContext context)
{
var count = current?.OrderCount ?? 0;
return new ReducersEventSequenceLocalOrderSummary(count + 1, context.Occurred);
}
}

View C# snippet source on GitHub

[EventLog] explicitly selects the default log instead of inferring an inbox from event-store metadata on the events. Use the event-log sequence name when configuring this selection through a sequence parameter.

If the events belong to another event store, follow Subscribe Reducers to External Event Stores to let Chronicle forward them from the source outbox to a local inbox. In .NET, [EventStore] on the reducer selects that inbox and cannot be combined with explicit sequence configuration, including [EventLog].

Use an explicit sequence to read from a known sequence instead of the inferred one, or to keep a reducer on the local event log despite external-store metadata on its events. Sequence selection chooses where events come from; it does not narrow delivery by tag, event source type, or stream type. For those rules, continue with reducer event filtering.