Skip to content

Cratis Chronicle projections: create a read model from an event

A simple projection in Cratis Chronicle turns an event into a read model you can query. When the event and read model have matching property names and compatible types, AutoMap copies those values without explicit mappings. The event source ID identifies the read-model instance.

In the .NET client, implement IProjectionFor<TReadModel> and subscribe to the event with From<TEvent>() inside Define:

using Cratis.Chronicle.Projections;
public class DecSimpleUserProjection : IProjectionFor<DecSimpleUser>
{
public void Define(IProjectionBuilderFor<DecSimpleUser> builder) => builder
.From<DecSimpleUserCreated>();
}

View C# snippet source on GitHub

The declaration maps the creation event’s Name, Email, and CreatedAt to the user read model. No .AutoMap() call is needed because automatic mapping is enabled at the top level. Child scopes inherit that setting unless they override it; see auto mapping.

If several event types share a contract, From<TEvent>() also accepts a base event type or an interface. It registers every known event type assignable to TEvent, including the base type itself when it is an event type. Each registration uses the same property mappings, key, and parent key. This applies at the root, in a children collection, and in a nested object.

You can add From<ConcreteEvent>() for an exception to the shared mapping. When it overlaps a base or interface registration, their mappings are combined; the concrete registration wins on conflicting properties and supplies the key and parent key, regardless of call order.

Without a concrete registration, the first matching base or interface registration supplies the keys, while later inherited mappings win on conflicting properties. The subscription still covers only assignable event types. To handle unrelated event types too, use FromAll.

Define the shape you want to query as a record or class. Here, the user read model exposes the same three properties as the creation event:

public record DecSimpleUser(string Name, string Email, DateTimeOffset CreatedAt);

View C# snippet source on GitHub

The creation event supplies those values with matching names and compatible types:

using Cratis.Chronicle.Events;
[EventType]
public record DecSimpleUserCreated(string Name, string Email, DateTimeOffset CreatedAt);

View C# snippet source on GitHub

After a creation event is appended, the projection’s observer processes it and creates or updates the user instance identified by the event source ID. AutoMap copies the matching properties into the read model, then the projection writes the result to its configured storage destination, called a sink.

This happens asynchronously: append completion does not mean the stored read model already includes the event. When a caller needs to read back its own append, see waiting for observer completion after append.

If your query needs different property names or values from event metadata, continue with projection with custom properties.