237
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
In code, you first need to register the event handler types in your IoC container, as shown in the
following example at
eShopOnContainers Ordering microservice
:
public
class
MediatorModule : Autofac.
Module
{
protected
override
void
Load
(ContainerBuilder builder)
{
// Other registrations ...
// Register the DomainEventHandler classes (they implement
IAsyncNotificationHandler<>)
// in assembly holding the Domain Events
builder.
RegisterAssemblyTypes
(
typeof
(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEvent
Handler)
.
GetTypeInfo
().
Assembly
)
.
AsClosedTypesOf
(
typeof
(IAsyncNotificationHandler<>));
// Other registrations ...
}
}
The code first identifies the assembly that contains the domain event handlers by locating the
assembly that holds any of the handlers (using typeof(ValidateOrAddBuyerAggregateWhenXxxx), but
you could have chosen any other event handler to locate the assembly). Since all the event handlers
implement the IAsyncNotificationHandler interface, the code then just searches for those types and
registers all the event handlers.
Dostları ilə paylaş: