Skip to content

Read a Cratis Chronicle model-bound projection on demand

A passive model-bound projection in Cratis Chronicle computes a read model from events when you request it, instead of maintaining a persistent copy as events arrive. Use it for occasional reads where computing state on demand is preferable to keeping a stored view up to date.

In C#, apply [Passive] from Cratis.Chronicle.ReadModels to the read model type:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.ReadModels;
[EventType]
public record MbPassiveSnapshotCreated(string Data);
[Passive]
public record MbPassiveHistoricalSnapshot(
[Key]
Guid Id,
[SetFrom<MbPassiveSnapshotCreated>(nameof(MbPassiveSnapshotCreated.Data))]
string Data);

View C# snippet source on GitHub

The attribute sets IsActive to false on the projection definition. Read MbPassiveHistoricalSnapshot through ReadModels.GetInstanceById, passing the event source ID. Chronicle folds that source’s relevant events in memory: it applies them in order to produce the result. Despite the example’s name, this is an on-demand view of the source, not a saved historical snapshot.

A read model declaring [EventStream<TSource>(stream)] from Cratis.Chronicle.ReadModels is implicitly passive, so [Passive] is unnecessary. It keeps its event source ID as the key but requires a stream-scoped read. See Read models scoped to one event stream.

Passive projections suit source-local state that you need only occasionally. A read starts from the beginning of the relevant history, and later reads compute the state again. There is no persistent read-model sink or cached result between reads, so read cost grows with the history being processed.

A by-ID read selects one event source. It cannot generally reproduce a stored projection that joins events across sources or aggregates under a custom key. Check Read-model consistency before changing an existing projection to passive.

Keep the history small enough to process on each read. If a frequently used query would repeatedly fold a long history, a materialized projection, which stores state as events arrive, may be a better fit when eventual consistency is acceptable.

No separate rebuild trigger is needed for an ordinary passive read: requesting the instance computes it. For an example of the read call and more detail on read-time cost, continue with Using passive projections.