Skip to content

Cratis Chronicle model-bound projections: Map matching properties

Use convention-based mapping in Cratis Chronicle when an event and a read model share property names and compatible shapes. Declare the source event on the read model, and Chronicle copies the matching properties without a set mapping on each one. This is the model-bound form of AutoMap.

Add explicit mappings for exceptions: a different source name, a value from event context, or an operation such as adding an amount instead of replacing a value.

Chronicle matches event properties to read model properties by name and compatible type. Nested objects can map when their internal names match; collections can map when their element shapes are compatible. A read model property absent from the event is left unchanged for that event.

Client syntax differs, but each client produces a projection definition containing the event type, the key that selects a read model instance, and the automatic mapping behavior.

Declare the source event at the read model level, using the syntax for your client. Matching properties then map without per-property set declarations:

Convention-based mapping
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ConventionUserRegistered(
string Name,
string Email,
DateTimeOffset RegisteredAt);
[FromEvent<ConventionUserRegistered>]
public record ConventionUser(
[Key] Guid Id,
string Name,
string Email,
DateTimeOffset RegisteredAt);

View C# snippet source on GitHub

For these matching properties, the result is the same as declaring each set mapping explicitly:

Equivalent explicit mappings
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ExplicitConventionUserRegistered(
string Name,
string Email,
DateTimeOffset RegisteredAt);
public record ExplicitConventionUser(
[Key] Guid Id,
[SetFrom<ExplicitConventionUserRegistered>(nameof(ExplicitConventionUserRegistered.Name))]
string Name,
[SetFrom<ExplicitConventionUserRegistered>(nameof(ExplicitConventionUserRegistered.Email))]
string Email,
[SetFrom<ExplicitConventionUserRegistered>(nameof(ExplicitConventionUserRegistered.RegisteredAt))]
DateTimeOffset RegisteredAt);

View C# snippet source on GitHub

Declare more than one source event when several event types contribute to a read model. Each event updates the matching properties it contains and leaves the rest unchanged. If two events both contain a matching property, either can replace its value.

Multiple convention events
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ConventionUserProfileCreated(string Name, string Email);
[EventType]
public record ConventionUserProfileUpdated(string Name, string Email, string Phone);
[FromEvent<ConventionUserProfileCreated>]
[FromEvent<ConventionUserProfileUpdated>]
public record ConventionUserProfile(
[Key] Guid Id,
string Name,
string Email,
string Phone);

View C# snippet source on GitHub

By default, convention-based mappings use the event source id as the read model key. Use a custom key when the event carries the identifier of the read model instance in its content.

Custom key
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ConventionUserRegisteredWithKey(
Guid UserId,
string Name,
string Email);
[FromEvent<ConventionUserRegisteredWithKey>(key: nameof(ConventionUserRegisteredWithKey.UserId))]
public record ConventionUserById(
[Key] Guid Id,
string Name,
string Email);

View C# snippet source on GitHub

The custom key changes which instance receives the event; it does not change how property names match.

Convention-based model-bound mapping and declarative AutoMap describe the same mapping behavior through different APIs.

Model-bound and declarative AutoMap
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ConventionEquivalentUserRegistered(string Name, string Email);
[FromEvent<ConventionEquivalentUserRegistered>]
public record ConventionEquivalentUser(
[Key] Guid Id,
string Name,
string Email);
public class ConventionEquivalentProjection : IProjectionFor<ConventionEquivalentUser>
{
public void Define(IProjectionBuilderFor<ConventionEquivalentUser> builder) =>
builder.From<ConventionEquivalentUserRegistered>();
}

View C# snippet source on GitHub

Keep the model-bound form when the mapping is easiest to understand beside the read model’s properties. Use a declarative projection when its builder operations fit the mapping better or the definition should stay separate from the read model type.

Matching names and shapes can also map nested values and collections. Check the members inside the nested value or collection element, not just the outer property name:

Matching nested structures and collections
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
public record ConventionAddress(string Street, string City, string PostalCode);
public record ConventionLineItem(string ProductName, decimal UnitPrice, int Quantity);
[EventType]
public record ConventionCustomerRegistered(
string FirstName,
string LastName,
ConventionAddress BillingAddress,
ConventionAddress ShippingAddress);
[EventType]
public record ConventionOrderCreated(
string CustomerEmail,
ConventionLineItem[] Items,
string[] Tags);
[FromEvent<ConventionCustomerRegistered>]
public record ConventionCustomer(
[Key] Guid Id,
string FirstName,
string LastName,
ConventionAddress BillingAddress,
ConventionAddress ShippingAddress);
[FromEvent<ConventionOrderCreated>]
public record ConventionOrder(
[Key] Guid Id,
string CustomerEmail,
ConventionLineItem[] Items,
string[] Tags);

View C# snippet source on GitHub

An event can supply only part of the read model. Here, each event updates its matching properties without clearing values supplied by the other events:

Partial event shapes
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ConventionPartialUserRegistered(string Email);
[EventType]
public record ConventionPartialUserCompleted(
string FirstName,
string LastName,
string Phone);
[FromEvent<ConventionPartialUserRegistered>]
[FromEvent<ConventionPartialUserCompleted>]
public record ConventionPartialUser(
[Key] Guid Id,
string Email,
string FirstName,
string LastName,
string Phone);

View C# snippet source on GitHub

An event subscribed through a property mapping or join can also carry properties that match by convention. An explicit mapping from one event does not, by itself, stop another event from overwriting the same read model property by name.

Events used only for aggregation are excluded automatically. For other collisions, exclude the affected property with [NoAutoMap].

When every mapping for an event is an aggregate operation, such as count, increment, decrement, add, or subtract, Chronicle does not copy that event’s other properties by convention. Counting an event therefore cannot, on its own, overwrite a name or description with an unrelated same-named field. No exclusion attribute is needed.

Aggregating an event does not map its other properties
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record AggOnlyArrangementSet(string Location);
[EventType]
public record AggOnlyCandidateSubmitted(string Name, string Location);
[FromEvent<AggOnlyArrangementSet>]
public record AggOnlyAssignmentSummary(
[Key]
Guid Id,
// AggOnlyCandidateSubmitted is subscribed only to be counted, so its identically named
// Location is not auto-mapped over the value sourced from AggOnlyArrangementSet.
[SetFrom<AggOnlyArrangementSet>(nameof(AggOnlyArrangementSet.Location))]
string Location,
[Count<AggOnlyCandidateSubmitted>]
int CandidateCount);

View C# snippet source on GitHub

For a collision involving a value-mapped ([SetFrom]) or joined ([Join]) event, apply [NoAutoMap] to the read model property or record parameter. Only explicit mappings can then change that property; the other properties keep mapping by convention.

Exclude a single property from convention mapping
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record NoAutoMapWorkArrangementSet(string Location, int WorkMode);
[EventType]
public record NoAutoMapCandidateSubmitted(string Name, string Location);
[FromEvent<NoAutoMapWorkArrangementSet>]
public record NoAutoMapAssignmentSummary(
[Key]
Guid Id,
// Location is sourced only from NoAutoMapWorkArrangementSet. NoAutoMapCandidateSubmitted is
// value-mapped (for CandidateName) and also carries a Location; [NoAutoMap] stops that Location
// from being auto-mapped over the explicit value, while every other property keeps mapping.
[SetFrom<NoAutoMapWorkArrangementSet>(nameof(NoAutoMapWorkArrangementSet.Location))]
[NoAutoMap]
string Location,
[SetFrom<NoAutoMapCandidateSubmitted>(nameof(NoAutoMapCandidateSubmitted.Name))]
string CandidateName);

View C# snippet source on GitHub

[NoAutoMap] also works at the read model (class) level to disable convention mapping for the whole read model. Use the property form when you only need to protect a single property and want convention mapping everywhere else.

Start with convention mapping when most properties are direct copies and names are consistent. Add explicit mappings where the source or operation differs, and use a property-level exclusion when a value must only come from its declared source.

The client builds the projection definition from the model metadata; Chronicle processes events using that definition rather than reflecting over your model attributes for every event. For the explicit operations, continue with basic property mapping.