|
| 1 | +# C# client usage |
| 2 | + |
| 3 | +This guide covers how to declare event type migrations in a .NET client, the operations available to you, and what happens when your migrators are registered with the Chronicle Kernel. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- A Chronicle-enabled .NET application |
| 8 | +- An event type marked with `[EventType]` that has evolved beyond generation 1 |
| 9 | + |
| 10 | +## Marking an event type with a generation |
| 11 | + |
| 12 | +Every `[EventType]` that has evolved past its first version must declare its current generation. The generation is part of the event type identity: |
| 13 | + |
| 14 | +```csharp |
| 15 | +// Generation 1 (original) — no explicit generation needed, defaults to 1 |
| 16 | +[EventType] |
| 17 | +public record AuthorRegistered(string Name); |
| 18 | +``` |
| 19 | + |
| 20 | +When the schema changes, the new record carries the higher generation. The old record can be kept for documentation or removed — Chronicle identifies events by the type name, not the .NET class: |
| 21 | + |
| 22 | +```csharp |
| 23 | +// Generation 2 — Name has been split into FirstName and LastName |
| 24 | +[EventType(2)] |
| 25 | +public record AuthorRegistered(string FirstName, string LastName); |
| 26 | +``` |
| 27 | + |
| 28 | +## Defining a migrator |
| 29 | + |
| 30 | +Implement `IEventTypeMigrationFor<TEvent>` where `TEvent` is the **latest generation** of the event type. The interface requires: |
| 31 | + |
| 32 | +- `From` — the generation this migrator reads from |
| 33 | +- `To` — the generation this migrator produces |
| 34 | +- `Upcast(IEventMigrationBuilder)` — transformation from `From` to `To` |
| 35 | +- `Downcast(IEventMigrationBuilder)` — transformation from `To` back to `From` |
| 36 | + |
| 37 | +```csharp |
| 38 | +using Cratis.Chronicle.Events; |
| 39 | +using Cratis.Chronicle.Events.Migrations; |
| 40 | + |
| 41 | +public class AuthorRegisteredMigrator : IEventTypeMigrationFor<AuthorRegistered> |
| 42 | +{ |
| 43 | + public EventTypeGeneration From => 1; |
| 44 | + public EventTypeGeneration To => 2; |
| 45 | + |
| 46 | + public void Upcast(IEventMigrationBuilder builder) |
| 47 | + { |
| 48 | + builder.Properties(pb => |
| 49 | + { |
| 50 | + var firstName = pb.Split("Name", separator: " ", part: SplitPartIndex.First); |
| 51 | + var lastName = pb.Split("Name", separator: " ", part: SplitPartIndex.Second); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public void Downcast(IEventMigrationBuilder builder) |
| 56 | + { |
| 57 | + builder.Properties(pb => |
| 58 | + { |
| 59 | + var name = pb.Combine("FirstName", "LastName"); |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Migrators are discovered automatically at startup — no explicit registration is needed. |
| 66 | + |
| 67 | +## Migration operations |
| 68 | + |
| 69 | +All operations return a `PropertyExpression` that identifies the expression in the migration definition. The property name you assign to the returned expression in `Properties()` becomes the output property name in the transformed event. |
| 70 | + |
| 71 | +### Split |
| 72 | + |
| 73 | +Extracts one segment of a string property by splitting it on a separator. |
| 74 | + |
| 75 | +```csharp |
| 76 | +builder.Properties(pb => |
| 77 | +{ |
| 78 | + var firstName = pb.Split("FullName", separator: " ", part: 0); // first segment |
| 79 | + var lastName = pb.Split("FullName", separator: " ", part: 1); // second segment |
| 80 | +}); |
| 81 | +``` |
| 82 | + |
| 83 | +Use `SplitPartIndex.First` and `SplitPartIndex.Second` for the most common cases: |
| 84 | + |
| 85 | +```csharp |
| 86 | +var firstName = pb.Split("FullName", " ", SplitPartIndex.First); |
| 87 | +var lastName = pb.Split("FullName", " ", SplitPartIndex.Second); |
| 88 | +``` |
| 89 | + |
| 90 | +### Combine |
| 91 | + |
| 92 | +Joins multiple source properties into a single string value, separated by a space. |
| 93 | + |
| 94 | +```csharp |
| 95 | +builder.Properties(pb => |
| 96 | +{ |
| 97 | + var fullName = pb.Combine("FirstName", "LastName"); |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +### RenamedFrom |
| 102 | + |
| 103 | +Reads a property value from an old property name. Use this when a property is being renamed between generations. |
| 104 | + |
| 105 | +```csharp |
| 106 | +builder.Properties(pb => |
| 107 | +{ |
| 108 | + var email = pb.RenamedFrom("EmailAddress"); // was EmailAddress, now Email |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +### DefaultValue |
| 113 | + |
| 114 | +Provides a literal default value for a property that did not exist in the source generation. |
| 115 | + |
| 116 | +```csharp |
| 117 | +builder.Properties(pb => |
| 118 | +{ |
| 119 | + var status = pb.DefaultValue("active"); |
| 120 | + var retries = pb.DefaultValue(0); |
| 121 | + var enabled = pb.DefaultValue(true); |
| 122 | +}); |
| 123 | +``` |
| 124 | + |
| 125 | +## Multi-generation migrations |
| 126 | + |
| 127 | +If your event type spans more than two generations, define one migrator per generation pair. Chronicle chains the migrators automatically: |
| 128 | + |
| 129 | +```csharp |
| 130 | +// Generation 1 → 2 migrator |
| 131 | +public class PersonRegisteredV1ToV2 : IEventTypeMigrationFor<PersonRegistered> |
| 132 | +{ |
| 133 | + public EventTypeGeneration From => 1; |
| 134 | + public EventTypeGeneration To => 2; |
| 135 | + |
| 136 | + public void Upcast(IEventMigrationBuilder builder) => |
| 137 | + builder.Properties(pb => |
| 138 | + { |
| 139 | + var email = pb.RenamedFrom("EmailAddress"); |
| 140 | + }); |
| 141 | + |
| 142 | + public void Downcast(IEventMigrationBuilder builder) => |
| 143 | + builder.Properties(pb => |
| 144 | + { |
| 145 | + var emailAddress = pb.RenamedFrom("Email"); |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +// Generation 2 → 3 migrator |
| 150 | +public class PersonRegisteredV2ToV3 : IEventTypeMigrationFor<PersonRegistered> |
| 151 | +{ |
| 152 | + public EventTypeGeneration From => 2; |
| 153 | + public EventTypeGeneration To => 3; |
| 154 | + |
| 155 | + public void Upcast(IEventMigrationBuilder builder) => |
| 156 | + builder.Properties(pb => |
| 157 | + { |
| 158 | + var firstName = pb.Split("Name", " ", SplitPartIndex.First); |
| 159 | + var lastName = pb.Split("Name", " ", SplitPartIndex.Second); |
| 160 | + }); |
| 161 | + |
| 162 | + public void Downcast(IEventMigrationBuilder builder) => |
| 163 | + builder.Properties(pb => |
| 164 | + { |
| 165 | + var name = pb.Combine("FirstName", "LastName"); |
| 166 | + }); |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +When a generation 1 event arrives, the Kernel chains the upcasts: 1→2, then 2→3, and stores all three generations. |
| 171 | + |
| 172 | +## How registration works |
| 173 | + |
| 174 | +When your application connects to Chronicle, the client: |
| 175 | + |
| 176 | +1. Discovers all `IEventTypeMigrationFor<T>` implementations via `IClientArtifactsProvider` |
| 177 | +2. Invokes `Upcast` and `Downcast` on each migrator to capture the transformation declarations |
| 178 | +3. Converts the declarations into JmesPath expressions |
| 179 | +4. Sends the complete `EventTypeDefinition` — including all generations and their migration definitions — to the Kernel during event type registration |
| 180 | + |
| 181 | +From that point on, the Kernel applies the migrations autonomously on every event append, without any further involvement from the client. |
| 182 | + |
| 183 | +## Validation: missing migrators |
| 184 | + |
| 185 | +If an event type is declared with a generation higher than 1 but has no migrators covering all generations up to the current one, Chronicle throws `MissingEventTypeMigrators` during startup. This prevents silent data loss from an incomplete migration chain. |
| 186 | + |
| 187 | +```text |
| 188 | +Cratis.Chronicle.Events.Migrations.MissingEventTypeMigrators: |
| 189 | + Event type 'AuthorRegistered' is at generation 3 but no migrators are registered for it. |
| 190 | +``` |
| 191 | + |
| 192 | +Ensure every generation gap has a corresponding `IEventTypeMigrationFor<T>` implementation before deploying an event type with a new generation. |
0 commit comments