Skip to content

Cratis Chronicle model-bound projections: Count events

Use counters in Cratis Chronicle model-bound projections to track how many times an event happened or how many items are currently active. Increment and Count add one to a numeric property; Decrement subtracts one. Choose the operation and property names to make clear whether the value represents a current quantity or an event total.

Apply Increment to a numeric property when each matching event should increase its current value by one. It does not read an amount from the event payload.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record MbCountersUserLoggedIn;
public record MbCountersUserStatistics(
[Key]
Guid UserId,
[Increment<MbCountersUserLoggedIn>]
int LoginCount);

View C# snippet source on GitHub

Each login event adds one to LoginCount on the selected read model instance.

Pair Decrement with Increment when a quantity can rise and fall, such as the number of active connections.

[EventType]
public record MbCountersUserConnected;
[EventType]
public record MbCountersUserDisconnected;
public record MbCountersServerStatistics(
[Key]
Guid ServerId,
[Increment<MbCountersUserConnected>]
[Decrement<MbCountersUserDisconnected>]
int ActiveConnections);

View C# snippet source on GitHub

A connection event adds one to ActiveConnections; a disconnection event subtracts one. Both operations change the current value rather than keeping separate totals.

Use Count to express that a property records occurrences of an event. If the property has no value yet, counting starts at zero, then adds one for each matching event.

Count uses the property’s current value, just as Increment does. It does not maintain a separate tally. If another mapping sets or changes that property, subsequent counts continue from the changed value.

[EventType]
public record MbCountersOrderPlaced;
[EventType]
public record MbCountersOrderCancelled;
public record MbCountersEventMetrics(
[Key]
Guid Id,
[Count<MbCountersOrderPlaced>]
int TotalOrders,
[Count<MbCountersOrderCancelled>]
int CancelledOrders);

View C# snippet source on GitHub

Multiple event types can change the same property. Here, the creation event sets Quantity from InitialQuantity, restocking adds one, and a sale subtracts one. Separate properties count restocks and sales so those totals remain available even as the quantity changes:

[EventType]
public record MbCountersItemCreated(string Name, int InitialQuantity);
[EventType]
public record MbCountersItemRestocked;
[EventType]
public record MbCountersItemSold;
public record MbCountersInventoryItem(
[Key]
Guid ItemId,
[SetFrom<MbCountersItemCreated>(nameof(MbCountersItemCreated.Name))]
string Name,
[SetFrom<MbCountersItemCreated>(nameof(MbCountersItemCreated.InitialQuantity))]
[Increment<MbCountersItemRestocked>]
[Decrement<MbCountersItemSold>]
int Quantity,
[Count<MbCountersItemRestocked>]
int RestockCount,
[Count<MbCountersItemSold>]
int SalesCount);

View C# snippet source on GitHub

This activity read model keeps event totals separate from current state. Login and logout counts record each kind of event, while ActiveSessions rises and falls. Purchase and refund counts track occurrences; NetSpent adds and subtracts the amounts carried by those events.

// Events
[EventType]
public record MbCountersUserLoggedInFull(DateTimeOffset Timestamp);
[EventType]
public record MbCountersUserLoggedOutFull(DateTimeOffset Timestamp);
[EventType]
public record MbCountersPurchaseMade(decimal Amount);
[EventType]
public record MbCountersRefundIssued(decimal Amount);
// Read Model
public record MbCountersUserActivity(
[Key]
Guid UserId,
// Track login/logout counts
[Count<MbCountersUserLoggedInFull>]
int TotalLogins,
[Count<MbCountersUserLoggedOutFull>]
int TotalLogouts,
// Track active sessions
[Increment<MbCountersUserLoggedInFull>]
[Decrement<MbCountersUserLoggedOutFull>]
int ActiveSessions,
// Track transaction counts
[Count<MbCountersPurchaseMade>]
int PurchaseCount,
[Count<MbCountersRefundIssued>]
int RefundCount,
// Track transaction values
[AddFrom<MbCountersPurchaseMade>(nameof(MbCountersPurchaseMade.Amount))]
[SubtractFrom<MbCountersRefundIssued>(nameof(MbCountersRefundIssued.Amount))]
decimal NetSpent);

View C# snippet source on GitHub

Count and Increment both add one to the stored property. Their distinction is intent: Count reads as an event total, while Increment paired with Decrement reads as a changing quantity. Neither protects the value from other mappings.

To retain a total alongside a current quantity, declare separate properties, as in the examples above. Use AddFrom or SubtractFrom when the event carries an amount instead of representing a change of one.

Check which read model instance receives the counter event. By default, its event source id selects the instance, so events from different sources do not automatically contribute to one global total. Use a constant key when they should.

If a creation event supplies an initial quantity, set it with SetFrom before later events adjust it. Keep an event-total property dedicated to counting when it must not be reset by that initialization.