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);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.Passiveimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbPassiveSnapshotCreated(val data: String)
@Passive@ReadModel@FromEvent(MbPassiveSnapshotCreated::class)data class MbPassiveHistoricalSnapshot( @SetFrom("data", MbPassiveSnapshotCreated::class) val data: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.Passive;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbPassiveSnapshotCreated(String data) {}
@Passive@ReadModel@FromEvent(eventType = MbPassiveSnapshotCreated.class)class MbPassiveHistoricalSnapshot { @SetFrom(propertyPath = "data", eventType = MbPassiveSnapshotCreated.class) public String data = "";}defmodule MyApp.Events.PassiveSnapshotCreated do use Cratis.Chronicle.Events.EventType, id: "passive-snapshot-created-v1"
defstruct [:data]end
defmodule MyApp.ReadModels.PassiveHistoricalSnapshot do use Cratis.Chronicle.ReadModels.ReadModel, passive: true
alias MyApp.Events.PassiveSnapshotCreated
defstruct [:id, :data]
from PassiveSnapshotCreated, set: [id: :event_source_id, data: :data]endimport { eventType, Guid, passive, setFrom } from '@cratis/chronicle';
@eventType()export class MbPassiveSnapshotCreated { data = '';}
@passiveexport class MbPassiveHistoricalSnapshot { id: Guid = Guid.empty;
@setFrom(MbPassiveSnapshotCreated, 'data') data = '';}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.
When to use
Section titled “When to use”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.
Best practices
Section titled “Best practices”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.