Skip to content

Cratis Chronicle passive projections: compute state on demand

A passive projection in Cratis Chronicle computes a read model from events when you request it, rather than maintaining a stored result. Use it for source-local lookups when you want to read from event history without waiting for a background observer to update a read-model store.

Call .Passive() on the projection builder. Keep the event mappings you would use for a materialized projection:

using Cratis.Chronicle.Projections;
public class DecPassiveUserSummaryProjection : IProjectionFor<DecPassiveUserSummary>
{
public void Define(IProjectionBuilderFor<DecPassiveUserSummary> builder) => builder
.Passive()
.From<DecPassiveUserCreated>()
.From<DecPassiveUserUpdated>();
}

View C# snippet source on GitHub

The declaration disables background materialization. ReadModels.GetInstanceById instead reconstructs the requested instance from its event history.

A read model declaring [EventStream<TSource>(stream)] is also passive, even without .Passive(). It stays keyed by event source ID but requires a stream-aware instance read; a key-only read is refused rather than mixing stream IDs. See Read models scoped to one event stream.

Pass the event source ID to GetInstanceById through the event store’s ReadModels API:

using Cratis.Chronicle;
public class DecPassiveUserService(IEventStore eventStore)
{
public Task<DecPassiveUserSummary> GetUserSummary(string userId) =>
eventStore.ReadModels.GetInstanceById<DecPassiveUserSummary>(userId);
}

View C# snippet source on GitHub

The read model remains an ordinary record or class. This example holds a user’s summary:

public record DecPassiveUserSummary(
string Name,
string Email,
int LoginCount,
DateTimeOffset LastLoginAt);

View C# snippet source on GitHub

The creation and update events supply the user’s name and email. Matching properties map automatically; use explicit mappings when the names differ:

using Cratis.Chronicle.Events;
[EventType]
public record DecPassiveUserCreated(string Name, string Email);
[EventType]
public record DecPassiveUserUpdated(string Name, string Email);
[EventType]
public record DecPassiveUserLoggedIn(DateTimeOffset LoginTime);

View C# snippet source on GitHub

When you call GetInstanceById on a passive projection:

  1. Chronicle retrieves the subscribed event types for the requested event source ID, starting at the beginning of its history.
  2. It applies the projection mappings in memory and returns the computed instance.
  3. A later read repeats that work from the start rather than reusing the previous read-model state.

There is no persistent read-model sink, meaning no storage destination containing a materialized result. A read can include events you have just successfully appended without waiting for an observer, but it does not reserve that state against another writer. If the result drives a decision that must hold under concurrent writes, use a decision-consistent read.

By-ID reads select one event source. A cross-source join or custom-key aggregation cannot generally reconstruct the same answer as its materialized counterpart this way. See consistency models before making an existing projection passive.

Choose a passive projection when each lookup concerns one event source and replaying its history fits the request’s latency requirements. Infrequent reads over short histories are a good fit: you avoid maintaining a stored result that callers rarely use.

Keep a projection materialized when it joins other sources, aggregates under a custom key, or serves frequent reads over long histories. Passive mode moves the work to read time; it does not remove that work.

Read cost grows with the number of relevant events for the source. A reactor or command that reads a passive model on every event repeats the full fold each time, so measure that path with representative histories.

For the trade-off between computing from events, reading stored state, and waiting for an observer, continue with consistency models.