Skip to content

Cratis Chronicle model-bound projections: Use constant keys

Use a constant key in Cratis Chronicle when events from different event sources should update one shared read model instance. Instead of selecting an instance by the event source id, the mapping uses a fixed string, such as "metrics". This lets a counter track a total across sources rather than a separate total for each source.

In .NET, set ConstantKey on the class-level [FromEvent] attribute to route that event type to a fixed instance:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record MbConstantKeyOrderPlaced(string CustomerName, DateTimeOffset PlacedAt);
[FromEvent<MbConstantKeyOrderPlaced>(ConstantKey = "global")]
public record MbConstantKeyGlobalOrderSummary(
[SetFrom<MbConstantKeyOrderPlaced>(nameof(MbConstantKeyOrderPlaced.CustomerName))]
string LastCustomer,
[SetFrom<MbConstantKeyOrderPlaced>(nameof(MbConstantKeyOrderPlaced.PlacedAt))]
DateTimeOffset LastOrderDate);

View C# snippet source on GitHub

Every matching order-placement event updates the same summary instance, regardless of its event source. A constant key selects the destination; the property’s mappings still decide whether to replace a value, add an amount, or count the event.

Count, Increment, and Decrement with ConstantKey

Section titled “Count, Increment, and Decrement with ConstantKey”

Count, Increment, and Decrement also accept ConstantKey. Give the counter events the same key when they should contribute to one shared document:

using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record MbConstantKeyOrderPlacedForMetrics;
[EventType]
public record MbConstantKeyUserLoggedIn;
[EventType]
public record MbConstantKeyUserLoggedOut;
[EventType]
public record MbConstantKeyErrorOccurred;
public record MbConstantKeySystemMetrics(
[Count<MbConstantKeyOrderPlacedForMetrics>(ConstantKey = "metrics")]
int TotalOrders,
[Increment<MbConstantKeyUserLoggedIn>(ConstantKey = "metrics")]
[Decrement<MbConstantKeyUserLoggedOut>(ConstantKey = "metrics")]
int ActiveSessions,
[Count<MbConstantKeyErrorOccurred>(ConstantKey = "metrics")]
int TotalErrors);

View C# snippet source on GitHub

Here, the three counter properties update the "metrics" document regardless of which user or order produced the events.

A read model can use the event source id for one event type and a constant key for another. The destination is selected per event mapping, not once for the entire read model type:

[EventType]
public record MbConstantKeyUserRegistered;
[EventType]
public record MbConstantKeyOrderPlacedGlobal;
[FromEvent<MbConstantKeyUserRegistered>]
public record MbConstantKeyUserDashboard(
[Key]
Guid UserId,
string Name,
// A per-instance property alongside a constant-keyed one on the same read model
[Count<MbConstantKeyOrderPlacedGlobal>(ConstantKey = "global-stats")]
int PlatformTotalOrders);

View C# snippet source on GitHub

The registration event selects a user-specific dashboard. The order counter selects the document with key "global-stats"; it does not copy the platform total into every user’s dashboard. Both documents use the same read model type, but they are different instances.

This store summary routes its counter events to the same key, so users and products contribute to one set of store-wide metrics:

// Events
[EventType]
public record MbConstantKeyProductPurchased(string ProductId, decimal Amount);
[EventType]
public record MbConstantKeyProductReturned(string ProductId, decimal Amount);
[EventType]
public record MbConstantKeyPageViewed(string PageUrl);
// Global read model
public record MbConstantKeyStoreMetrics(
[Count<MbConstantKeyProductPurchased>(ConstantKey = "store")]
int TotalPurchases,
[Count<MbConstantKeyProductReturned>(ConstantKey = "store")]
int TotalReturns,
[Increment<MbConstantKeyProductPurchased>(ConstantKey = "store")]
[Decrement<MbConstantKeyProductReturned>(ConstantKey = "store")]
int NetTransactions,
[Count<MbConstantKeyPageViewed>(ConstantKey = "store")]
int TotalPageViews);

View C# snippet source on GitHub

The matching events accumulate into the document with key "store". For the difference between an event total and a quantity that rises and falls, see counters.