Skip to content

Getting a Collection of Instances

Sometimes you need the exact current state of every instance of a read model: a small report, an integrity check, a back-office export, or a diagnostic script. Chronicle can rebuild the whole collection by replaying the events that feed that read model.

This is a strong-consistency operation. It is also proportional to event history, so it is not the default tool for hot list views or large data sets.

Use collection replay when the result set is small enough to fit comfortably in memory and the caller needs state that includes every event appended so far.

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

Kotlin currently exposes single-instance read-model lookup only, so collection replay is not shown for that client.

Replay returns every instance. Apply language-native filtering after the read, and keep that in-memory cost in mind.

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.");

For large result sets, query the materialized sink directly or use a product-specific query model instead of replaying everything and filtering afterward.

You can cap the number of events Chronicle processes. This can make diagnostics faster, but it can also return incomplete state if the cap cuts off events that matter.

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

Use an event-count cap for historical analysis, back-testing, or bounded diagnostics. Do not use it when the reader expects the current truth.

Use collection replay when:

  • You need a strongly consistent report over all current instances.
  • The read model population is small.
  • You are running an administrative or diagnostic task.
  • You are validating data derived from the event log.

Prefer materialized or database-native reads when:

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