You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/content/docs/next/persistence/serialisation.md
+18-21Lines changed: 18 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,18 +52,29 @@ Then, you can call this code in your bootstrap code:
52
52
BookingEvents.MapBookingEvents();
53
53
```
54
54
55
-
### Auto-registration of types
55
+
### Auto-registration with source generator
56
56
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.
58
58
59
-
Annotate your events with it like this:
59
+
Annotate your events with the `[EventType]` attribute:
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]`:
67
78
68
79
```csharp
69
80
TypeMap.RegisterKnownEventTypes();
@@ -75,23 +86,9 @@ The registration won't work if event classes are defined in another assembly, wh
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
-
usingSystem.Diagnostics.CodeAnalysis;
82
-
usingSystem.Runtime.CompilerServices;
83
-
usingEventuous;
84
-
85
-
namespaceBookings.Domain;
86
-
87
-
staticclassDomainModule {
88
-
[ModuleInitializer]
89
-
[SuppressMessage("Usage", "CA2255", MessageId="The \'ModuleInitializer\' attribute should not be used in libraries")]
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.
0 commit comments