Skip to content

Migrate Chronicle event source strings to definitions

You can replace routing strings with an event source definition in Cratis Chronicle without rewriting stored events. Keep the existing event source and stream names exactly as they are so that old and new observers continue to match the same history.

This guide assumes your .NET code appends with eventSourceType:, eventStreamType: and eventStreamId: and filters observers with [EventSourceType] and [EventStreamType]. If you have not declared a definition before, start with Define an event source.

The stored events do not change. A definition writes the same two values the strings did: the event source type and the event stream type. If the definition’s names equal the strings you used, events you wrote before the migration still match the new filters, and new events match the old ones.

The definition gives appends and observers one place to get their routing names. The client and kernel check those names, and you can declare which routing values take part in concurrency checks. Events appended through the definition also carry its name in EventContext.EventSource; that field stays empty on older events.

  1. Collect the strings in use. Search for eventSourceType:, eventStreamType:, [EventSourceType( and [EventStreamType(. Each distinct event source type becomes a definition. Each distinct stream type that goes with it becomes a stream.
  2. Declare the definitions with the existing strings as names. Keep the casing: order and Order are different event source types. Leave concurrency dimensions undeclared while you migrate the routing.
  3. Switch the appends to Append<TSource> with eventStream: and eventStreamId:. Keep the event source id and stream id values as they were.
  4. Switch the observers to [FromEventSource<TSource>("stream")].
  5. Build the application. The analyzer reports a stream the definition does not declare (CHR0055) and a leftover string attribute that contradicts the typed filter (CHR0056). Correct unknown stream names and remove the replaced string attributes.
  6. Check that the typed observer receives events written with the old strings and that a string-filtered observer receives events written through the definition. If either misses events, compare the exact names and casing before changing the filters.

Choose the concurrency dimensions separately once routing is typed. Until you declare any, the appends keep the guard they had.

Before, the routing is repeated at every call site and in every observer:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reactors;
[EventType]
public record PaymentReceived(decimal Amount);
public class PaymentWriter(IEventLog eventLog)
{
// The routing values are strings that every caller must repeat exactly.
public Task<AppendResult> Receive(EventSourceId orderId, decimal amount, string period) =>
eventLog.Append(
orderId,
new PaymentReceived(amount),
eventStreamType: "payments",
eventStreamId: period,
eventSourceType: "order");
}
[EventSourceType("order")]
[EventStreamType("payments")]
public class PaymentReactor : IReactor
{
public Task Received(PaymentReceived @event, EventContext context) => Task.CompletedTask;
}

View C# snippet source on GitHub

After, it is declared once:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.EventSources;
using Cratis.Chronicle.Reactors;
// The names are the strings that are already stored on existing events, so those events still match.
[EventSource("order")]
[EventStream("payments")]
public class OrderEventSource : IEventSource;
public class TypedPaymentWriter(IEventLog eventLog)
{
public Task<AppendResult> Receive(EventSourceId orderId, decimal amount, string period) =>
eventLog.Append<OrderEventSource>(
orderId,
new PaymentReceived(amount),
eventStream: "payments",
eventStreamId: period);
}
[FromEventSource<OrderEventSource>("payments")]
public class TypedPaymentReactor : IReactor
{
public Task Received(PaymentReceived @event, EventContext context) => Task.CompletedTask;
}

View C# snippet source on GitHub

OrderEventSource is the new C# type, but its stored names remain order and payments. The typed append and observer resolve those same strings from the definition, so existing events still match. Renaming the C# type is not a reason to change the declared names.

Migrate one event source at a time if that makes the change easier to check. Registering a definition does not require every writer to use it: the kernel still accepts string-based appends, even when their event source type equals a registered name. Observers compare the stored values, so typed and string filters can coexist while you update call sites.

When the strings and the names must differ

Section titled “When the strings and the names must differ”

If you want a new, cleaner name, treat it as a rename, not a migration. Events already stored keep the old event source type, and observers on the new name will not see them. See Questions and pitfalls.

Continue with Filter observers to check how typed filters combine with other filters. For the underlying string form, use Filter by event source type. Dynamic Consistency Boundary in Chronicle explains what the migration does and does not change about consistency.