Skip to content

Chronicle projection functions for counters and totals

Cratis Chronicle projection functions maintain counters and totals from events, so your application can read a login count, stock quantity, or account balance without calculating it on each query. Choose a function according to what the event contributes: one occurrence, one item, or an amount carried in the event.

Use Count() inside a From mapping to add one for each handled event of that type. In this example, login events and action events update separate counters; neither counter includes unrelated event types:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecFunctionsUserLoggedIn(string Username);
[EventType]
public record DecFunctionsUserPerformedAction(string Username, string ActionType);
public record DecFunctionsUserActivity(
string Username,
int LoginCount,
int ActionCount);
public class DecFunctionsUserActivityProjection : IProjectionFor<DecFunctionsUserActivity>
{
public void Define(IProjectionBuilderFor<DecFunctionsUserActivity> builder) => builder
.From<DecFunctionsUserLoggedIn>(_ => _
.Count(m => m.LoginCount))
.From<DecFunctionsUserPerformedAction>(_ => _
.Count(m => m.ActionCount));
}

View C# snippet source on GitHub

Use Increment() and Decrement() when an event changes a quantity by one. An item-added event increases inventory; an item-removed event decreases it:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecFunctionsItemAdded(string Name);
[EventType]
public record DecFunctionsItemRemoved(string Name);
public record DecFunctionsInventory(int Quantity);
public class DecFunctionsInventoryProjection : IProjectionFor<DecFunctionsInventory>
{
public void Define(IProjectionBuilderFor<DecFunctionsInventory> builder) => builder
.From<DecFunctionsItemAdded>(_ => _
.Increment(m => m.Quantity))
.From<DecFunctionsItemRemoved>(_ => _
.Decrement(m => m.Quantity));
}

View C# snippet source on GitHub

Each call changes the value by exactly one, regardless of the event’s payload. Count() and Increment() both add one; choose the name that expresses the meaning of the field.

Use Add() or Subtract() when the change comes from an event property. In the .NET builder, .With(e => e.Amount) supplies the amount to apply to the current balance:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecFunctionsAccountOpened(string Number);
[EventType]
public record DecFunctionsMoneyDeposited(decimal Amount);
[EventType]
public record DecFunctionsMoneyWithdrawn(decimal Amount);
public record DecFunctionsAccount(string Number, decimal Balance);
public class DecFunctionsAccountProjection : IProjectionFor<DecFunctionsAccount>
{
public void Define(IProjectionBuilderFor<DecFunctionsAccount> builder) => builder
.From<DecFunctionsAccountOpened>(_ => _
.Set(m => m.Balance).ToValue(0m))
.From<DecFunctionsMoneyDeposited>(_ => _
.Add(m => m.Balance).With(e => e.Amount))
.From<DecFunctionsMoneyWithdrawn>(_ => _
.Subtract(m => m.Balance).With(e => e.Amount));
}

View C# snippet source on GitHub

The kernel supports numeric targets including int, long, float, double, and decimal. It uses the target property’s schema to select the numeric type and converts values for the operation. Declare a target type suitable for the values you accumulate; client-language type names differ.

Each function reads the property’s current value, applies the change, and stores the result for the next event. A missing numeric property starts at zero. If the read model needs another starting value, declare initial values before accumulating events.

The read model key determines which instance receives the change. A balance keyed by account accumulates that account’s events, not every account’s events. See composite keys when one identifier is not enough to select the instance.

A single event can update several properties. Here each transaction increases the transaction count and processed-event count by one, while its amount is added to the total:

[EventType]
public record DecFunctionsTransaction(decimal Amount);
public record DecFunctionsTransactionSummary(
int TransactionCount,
decimal TotalAmount,
int ProcessedEvents);
public class DecFunctionsTransactionSummaryProjection : IProjectionFor<DecFunctionsTransactionSummary>
{
public void Define(IProjectionBuilderFor<DecFunctionsTransactionSummary> builder) => builder
.From<DecFunctionsTransaction>(_ => _
.Count(m => m.TransactionCount)
.Add(m => m.TotalAmount).With(e => e.Amount)
.Increment(m => m.ProcessedEvents));
}

View C# snippet source on GitHub

For a field that should copy a value rather than accumulate it, use Set properties. For a timestamp that should follow every handled event, use FromEvery.