Skip to content

Set constant values in Cratis Chronicle model-bound projections

SetValue assigns a fixed value to a property when a Cratis Chronicle model-bound projection handles an event of the specified type. Use it when the event itself determines the value, such as an order-canceled event setting an order’s status to "canceled", even if the payload has no status property.

In C#, apply SetValue<TEvent>(value) to the property that should change:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record MbSetValueOrderPlaced(string CustomerName);
[EventType]
public record MbSetValueOrderCanceled;
public record MbSetValueOrder(
[Key]
Guid Id,
[SetFrom<MbSetValueOrderPlaced>(nameof(MbSetValueOrderPlaced.CustomerName))]
string CustomerName,
[SetValue<MbSetValueOrderPlaced>("active")]
[SetValue<MbSetValueOrderCanceled>("canceled")]
string Status);

View C# snippet source on GitHub

The two attributes on Status select a value by event type. The order-placed event writes "active"; the order-canceled event writes "canceled". Neither value needs to be stored in the event payload.

The attribute value must be valid as a compile-time attribute argument. You can use strings, integers, longs, doubles, booleans, and enum values:

[EventType]
public record MbSetValueThingHappened;
public record MbSetValueThing(
[Key]
Guid Id,
[SetValue<MbSetValueThingHappened>("pending")]
string StatusLabel,
[SetValue<MbSetValueThingHappened>(42)]
int Priority,
[SetValue<MbSetValueThingHappened>(true)]
bool IsActive,
[SetValue<MbSetValueThingHappened>(3.14)]
double Score);

View C# snippet source on GitHub

The example assigns a string, an integer, a boolean, and a double from one event. Choose a value that matches the target property’s type.

Apply the attribute more than once when several event types set the same property:

[EventType]
public record MbSetValueSubscriptionStarted;
[EventType]
public record MbSetValueSubscriptionPaused;
[EventType]
public record MbSetValueSubscriptionCanceled;
public record MbSetValueSubscription(
[Key]
Guid Id,
[SetValue<MbSetValueSubscriptionStarted>("active")]
[SetValue<MbSetValueSubscriptionPaused>("paused")]
[SetValue<MbSetValueSubscriptionCanceled>("canceled")]
string State);

View C# snippet source on GitHub

The subscription example sets State to "active", "paused", or "canceled". Each event writes its declared value whenever it is processed.

Passing null clears the member every time the event occurs. Use ClearWith when you want the declaration to state that intent directly; it has the same clearing behavior. The member must be nullable. A non-nullable member raises CHR0048, and Chronicle refuses to build the projection because a type default is not the same as an absent value.

For nullable-member examples and the fluent Clear equivalent, see Clearing Values.

Attribute Source When to use
SetValue<TEvent>(value) Compile-time constant The value does not come from the event payload
SetFrom<TEvent>(property) Event property The value comes from a property on the event
ClearWith<TEvent> No value The event takes the value away without putting one in its place

Use SetValue and SetFrom together when the event payload supplies some values and the event type determines others:

[EventType]
public record MbSetValueInvoiceIssued(decimal Amount);
[EventType]
public record MbSetValueInvoicePaid;
public record MbSetValueInvoice(
[Key]
Guid Id,
[SetFrom<MbSetValueInvoiceIssued>(nameof(MbSetValueInvoiceIssued.Amount))]
decimal Amount,
[SetValue<MbSetValueInvoiceIssued>("issued")]
[SetValue<MbSetValueInvoicePaid>("paid")]
string Status);

View C# snippet source on GitHub

The invoice-issued event supplies Amount through SetFrom while setting Status to "issued" through SetValue. The invoice-paid event changes only the status to "paid". For payload mapping options, continue with Basic mapping.