Skip to content

Cratis Chronicle projections: map properties with Set

Use Set() in a Cratis Chronicle projection when a read-model property needs a different source than its matching event property. You can select an event property, a constant, or event metadata while leaving automatic mapping enabled for the rest.

Choose the read-model property with Set(), then its source with To(). In this account example, AccountNumber comes from Number, and CustomerName comes from the nested event property Owner.Name:

using Cratis.Chronicle.Projections;
public class DecSetPropsAccountProjection : IProjectionFor<DecSetPropsAccount>
{
public void Define(IProjectionBuilderFor<DecSetPropsAccount> builder) => builder
.From<DecSetPropsAccountOpened>(_ => _
.Set(m => m.AccountNumber).To(e => e.Number)
.Set(m => m.CustomerName).To(e => e.Owner.Name)
.Set(m => m.Balance).ToValue(42.0m)
.Set(m => m.IsActive).ToValue(true)
.Set(m => m.OpenedAt).To(e => e.Timestamp))
.From<DecSetPropsMoneyDeposited>(_ => _
.Set(m => m.Balance).To(e => e.Amount)
.Set(m => m.LastTransaction).To(e => e.Timestamp));
}

View C# snippet source on GitHub

AutoMap, Chronicle’s automatic property mapping, is enabled by default at the projection root. You do not need to call .AutoMap() there. Add explicit mappings for exceptions; they take precedence over automatic mappings for the same target property:

public class DecSetPropsCombinedAccountProjection : IProjectionFor<DecSetPropsAccount>
{
public void Define(IProjectionBuilderFor<DecSetPropsAccount> builder) => builder
.From<DecSetPropsAccountOpened>(_ => _
.Set(m => m.CustomerName).To(e => e.Owner.Name) // Custom mapping for nested property
.Set(m => m.IsActive).ToValue(true)) // Custom mapping for constant
.From<DecSetPropsMoneyDeposited>(); // Uses AutoMap for all properties
}

View C# snippet source on GitHub

AutoMap copies matching properties with compatible types, including nested structures and collections. It does not infer that Amount means Balance or that Timestamp means LastTransaction, so the parameterless deposit subscription above does not map those values to those targets. Use the explicit mappings from the first example for that behavior.

NoAutoMap() disables automatic mapping at the scope where it is called. Child scopes inherit the parent’s setting unless they override it with .AutoMap() or NoAutoMap(). There is no per-event-type toggle; put event-specific .Set() calls inside that event’s .From<TEvent>() block. See auto mapping for the matching rules.

The read model uses names suited to the query rather than copying the event’s structure. Each selected value must still be compatible with its target property:

public record DecSetPropsAccount(
string AccountNumber,
string CustomerName,
decimal Balance,
bool IsActive,
DateTimeOffset OpenedAt,
DateTimeOffset? LastTransaction);

View C# snippet source on GitHub

The account-opened event nests customer details under Owner, while the deposit event supplies an amount and timestamp:

using Cratis.Chronicle.Events;
[EventType]
public record DecSetPropsAccountOpened(
string Number,
DecSetPropsCustomer Owner,
DateTimeOffset Timestamp);
[EventType]
public record DecSetPropsMoneyDeposited(
decimal Amount,
DateTimeOffset Timestamp);
public record DecSetPropsCustomer(string Name, string Email);

View C# snippet source on GitHub

Choose the source that represents the fact you want to store. Event context is the metadata accompanying an event, such as its occurrence time.

Source Mapping
Event property .Set(m => m.CustomerName).To(e => e.Owner.Name)
Constant value .Set(m => m.IsActive).ToValue(true)
Event context .Set(m => m.OpenedAt).ToEventContextProperty(c => c.Occurred)
Event source ID .Set(m => m.Id).ToEventSourceId()

A set-once mapping assigns its value only while the target property has no value - it is missing or null. Once the property holds a value, later events leave it unchanged and produce no change for the read model. Use it for facts that belong to the first event that supplies them, such as who created something or when it was first seen.

In the next example every revision updates Title and LastRevised, while OriginalTitle, CreatedBy and Created keep what the first revision supplied:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecSetOnceDocumentRevised(string Title);
public record DecSetOnceDocument(string OriginalTitle, string Title, string CreatedBy, DateTimeOffset Created, DateTimeOffset LastRevised);
public class DecSetOnceDocumentProjection : IProjectionFor<DecSetOnceDocument>
{
public void Define(IProjectionBuilderFor<DecSetOnceDocument> builder) => builder
.From<DecSetOnceDocumentRevised>(_ => _
.SetOnce(m => m.OriginalTitle).To(e => e.Title)
.Set(m => m.Title).To(e => e.Title)
.SetOnce(m => m.CreatedBy).ToEventContextProperty(c => c.CausedBy.Subject)
.SetOnce(m => m.Created).ToEventContextProperty(c => c.Occurred)
.Set(m => m.LastRevised).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

The source can be anything a normal mapping can read: an event property, a constant, the event source ID, event context such as Occurred, or the identity that caused the event. Set once works on root properties, children, nested objects and dictionary entries, including dictionaries keyed by event content.

The decision depends only on the read model state built from earlier events, so a replay assigns the same value. A property that is cleared back to null can be set once more by a later event.

Each From<TEvent>() block defines the changes for that event type. In the first example, opening the account sets its number, customer name, balance, active flag, and opening time. A deposit replaces Balance with the event’s Amount and updates LastTransaction; properties not mapped by that event retain their previous values.

Set() assigns a value; it does not add a deposit to the existing balance. For accumulation, use the operations described in projection functions.