Skip to content

Cratis Chronicle projections: use a constant key

A constant key in Cratis Chronicle directs events from different event sources into one read model instance. Use it when you need one order count or engagement total rather than a separate result for each source. Apply the same key to every event mapping that contributes to that result.

Use UsingConstantKey(string value) on a From mapping to select a fixed read model key. The read model remains a normal record or class; you do not need a different model shape to share one instance across event sources.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecConstantKeyOrderPlaced(decimal Total);
public record DecConstantKeyGlobalCounter(int TotalOrders);
public class DecConstantKeyGlobalCounterProjection : IProjectionFor<DecConstantKeyGlobalCounter>
{
public void Define(IProjectionBuilderFor<DecConstantKeyGlobalCounter> builder) => builder
.From<DecConstantKeyOrderPlaced>(_ => _
.UsingConstantKey("global")
.Count(m => m.TotalOrders));
}

View C# snippet source on GitHub

Every handled order-placement event updates the same counter with the key "global", regardless of its event source ID. A constant key changes where events are mapped; it does not subscribe the projection to additional event types or sequences.

Use Count for the number of registrations and Increment/Decrement for a total that can rise and fall, such as active sessions. Each contributing mapping below uses the key "site" so the totals stay in one instance:

[EventType]
public record DecConstantKeyUserRegistered(string Name);
[EventType]
public record DecConstantKeyUserLoggedIn;
[EventType]
public record DecConstantKeyUserLoggedOut;
public record DecConstantKeySiteStatistics(
int TotalUsers,
int ActiveSessions);
public class DecConstantKeySiteStatisticsProjection : IProjectionFor<DecConstantKeySiteStatistics>
{
public void Define(IProjectionBuilderFor<DecConstantKeySiteStatistics> builder) => builder
.From<DecConstantKeyUserRegistered>(_ => _
.UsingConstantKey("site")
.Count(m => m.TotalUsers))
.From<DecConstantKeyUserLoggedIn>(_ => _
.UsingConstantKey("site")
.Increment(m => m.ActiveSessions))
.From<DecConstantKeyUserLoggedOut>(_ => _
.UsingConstantKey("site")
.Decrement(m => m.ActiveSessions));
}

View C# snippet source on GitHub

Use UsingConstantParentKey(string value) to send child events to one shared parent. The child still needs its own key so Chronicle can distinguish items within that parent’s collection:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecConstantKeyUserJoined(string UserId, string UserName);
public record DecConstantKeyTeamMember(string UserId, string Name);
public record DecConstantKeyTeam(IEnumerable<DecConstantKeyTeamMember> Members);
public class DecConstantKeyTeamActivityProjection : IProjectionFor<DecConstantKeyTeam>
{
public void Define(IProjectionBuilderFor<DecConstantKeyTeam> builder) => builder
.Children(m => m.Members, children => children
.IdentifiedBy(e => e.UserId)
.From<DecConstantKeyUserJoined>(_ => _
.UsingConstantParentKey("main-team")
.Set(m => m.Name).To(e => e.UserName)));
}

View C# snippet source on GitHub

Strategy Method When to use
Event source ID (default) Keep one instance per event source ID.
Event property UsingKey(e => e.Property) Identify the instance from event content.
Event context UsingKeyFromContext(c => c.Property) Identify the instance from event metadata.
Composite UsingCompositeKey<T>(...) Use several values together as the identity.
Constant UsingConstantKey("value") Direct the configured events to one shared instance.

Use the same key across several event types to collect their metrics in one document:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecConstantKeyPageViewed(string PageUrl);
[EventType]
public record DecConstantKeyButtonClicked(string ButtonId);
[EventType]
public record DecConstantKeyFormSubmitted(string FormId);
public record DecConstantKeyEngagementMetrics(
int PageViews,
int ButtonClicks,
int FormSubmissions);
public class DecConstantKeyEngagementMetricsProjection : IProjectionFor<DecConstantKeyEngagementMetrics>
{
public void Define(IProjectionBuilderFor<DecConstantKeyEngagementMetrics> builder) => builder
.From<DecConstantKeyPageViewed>(_ => _
.UsingConstantKey("metrics")
.Count(m => m.PageViews))
.From<DecConstantKeyButtonClicked>(_ => _
.UsingConstantKey("metrics")
.Count(m => m.ButtonClicks))
.From<DecConstantKeyFormSubmitted>(_ => _
.UsingConstantKey("metrics")
.Count(m => m.FormSubmissions));
}

View C# snippet source on GitHub

The configured engagement events contribute to a single metrics document with the key "metrics". To keep separate totals for combinations such as customer and reporting period, use composite keys. For the operations available on each total, see projection functions.