Skip to content

Read a collection of Cratis Chronicle read models

Read a whole collection in Cratis Chronicle when a small report or diagnostic task needs every instance of a read model. Without an event-count limit, the read returns stored instances for a materialized model and computes passive models from events.

The caller receives the entire result, so use materialized paging for list views or large data sets. A small number of instances can still have a long event history; allow for replay work as well as the size of the returned collection.

var accounts = await eventStore.ReadModels.GetInstances<Account>();
foreach (var account in accounts)
{
Console.WriteLine($"{account.Name}: {account.Balance:C}");
}

View C# snippet source on GitHub

For a materialized model, the kernel pages through the sink, the storage destination updated by its observer, and releases compliance-protected values before returning the stored instances. The result reflects observer progress; reading all instances does not wait for the observer to catch up.

For a passive model, the read processes the events that feed it and returns the computed instances. The work grows with the relevant event history. This is a collection read, not a guard for a later append; use decision-consistent reads when a command depends on state remaining unchanged.

A per-stream read model cannot be read as a whole collection. Read one source and stream instance explicitly instead.

Filtering after the read does not reduce the events processed or the instances returned to the client. Use language-native filtering when the complete collection is small enough to load first.

var accounts = await eventStore.ReadModels.GetInstances<Account>();
var highValueAccounts = accounts
.Where(account => account.Balance > threshold)
.OrderByDescending(account => account.Balance)
.ToList();
Console.WriteLine($"Found {highValueAccounts.Count} high-value accounts.");

View C# snippet source on GitHub

For large result sets, page the materialized read model or use a product-specific query model instead of reading everything and filtering afterward.

Pass an event-count cap to process a limited number of matching events from the beginning of the history. This requests replay even for a materialized model; it does not limit the number of stored instances returned.

var orders = await eventStore.ReadModels.GetInstances<Order>(eventCount: 1_000);
Console.WriteLine($"Replayed {orders.Count()} orders from the capped history.");

View C# snippet source on GitHub

A cap bounds replay work, but later events may change or remove the instances it returns. Use it to inspect a prefix of history for diagnostics or back-testing, not when the reader expects current state. For historical state of one key, continue with Getting Snapshots.

Read the whole collection when:

  • The read model population is small.
  • You are running an administrative or diagnostic task.
  • You are validating data derived from the event log.

Prefer paging or a database-native read when:

  • The page is user-facing and called frequently.
  • You need server-side filtering, sorting, or paging.
  • The read model has many instances.