Skip to content

Commit 6d620bb

Browse files
alexeyzimarevclaude
andcommitted
docs: update serialisation page in preview docs to match v0.16
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3e3e7eb commit 6d620bb

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

docs/src/content/docs/next/persistence/serialisation.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,29 @@ Then, you can call this code in your bootstrap code:
5252
BookingEvents.MapBookingEvents();
5353
```
5454

55-
### Auto-registration of types
55+
### Auto-registration with source generator
5656

57-
For convenience purposes, you can avoid manual mapping between type names and types by using the `EventType` attribute.
57+
The recommended way to register event types is to use the `[EventType]` attribute combined with the Eventuous source generator. The generator automatically discovers all types decorated with `[EventType]` in your project and generates a module initializer that registers them at startup — no manual registration code needed.
5858

59-
Annotate your events with it like this:
59+
Annotate your events with the `[EventType]` attribute:
6060

6161
```csharp
6262
[EventType("V1.FullyPaid")]
6363
public record BookingFullyPaid(string BookingId, DateTimeOffset FullyPaidAt);
64+
65+
[EventType("V1.RoomBooked")]
66+
public record RoomBooked(string RoomId, LocalDate CheckIn, LocalDate CheckOut, float Price);
6467
```
6568

66-
Then, use the registration code in the bootstrap code:
69+
That's it. The source generator produces a module initializer class per assembly, which calls `TypeMap.Instance.AddType(...)` for each annotated event type. Registration happens automatically when the assembly is loaded — you don't need to write any startup code.
70+
71+
:::tip
72+
Eventuous also includes a diagnostic analyzer (`EVTC001`) that warns you when an event type is used in aggregates or state projections but is missing the `[EventType]` attribute.
73+
:::
74+
75+
### Reflection-based registration
76+
77+
As an alternative to the source generator, you can use reflection-based registration. This scans assemblies at runtime for types decorated with `[EventType]`:
6778

6879
```csharp
6980
TypeMap.RegisterKnownEventTypes();
@@ -75,23 +86,9 @@ The registration won't work if event classes are defined in another assembly, wh
7586
TypeMap.RegisterKnownEventTypes(typeof(BookingFullyPaid).Assembly);
7687
```
7788

78-
If you use the .NET version that supports module initializers, you can register event types in the module. For example, if the domain event classes are located in a separate project, add the file `DomainModule.cs` to that project with the following code:
79-
80-
```csharp title="DomainModule.cs"
81-
using System.Diagnostics.CodeAnalysis;
82-
using System.Runtime.CompilerServices;
83-
using Eventuous;
84-
85-
namespace Bookings.Domain;
86-
87-
static class DomainModule {
88-
[ModuleInitializer]
89-
[SuppressMessage("Usage", "CA2255", MessageId = "The \'ModuleInitializer\' attribute should not be used in libraries")]
90-
internal static void InitializeDomainModule() => TypeMap.RegisterKnownEventTypes();
91-
}
92-
```
93-
94-
Then, you won't need to call the `TypeMap` registration in the application code at all.
89+
:::note
90+
With the source generator in place, calling `RegisterKnownEventTypes()` is typically unnecessary. The generator handles registration at compile time, which is both more reliable and avoids the overhead of runtime assembly scanning.
91+
:::
9592

9693
### Default serializer
9794

0 commit comments

Comments
 (0)