Skip to content

Modeling events in Cratis Chronicle

An event in Cratis Chronicle records a business fact that happened, rather than an instruction to do something or a copy of the current state. Model that fact explicitly so a projection can build a view of it, a reactor can act on it, and a later reader can understand the change.

Start with the decision the system accepted. For an order, placing it and applying a discount are meaningful facts; overwriting an order row does not explain either decision. The event’s name and payload should preserve that distinction.

Use a past-tense phrase in the domain’s language: OrderPlaced, AddressChanged, PaymentCaptured, or BookReturned. CreateOrder names a command, which expresses intent and can still be rejected. OrderState names a model, not the change that produced it. If the name sounds like an instruction, identify the fact that becomes true after the instruction succeeds.

using Cratis.Chronicle.Events;
public record ModelingEventsAddress(string Street, string City);
// A fact that happened
[EventType]
public record ModelingEventsAddressChanged(ModelingEventsAddress Address);
// An intent (that's a command) or a state blob (that's a read model) — not an event
[EventType]
public record ModelingEventsUpdateAddress(ModelingEventsAddress Address);

View C# snippet source on GitHub

Give each event one meaningful change to describe. A generic CustomerUpdated with optional name, address, and status fields makes every consumer infer which change occurred. Separate facts let a consumer subscribe to the change it needs without interpreting unrelated fields.

using Cratis.Chronicle.Events;
public record ModelingEventsCustomerName(string Value);
public record ModelingEventsEmail(string Value);
public record ModelingEventsDeactivationReason(string Value);
public record ModelingEventsCustomerAddress(string Street, string City);
// One event trying to be everything — consumers must guess what changed
[EventType]
public record ModelingEventsCustomerUpdated(
string? Name,
ModelingEventsCustomerAddress? Address,
ModelingEventsEmail? Email,
bool? Deactivated);
// Distinct facts — each consumer subscribes to exactly what it cares about
[EventType]
public record ModelingEventsCustomerRenamed(ModelingEventsCustomerName Name);
[EventType]
public record ModelingEventsCustomerAddressChanged(ModelingEventsCustomerAddress Address);
[EventType]
public record ModelingEventsCustomerDeactivated(ModelingEventsDeactivationReason Reason);

View C# snippet source on GitHub

Avoid nullable properties that stand in for a separate business fact. In the example below, placing an order and applying a discount describe different changes. Separate events make the distinction visible instead of asking each consumer to interpret an optional discount.

This is modeling guidance, not a prohibition in Chronicle’s schema system: nullable properties are supported. The question is whether absence describes the fact accurately or hides another change that deserves its own name.

using Cratis.Chronicle.Events;
public record ModelingEventsOrderId(Guid Value);
public record ModelingEventsMoney(decimal Amount, string Currency);
// Nullable smell — "sometimes there's a discount, sometimes not"
[EventType]
public record ModelingEventsOrderPlacedWithNullableDiscount(
ModelingEventsOrderId Id,
ModelingEventsMoney Total,
ModelingEventsMoney? Discount);
// Two facts
[EventType]
public record ModelingEventsOrderPlaced(ModelingEventsOrderId Id, ModelingEventsMoney Total);
[EventType]
public record ModelingEventsDiscountApplied(ModelingEventsOrderId Id, ModelingEventsMoney Amount);

View C# snippet source on GitHub

For existing schemas that allow nullable properties, explicit null and absence are equivalent and read back as null; missing non-nullable values use the type default where the schema permits it. A present nullable value such as decimal? holding 0 stays a value. Do not use JSON property presence to model a separate fact, and use schema-aware ContentComparison / VerifyContent rather than raw JSON equality to compare appended and stored content. See Schema representation.

When moving from CRUD, it is tempting to emit CustomerUpdated whenever a row changes. Name the business transition instead. AddressChanged records an address change, but it does not by itself say whether the customer moved or corrected a typo. If that distinction matters to the business, capture it rather than expecting a consumer to reconstruct the reason later.

Carry what was true then — and only that

Section titled “Carry what was true then — and only that”

Capture values whose historical meaning matters. An order can reference a product by identifier, but looking up that product’s current price cannot explain the price accepted for the order. Record the accepted price in the event.

References are useful for identity; they are not substitutes for historical values. Include enough data to interpret the fact later, without copying every field a particular screen might want. Derive presentation-specific values in a read model.

Before committing to an event type, ask whether someone could understand it in five years without the code that produced it. Check the name, identifiers, units, and values that explain the fact. A field named Amount needs a clear currency or other unit; a reference needs a clear meaning. This review is cheaper before consumers depend on the schema.

Events change by migration, never by editing

Section titled “Events change by migration, never by editing”

When the same fact needs a new shape, define a new generation and an event type migration rather than silently changing the stored contract. If the business meaning has changed, introduce a different event type instead of making an old name mean two things. Evolve an event’s shape walks through the generation and migration workflow.