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.
Defining a simple projection
Section titled “Defining a simple projection”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>();}import io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
class DecSimpleUserProjection : IProjectionFor<DecSimpleUser> { override fun define(builder: IProjectionBuilderFor<DecSimpleUser>) { builder.from(DecSimpleUserCreated::class) }}import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
class DecSimpleUserProjection implements IProjectionFor<DecSimpleUser> { @Override public void define(IProjectionBuilderFor<DecSimpleUser> builder) { builder.from(DecSimpleUserCreated.class); }}defmodule MyApp.Projections.DecSimpleUserProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecSimpleUser
alias MyApp.Events.DecSimpleUserCreated
from DecSimpleUserCreatedendimport { IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@projection()class DecSimpleUserProjection implements IProjectionFor<DecSimpleUser> { define(builder: IProjectionBuilderFor<DecSimpleUser>): void { builder.from(DecSimpleUserCreated); }}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.
Base event types in the .NET client
Section titled “Base event types in the .NET client”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.
Read model definition
Section titled “Read model definition”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);import java.time.OffsetDateTime
data class DecSimpleUser( val name: String = "", val email: String = "", val createdAt: OffsetDateTime? = null)class DecSimpleUser { public String name = ""; public String email = ""; public String createdAt = null;}defmodule MyApp.ReadModels.DecSimpleUser do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:name, :email, :created_at]endclass DecSimpleUser { name = ''; email = ''; createdAt = new Date();}Event definition
Section titled “Event definition”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);import io.cratis.chronicle.events.EventTypeimport java.time.OffsetDateTime
@EventTypedata class DecSimpleUserCreated(val name: String, val email: String, val createdAt: OffsetDateTime)import io.cratis.chronicle.events.EventType;
@EventTyperecord DecSimpleUserCreated(String name, String email, String createdAt) {}defmodule MyApp.Events.DecSimpleUserCreated do use Cratis.Chronicle.Events.EventType, id: "dec-simple-user-created"
defstruct [:name, :email, :created_at]endclass DecSimpleUserCreated { name = ''; email = ''; createdAt = new Date();}How it works
Section titled “How it works”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.