-
Notifications
You must be signed in to change notification settings - Fork 2
The Event Registry
The event registry keeps track of your event types and their names for you, so you can serialize the events and be confident that they can be deserialized to the same type. Its simplest incarnation is just a wrapper around an IReadOnlyDictionary<string, Type>
, which also caches the inverse mapping. However, most of the time you'll not want to create that dictionary manually.
You can use an AssemblyEventRegistry
, that does a lot of the work for you:
var eventRegistry = new AssemblyEventRegistry(
typeof(SomeEventType),
namer: type => type.Name,
inclusionPredicate: type => type.Name.EndsWith("Event"));
The namer and predicate are optional, defaulting to type => type.Name
and type => true
.
The event registry is built to be composable, and there are a couple of classes that help you compose instances into a new registry.
The ComposedEventRegistry
takes a number of registries and combines them into one. If there are multiple colliding registrations, an exception will be thrown:
var assemblyA = new AssemblyEventRegistry(typeof(TypeFromAssemblyA));
var assemblyB = new AssemblyEventRegistry(typeof(TypeFromAssemblyB));
var registry = new ComposedEventRegistry(assemblyA, assemblyB);
If you've already stored events in your database, and you want to refactor their class names, you can utilize a translating event registry.
var assemblyRegistry = new AssemblyEventRegistry(typeof(SomeEvent), type => type.Name.EndsWith("Event"));
var translations = new Dictionary<string, string>
{
// If events are renamed (e.g. if switching from SimpleEventRegistry to AssemblyEventRegistry)
// add translations here. Example:
{ "OldEventName", "NewEventName" }
};
var registry = new TranslatingEventRegistry(translations, assemblyRegistry, translateNewEvents: false);
By default, it will read events with both the old and new names, and store them with the new name. If, for some reason, you want to keep using the old name in the database, use translateNewEvents: true
.