Skip to content

Cratis Chronicle: query and watch read models

Cratis Chronicle lets you retrieve a read model by key, load a collection, or subscribe to live changes. Use materialized state, stored by a projection or reducer, for lists that can tolerate a delay after an append. Use a passive read model when the read must compute state from the event log rather than wait for that stored state to catch up.

The C# examples use IReadModels through IEventStore.ReadModels. Start with a registered read model and the projection or reducer that builds it. If you still need to choose that shape, read Designing read models first.

GetInstanceById returns the instance for one key. By default it reads materialized state from the sink, the storage used by the projection or reducer. That read is eventually consistent: the model may not yet include a recent append. Marking the read model [Passive] makes the lookup compute state from the key’s events on demand instead:

using Cratis.Chronicle;
using Cratis.Chronicle.Events;
public record ScenariosQueryBook(string Title, bool OnLoan);
public class ScenariosQueryBookService(IEventStore eventStore)
{
public Task<ScenariosQueryBook> GetBook(EventSourceId bookId) =>
eventStore.ReadModels.GetInstanceById<ScenariosQueryBook>(bookId.Value);
}

View C# snippet source on GitHub

ReadModelKey converts implicitly from string, Guid, and EventSourceId, so you can pass the id you already have.

GetInstances returns every instance in one call. It reads the sink for a materialized model and computes from the event log for a passive one. Keep this for small collections: filtering the returned set with LINQ still loads the whole set first.

using Cratis.Chronicle;
public class ScenariosQueryOnLoanBooks(IEventStore eventStore)
{
public async Task<IEnumerable<ScenariosQueryBook>> GetOnLoan()
{
var books = await eventStore.ReadModels.GetInstances<ScenariosQueryBook>();
return books.Where(b => b.OnLoan);
}
}

View C# snippet source on GitHub

For a catalog or grid, use Materialized to fetch only the page you need from stored state. This avoids replaying event history for the read, but does not remove the delay between appending an event and the projection updating its sink:

using Cratis.Chronicle;
public class ScenariosQueryBookPageService(IEventStore eventStore)
{
public Task<IEnumerable<ScenariosQueryBook>> GetPage() =>
eventStore.ReadModels.Materialized.GetInstances<ScenariosQueryBook>(skip: 0, take: 20);
}

View C# snippet source on GitHub

Use Watch<T>() when you need individual changes rather than a replacement page. It returns an IObservable<ReadModelChangeset<T>>; each changeset identifies the key, carries the new state, and reports whether the instance was removed:

using Cratis.Chronicle;
public class ScenariosQueryBookWatcher(IEventStore eventStore)
{
public IDisposable Watch() =>
eventStore.ReadModels.Watch<ScenariosQueryBook>()
.Subscribe(changeset =>
{
if (changeset.Removed || changeset.ReadModel is null)
{
return;
}
Console.WriteLine($"{changeset.ModelKey}: on loan = {changeset.ReadModel.OnLoan}");
});
}

View C# snippet source on GitHub

Dispose the subscription when the consumer stops watching. For a projection-backed model, use GetWatcherFor<Book>() when you need to await the watcher’s Subscribed signal before producing events.

Use Materialized.ObserveInstances for a table that should replace its current page as stored data changes. It pushes page snapshots, so the consumer does not need to poll:

using Cratis.Chronicle;
public class ScenariosQueryLiveBookPage(IEventStore eventStore)
{
public IDisposable Subscribe(Action<IEnumerable<ScenariosQueryBook>> updateView) =>
eventStore.ReadModels.Materialized
.ObserveInstances<ScenariosQueryBook>(take: 50)
.Subscribe(books => updateView(books));
}

View C# snippet source on GitHub

After a command appends BookBorrowed, the projection updates the materialized Book and the subscription can receive a new page. The gap between append and push is the eventual consistency window; do not treat subscribing as a guarantee that a just-appended event is already visible.

To serve a live page to a frontend, Cratis Arc can expose observable queries with generated TypeScript proxies. Displaying data shows how to connect those queries to a table.

You need Use Consistency
One instance GetInstanceById<T>(key) Eventual by default; strong for [Passive] models
A small collection GetInstances<T>() Eventual by default; strong for [Passive] models
A page for a list or grid Materialized.GetInstances<T>(skip, take) Eventual
To react to every change of a type Watch<T>() Pushed as changes apply
A live-updating page Materialized.ObserveInstances<T>(skip, take) Eventual, pushed

If the read feeds a business decision, use Consistency models to choose between passive and materialized state. Getting a single instance and Getting a collection of instances cover caching and non-generic forms.

For a larger list, continue with Materialized read models. For a long-lived subscription, Watching read models covers changesets, filtering and error handling.