Scope a Cratis Chronicle read model to one event stream
Use a per-stream read model in Cratis Chronicle when a rule depends on one stream instance, such as withdrawals for one account in one month. Each read folds that stream’s events, applying them in order to compute state. Keep the account as the model’s Id and select the month in the read: a whole-account total would mix months, while a model keyed only by month could collide across accounts.
| Your rule needs | Use |
|---|---|
| Folded state in one stream | A per-stream read model |
| The account’s whole history | A whole-source read model |
| Rich behavior across the whole entity | An aggregate root |
| A unique value across sources | A constraint |
| Another stream or source | An explicit stream-scoped decision read |
Declare the model and guard the decision
Section titled “Declare the model and guard the decision”Declare [EventStream<TSource>("Transactions")] from Cratis.Chronicle.ReadModels on the model. TSource is an IEventSource definition that declares that stream. The non-generic [EventStream("Transactions")] belongs on the event source definition, not the read model.
This example limits withdrawals to 10,000 in BankAccount/Transactions/2026-10. It assumes a connected event store with the definition, event and model discovered through the normal client setup, and a positive withdrawal amount.
namespace MonthlyLimitExample{ using Cratis.Chronicle; using Cratis.Chronicle.Events; using Cratis.Chronicle.EventSequences; using Cratis.Chronicle.EventSources; using Cratis.Chronicle.Projections.ModelBound; using Cratis.Chronicle.ReadModels;
public record AccountId(string Value) : EventSourceId(Value);
[EventSource("BankAccount")] [Cratis.Chronicle.EventSources.EventStream("Transactions")] public class BankAccountEventSource : IEventSource;
[EventType] public record MoneyWithdrawn(decimal Amount);
[EventStream<BankAccountEventSource>("Transactions")] public record MonthlyWithdrawals( AccountId Id, [AddFrom<MoneyWithdrawn>(nameof(MoneyWithdrawn.Amount))] decimal Withdrawn);
public record WithdrawalOutcome(bool LimitReached, AppendManyResult? Append);
public static class OctoberWithdrawals { public static async Task<WithdrawalOutcome> Withdraw( IEventStore eventStore, AccountId account, decimal amount) { var definition = eventStore.EventSources.GetFor(typeof(BankAccountEventSource)); EventStreamId month = "2026-10"; var read = await eventStore.GetDecisionReads().GetDetached<MonthlyWithdrawals>( account, "Transactions", month, definition.EventSourceType); if ((read.Instance?.Withdrawn ?? 0m) + amount > 10_000m) { return new(true, null); }
var result = await eventStore.EventLog.AppendMany( [new EventForEventSourceId(account, new MoneyWithdrawn(amount)) { EventSourceType = definition.EventSourceType, EventStreamType = "Transactions", EventStreamId = month }], guardedBy: [read]); return new(false, result); }
public static Task<MonthlyWithdrawals> ForAccount( IEventStore eventStore, AccountId account) => eventStore.ReadModels.GetInstanceById<MonthlyWithdrawals>(account, "2026-10"); }}The attribute makes both model-bound and fluent projections passive, so reads compute state from events rather than a stored account instance that could accumulate several months. MonthlyWithdrawals.Id remains the account ID. The read selects the account, declared event source type, stream type and requested month together.
Withdraw checks the computed total and carries the decision read into the append. A matching withdrawal appended after the read makes the guarded append fail with a concurrency violation and write nothing. Appending a withdrawal in 2026-11 does not affect October’s fold or invalidate its stream guard.
If the method returns LimitReached, no append was attempted. Otherwise, check WithdrawalOutcome.Append.IsSuccess before treating the withdrawal as accepted. After a conflict, read again and re-evaluate the limit rather than resubmitting the old decision.
ForAccount uses the stream-aware IReadModels.GetInstanceById<T>(EventSourceId, EventStreamId) overload. Use this plain read to display the same monthly state without guarding a later append. It returns null when the selected events have not created an instance.
That overload throws DecisionReadRefused with StreamScopeNotSupported when the IReadModels implementation does not support stream scope. The interface’s default implementation always throws it, so a custom or test double of IReadModels must override the overload. The client’s own implementation also throws it when the kernel does not acknowledge stream scope.
Reads must name the stream
Section titled “Reads must name the stream”| Read | Result |
|---|---|
IDecisionReads.Get, GetFor or GetDetached with source, stream type, stream ID and the declaration’s source type |
A decision read scoped to that stream |
IReadModels.GetInstanceById<T>(source, streamId) |
A plain read; stream type and source type come from the declaration |
Key-only decision reads or key-only IReadModels.GetInstanceById |
DecisionReadRefused with ReadModelRequiresStreamScope |
GetInstances, snapshots, Observe, Watch or GetWatcherFor |
Refused; there is no materialized collection or key-only stream identity |
| A decision read with a different stream type or source type, including an omitted source type | StreamScopeDoesNotMatchReadModel |
Reducers, stream-keyed models, undeclared streams and EventStreamType.All are not valid declarations. CHR0064 reports statically visible misuse; registration also rejects invalid declarations. How guards are derived shows the guard such a read produces.
Supply a specific month. EventStreamId.Default does not narrow by stream ID: it folds every ID of the declared stream type. The client permits that explicit choice, but it is not a monthly rule.
Keep every writer on the same route
Section titled “Keep every writer on the same route”The declaration’s event source type is part of both the fold and the guard. An event written to Transactions/2026-10 without the BankAccount source type is neither folded nor guarded. Every writer participating in this rule must use the definition’s route and protect its decision. See event source definitions.
A decision can depend on more than one read. Adding a whole-source read, or a read of another stream for the same source, widens the merged guard to the whole source. Source-level declared concurrency can widen it too. That can produce more conflicts without losing protection; the per-stream declaration does not override the other state the decision depends on.
Stream-scoped reads require a kernel that acknowledges stream scope. An older kernel refuses with StreamScopeNotSupported rather than returning a whole-source fold.
On a kernel that reports a mutation epoch, a sequence-wide marker for changes to existing history, revision or redaction can also invalidate the guard even outside the selected stream. Without that marker, the guard detects appends, not changes to existing events. External data and definition changes remain outside it; see Costs and limits.
Each read processes the selected history again. Keep it short enough for on-demand use, and use materialized paging or collection observation for stored lists rather than treating a per-stream model as a list query.