Filter Chronicle observers by event source and stream
Use [FromEventSource<TSource>("Stream")] in Cratis Chronicle to restrict a reactor or reducer to a declared stream, such as an account’s transactions. The attribute takes the event source type from the definition and replaces the pair of strings in [EventSourceType] and [EventStreamType].
The examples use a .NET event source definition. Define an event source explains how to declare one.
Filter a reactor
Section titled “Filter a reactor”using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSources;using Cratis.Chronicle.Reactors;
[EventType]public record MoneyDeposited(decimal Amount);
[EventSource("Account")][EventStream("Onboarding")][EventStream("Transactions")]public class AccountEventSource : IEventSource;
// Receives MoneyDeposited only when it was appended to the Transactions stream of the Account event source.[FromEventSource<AccountEventSource>("Transactions")]public class StatementReactor : IReactor{ public Task Deposited(MoneyDeposited @event, EventContext context) => Task.CompletedTask;}The reactor receives MoneyDeposited only when the stored event source type is Account and the stream type is Transactions. A deposit with a different event source type, or in the Onboarding stream, is skipped. The filter does not select one account id or one month’s stream id.
Filter a reducer
Section titled “Filter a reducer”using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSources;using Cratis.Chronicle.Reducers;
public record DepositTotal(decimal Amount);
[FromEventSource<AccountEventSource>("Transactions")]public class DepositTotalReducer : IReducerFor<DepositTotal>{ public DepositTotal Deposited(MoneyDeposited @event, DepositTotal? current, EventContext context) => new((current?.Amount ?? 0m) + @event.Amount);}The reducer uses the same AccountEventSource and MoneyDeposited declarations as the reactor example. Its total includes only deposits that pass the Account and Transactions filters; deposits in Onboarding do not contribute.
How the match works
Section titled “How the match works”The attribute resolves to two values when the observer is registered: the definition’s name as the event source type, and the stream name as the event stream type. An event passes when its stored event source type and event stream type equal them. Chronicle compares the stored values, not the definition type, so an event appended with the same strings without a definition also matches.
If the definition or the stream cannot be resolved when the observer registers, the client throws UnknownEventSource or EventStreamDoesNotBelongToEventSource.
You can combine the typed filter with [FilterEventsByTag]. All configured categories must match. Filters apply to reactors and reducers. A projection ignores them, which CHR0041 reports.
Build-time checks
Section titled “Build-time checks”The code analyzer checks typed filters while you compile:
| Rule | Severity | Reports |
|---|---|---|
| CHR0055 | Error | The stream is not declared by the definition |
| CHR0056 | Error | [EventSourceType] or [EventStreamType] on the same type contradicts the typed filter |
An explicit attribute that repeats the same value as the typed filter is allowed, but it adds nothing.
Filtering by string
Section titled “Filtering by string”[EventSourceType] and [EventStreamType] still work without a definition. See Filter by event source type and Filter by event stream type. To move from the strings to the typed form, read Migrate from string routing.
Next steps
Section titled “Next steps”For a reactor that follows a process until its stream closes, continue with A process stream. The Reactors guide covers event handling. If the rule must reject an append before it is written, use the write-side guards in Dynamic Consistency Boundary; Dynamic Consistency Boundary in Chronicle explains that boundary.