Skip to content

Chronicle projection initial values for read model defaults

Initial values give a Cratis Chronicle read model meaningful defaults before event mappings assign its properties. Use them for a draft status, an inventory threshold, or another starting value that events may leave unchanged. Chronicle captures this state in the projection definition; it is not a factory run for each event or query.

.NET exposes WithInitialValues, while TypeScript exposes withInitialValues. Kotlin and Elixir capture defaults declared on the read model itself.

In .NET or TypeScript, provide a callback that returns the starting model. The builder evaluates it while creating the definition, and event mappings later apply on top of the captured state.

For Kotlin, give every primary-constructor parameter a default so the client can construct the initial model without arguments. For Elixir, use defstruct defaults. These defaults become part of the registered projection definition rather than being supplied by a constructor when a stored document is read. See The constructor may not run for why query-time construction is not a substitute for projection defaults.

Initial values
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
public enum InitialValuesUserStatus
{
Inactive,
Active
}
[EventType]
public record InitialValuesUserCreated(string Name, string Email);
public record InitialValuesUserProfile(
string Name,
string Email,
InitialValuesUserStatus Status,
DateTimeOffset CreatedAt,
DateTimeOffset? LastLogin,
int LoginCount,
bool IsVerified);
public class InitialValuesUserProfileProjection : IProjectionFor<InitialValuesUserProfile>
{
public void Define(IProjectionBuilderFor<InitialValuesUserProfile> builder) => builder
.WithInitialValues(() => new InitialValuesUserProfile(
Name: "Unknown user",
Email: string.Empty,
Status: InitialValuesUserStatus.Inactive,
CreatedAt: DateTimeOffset.UnixEpoch,
LastLogin: null,
LoginCount: 0,
IsVerified: false))
.From<InitialValuesUserCreated>(_ => _
.Set(m => m.Status).ToValue(InitialValuesUserStatus.Active)
.Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

An event often describes only the fields that changed. Initial values let the remaining fields carry a deliberate starting value rather than relying on the client’s or storage layer’s representation of a missing property.

Choose defaults according to what readers need:

Need Example
Business defaults A new order starts as draft until an event changes its status.
Non-null collections A customer read model starts with empty address and tag collections.
Sentinel values A timestamp starts at a known sentinel until the first event provides a real value.
Partial events Inventory limits are configured once while stock events update only quantities.

Set a default that describes the state before a relevant event changes it. For example, an order can start as draft while its creation event supplies the customer and order details:

Business defaults
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
public enum InitialValuesOrderStatus
{
Draft,
Submitted
}
[EventType]
public record InitialValuesOrderSubmitted(string CustomerName, decimal TotalAmount);
public record InitialValuesOrderSummary(
string CustomerName,
InitialValuesOrderStatus Status,
decimal TotalAmount,
DateTimeOffset SubmittedAt,
string Notes);
public class InitialValuesOrderSummaryProjection : IProjectionFor<InitialValuesOrderSummary>
{
public void Define(IProjectionBuilderFor<InitialValuesOrderSummary> builder) => builder
.WithInitialValues(() => new InitialValuesOrderSummary(
CustomerName: string.Empty,
Status: InitialValuesOrderStatus.Draft,
TotalAmount: 0m,
SubmittedAt: DateTimeOffset.UnixEpoch,
Notes: "No notes"))
.From<InitialValuesOrderSubmitted>(_ => _
.Set(m => m.Status).ToValue(InitialValuesOrderStatus.Submitted)
.Set(m => m.SubmittedAt).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

For ordinary collection-valued properties, an empty collection can let consumers enumerate values without checking for null. A collection managed by Children() follows different rules, explained below.

Initialize collections
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
public record InitialValuesAddress(string Street, string City);
[EventType]
public record InitialValuesCustomerRegistered(string Name);
public record InitialValuesCustomerRecord(
string Name,
IEnumerable<InitialValuesAddress> Addresses,
IEnumerable<string> Tags);
public class InitialValuesCustomerRecordProjection : IProjectionFor<InitialValuesCustomerRecord>
{
public void Define(IProjectionBuilderFor<InitialValuesCustomerRecord> builder) => builder
.WithInitialValues(() => new InitialValuesCustomerRecord(
Name: string.Empty,
Addresses: Array.Empty<InitialValuesAddress>(),
Tags: Array.Empty<string>()))
.From<InitialValuesCustomerRegistered>();
}

View C# snippet source on GitHub

Keep configuration defaults separate from event-driven changes. In this inventory example, stock events change quantities while minimum levels and reorder points retain their initial values. The Add and Subtract functions apply each event’s quantity to the current value:

Defaults for fields events do not set
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record InitialValuesStockReceived(int Quantity);
[EventType]
public record InitialValuesStockReserved(int Quantity);
public record InitialValuesInventoryItem(
int CurrentStock,
int ReservedStock,
DateTimeOffset LastUpdated,
int MinimumLevel,
int MaximumLevel,
int ReorderPoint);
public class InitialValuesInventoryProjection : IProjectionFor<InitialValuesInventoryItem>
{
public void Define(IProjectionBuilderFor<InitialValuesInventoryItem> builder) => builder
.WithInitialValues(() => new InitialValuesInventoryItem(
CurrentStock: 0,
ReservedStock: 0,
LastUpdated: DateTimeOffset.UnixEpoch,
MinimumLevel: 10,
MaximumLevel: 1000,
ReorderPoint: 20))
.From<InitialValuesStockReceived>(_ => _
.Add(m => m.CurrentStock).With(e => e.Quantity)
.Set(m => m.LastUpdated).ToEventContextProperty(c => c.Occurred))
.From<InitialValuesStockReserved>(_ => _
.Add(m => m.ReservedStock).With(e => e.Quantity)
.Set(m => m.LastUpdated).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

Keep defaults deterministic because they are captured when the projection definition is built. Reading the current time in the callback would capture definition-build time, not the time a later read model instance is created. For a timestamp describing an event, map its event context instead.

Use initial values for starting state, not for facts that should be recorded in events. Once the defaults are in place, continue with Set properties for assignments or Functions for counters and running totals.