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.
Using the [EventSequence] Attribute
Section titled “Using the [EventSequence] Attribute”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);}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.EventSequenceimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersEventSequenceRefundIssued(val amount: Double)
@ReadModeldata class ReducersEventSequenceRefundSummary(val refunded: Double = 0.0)
@Reducer@EventSequence("outbox")class ReducersEventSequenceRefundSummaryReducer { fun issued( event: ReducersEventSequenceRefundIssued, current: ReducersEventSequenceRefundSummary? ): ReducersEventSequenceRefundSummary = ReducersEventSequenceRefundSummary((current?.refunded ?: 0.0) + event.amount)}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.EventSequence;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersEventSequenceRefundIssued(double amount) {}
@ReadModelrecord ReducersEventSequenceRefundSummary(double refunded) { ReducersEventSequenceRefundSummary() { this(0.0); }}
@Reducer@EventSequence("outbox")class ReducersEventSequenceRefundSummaryReducer { ReducersEventSequenceRefundSummary issued( ReducersEventSequenceRefundIssued event, ReducersEventSequenceRefundSummary current) { double refunded = current == null ? 0.0 : current.refunded(); return new ReducersEventSequenceRefundSummary(refunded + event.amount()); }}Elixir does not support this workflow yet.import { eventType, reducer } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class EventSequenceShipmentDispatched { @field(String) readonly trackingNumber: string;
constructor(trackingNumber: string) { this.trackingNumber = trackingNumber; }}
class EventSequenceShipmentStatus { trackingNumber = '';}
@reducer('', 'fulfillment-events', EventSequenceShipmentStatus)class EventSequenceShipmentReducer { eventSequenceShipmentDispatched( event: EventSequenceShipmentDispatched, current: EventSequenceShipmentStatus | undefined ): EventSequenceShipmentStatus { return { trackingNumber: event.trackingNumber }; }}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.
Using the [Reducer] Attribute
Section titled “Using the [Reducer] Attribute”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);}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersEventSequencePaymentSettled(val amount: Double)
@ReadModeldata class ReducersEventSequencePaymentSummary(val settled: Double = 0.0)
@Reducer(id = "payment-summary", eventSequence = "outbox")class ReducersEventSequencePaymentSummaryReducer { fun settled( event: ReducersEventSequencePaymentSettled, current: ReducersEventSequencePaymentSummary? ): ReducersEventSequencePaymentSummary = ReducersEventSequencePaymentSummary((current?.settled ?: 0.0) + event.amount)}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersEventSequencePaymentSettled(double amount) {}
@ReadModelrecord ReducersEventSequencePaymentSummary(double settled) { ReducersEventSequencePaymentSummary() { this(0.0); }}
@Reducer(id = "payment-summary", eventSequence = "outbox")class ReducersEventSequencePaymentSummaryReducer { ReducersEventSequencePaymentSummary settled( ReducersEventSequencePaymentSettled event, ReducersEventSequencePaymentSummary current) { double settled = current == null ? 0.0 : current.settled(); return new ReducersEventSequencePaymentSummary(settled + event.amount()); }}Elixir does not support this workflow yet.import { eventType, reducer } from '@cratis/chronicle';
@eventType()class ReducersEventSequenceReducerAttributeShipmentDispatched { trackingNumber = ''; carrier = '';}
class ReducersEventSequenceReducerAttributeShipmentSummary { trackingNumber = ''; carrier = ''; dispatchedAt: Date = new Date(0);}
@reducer('shipment-summary', 'fulfillment-events', ReducersEventSequenceReducerAttributeShipmentSummary)class ReducersEventSequenceReducerAttributeShipmentSummaryReducer { // Without @handles(EventType), the method name must be the exact camelCase of the event's class name reducersEventSequenceReducerAttributeShipmentDispatched( event: ReducersEventSequenceReducerAttributeShipmentDispatched, _current: ReducersEventSequenceReducerAttributeShipmentSummary | undefined ): ReducersEventSequenceReducerAttributeShipmentSummary { return { trackingNumber: event.trackingNumber, carrier: event.carrier, dispatchedAt: new Date() }; }}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.
Convenience: [EventLog]
Section titled “Convenience: [EventLog]”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); }}Kotlin does not support this workflow yet.Java does not support this workflow yet.Elixir does not support this workflow yet.import { EventContext, eventType, Guid, reducer } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class EventSequenceLogOrderPlaced { @field(Guid) readonly orderId: Guid;
constructor(orderId: Guid) { this.orderId = orderId; }}
class EventSequenceLogOrderAudit { orderId: Guid = Guid.empty;}
// No eventSequenceId given - observes the default event log@reducer('', undefined, EventSequenceLogOrderAudit)class EventSequenceLocalAuditReducer { eventSequenceLogOrderPlaced( event: EventSequenceLogOrderPlaced, current: EventSequenceLogOrderAudit | undefined, context: EventContext ): EventSequenceLogOrderAudit { return { orderId: event.orderId }; }}[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.
External Event Store Inbox Routing
Section titled “External Event Store Inbox Routing”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].
When to Use [EventSequence] Explicitly
Section titled “When to Use [EventSequence] Explicitly”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.