Skip to content

Cratis Chronicle AutoMap: map matching event properties

AutoMap in Cratis Chronicle maps event properties to read model properties with matching names. It is enabled by default in declarative projections, so you can declare the events a read model needs without repeating each property assignment.

Use explicit mappings where names differ or a value comes from somewhere other than the event payload. Keep event and read model property types compatible; AutoMap is a naming convention, not a value transformation.

Declare each event with From. In this example, the creation event supplies Name and Email; the rename event changes only Name.

AutoMap by convention
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AutoMapUserCreated(string Name, string Email);
[EventType]
public record AutoMapUserRenamed(string Name);
public record AutoMapUser(string Name, string Email);
public class AutoMapUserProjection : IProjectionFor<AutoMapUser>
{
public void Define(IProjectionBuilderFor<AutoMapUser> builder) => builder
.From<AutoMapUserCreated>()
.From<AutoMapUserRenamed>();
}

View C# snippet source on GitHub

For events that supply read model values, AutoMap uses these rules:

Rule Behavior
Property names Chronicle compares serialized schema property names without regard to case. The client’s naming policy determines those serialized names.
Property types Choose event values compatible with the target property type.
Explicit mappings An explicit mapping takes precedence over AutoMap for the same target property in that event mapping.
Scope Child scopes inherit the parent’s setting unless they override it.

The kernel resolves matching properties when it constructs the projection’s mappings from the schemas, rather than discovering them through reflection for each event.

Disable AutoMap when a projection should map only the properties you name explicitly.

Disable AutoMap
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AutoMapDisabledAccountRegistered(string AccountName, string ContactEmail);
public record AutoMapDisabledAccount(
string Name,
string Email,
DateTimeOffset CreatedAt);
public class AutoMapDisabledAccountProjection : IProjectionFor<AutoMapDisabledAccount>
{
public void Define(IProjectionBuilderFor<AutoMapDisabledAccount> builder) => builder
.NoAutoMap()
.From<AutoMapDisabledAccountRegistered>(_ => _
.Set(m => m.Name).To(e => e.AccountName)
.Set(m => m.Email).To(e => e.ContactEmail)
.Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

At the top level, AutoMap() is already the default. Call it in a child scope when the parent uses NoAutoMap() but the child’s properties should still map by name.

Re-enable AutoMap for children
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AutoMapTeamFormed(string TeamName);
[EventType]
public record AutoMapMemberJoinedTeam(string MemberId, string DisplayName);
public record AutoMapTeamMember(string MemberId, string DisplayName);
public record AutoMapTeam(
string Name,
DateTimeOffset CreatedAt,
IEnumerable<AutoMapTeamMember> Members);
public class AutoMapTeamProjection : IProjectionFor<AutoMapTeam>
{
public void Define(IProjectionBuilderFor<AutoMapTeam> builder) => builder
.NoAutoMap()
.From<AutoMapTeamFormed>(_ => _
.Set(m => m.Name).To(e => e.TeamName)
.Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred))
.Children(m => m.Members, children => children
.IdentifiedBy(m => m.MemberId)
.AutoMap()
.From<AutoMapMemberJoinedTeam>(_ => _
.UsingKey(e => e.MemberId)));
}

View C# snippet source on GitHub

Leave AutoMap enabled for matching properties and add explicit mappings for the exceptions. Here, Name and Email come from event properties, while Status is a constant and CreatedAt comes from event context.

AutoMap with explicit mappings
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AutoMapAccountOpened(string Name, string Email);
[EventType]
public record AutoMapAccountEmailChanged(string Email);
public record AutoMapAccount(
string Name,
string Email,
string Status,
DateTimeOffset CreatedAt);
public class AutoMapAccountProjection : IProjectionFor<AutoMapAccount>
{
public void Define(IProjectionBuilderFor<AutoMapAccount> builder) => builder
.From<AutoMapAccountOpened>(_ => _
.Set(m => m.Status).ToValue("Active")
.Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred))
.From<AutoMapAccountEmailChanged>();
}

View C# snippet source on GitHub

AutoMap also applies to joined events. The join condition finds the read model to update; AutoMap supplies matching values such as DepartmentName.

AutoMap with a join
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AutoMapEmployeeHired(string EmployeeName, string DepartmentId);
[EventType]
public record AutoMapDepartmentRenamed(string DepartmentName);
public record AutoMapEmployee(
string EmployeeName,
string DepartmentId,
string DepartmentName);
public class AutoMapEmployeeProjection : IProjectionFor<AutoMapEmployee>
{
public void Define(IProjectionBuilderFor<AutoMapEmployee> builder) => builder
.From<AutoMapEmployeeHired>()
.Join<AutoMapDepartmentRenamed>(_ => _
.On(m => m.DepartmentId));
}

View C# snippet source on GitHub

When an event’s mappings consist only of Count, Increment, Decrement, Add, or Subtract, AutoMap does not copy its other properties. In this example, counting submitted candidates leaves the arrangement’s Location unchanged, even though the candidate event also has a Location property. You do not need to disable AutoMap for this behavior.

Aggregating an event does not map its other properties
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DeclAggArrangementSet(string Location);
[EventType]
public record DeclAggCandidateSubmitted(string Name, string Location);
public record DeclAggAssignmentSummary(string Location, int CandidateCount);
public class DeclAggAssignmentProjection : IProjectionFor<DeclAggAssignmentSummary>
{
public void Define(IProjectionBuilderFor<DeclAggAssignmentSummary> builder) => builder
.From<DeclAggArrangementSet>()
.From<DeclAggCandidateSubmitted>(_ => _
.Count(m => m.CandidateCount));
}

View C# snippet source on GitHub

Use AutoMap for properties that should retain the event’s name and value. Add explicit mappings for renamed properties, constants, event context, and calculations. If you need every assignment to be visible in the projection definition, disable AutoMap and map each property yourself.

Continue with set properties for the available value sources.

To apply these mappings inside a larger read model, choose the shape you need: