Using Domain Events¶
A domain event is something that happened in a particular domain, and something you want other parts of the same domain to be aware of and potentially react to (the in-process principle).
An important benefit of domain events is that any side effects of something happening in a domain can be expressed explicitly and not implicitly. Those side effects must be consistent, i.e. either all operations related to the task happen, or none. In addition, domain events enable a better separation of concerns among classes within the same domain.
Define Domain Events¶
A domain event is just a simple POCO type that represents an interesting occurence in the domain:
Define New Event Handler¶
Define a new event handler as follows:
public class CutomDomainEventHandler : IEventHandler<CustomDomainEvent>
{
public async Task Handle(CustomDomainEventmessage)
{
//Some logic here
}
}
Register Event Handler and Subscribe to Domain Event¶
Register an event handler or subscribe to a domain event as follows:
void Initialize(IServiceCollection serviceCollection)
{
...
serviceCollection.AddTransient<CustomDomainEventHandler>();
...
}
void PostInitialize(IApplicationBuilder appBuilder)
{
...
var eventHandlerRegistrar = appBuilder.ApplicationServices.GetService<IHandlerRegistrar>();
eventHandlerRegistrar.RegisterHandler<CustomDomainEvent>((message, token) => appBuilder.ApplicationServices.GetService<CustomDomainEventHandler>().Handle(message));
...
}
Raise Domain Events¶
In your domain entities, when any significant status change happens, you can raise your domain events as follows:
var eventPublisher = _container.Resolve<IEventPublisher>();
eventPublisher.Publish(new CustomDomainEvent()));
Override Existing Event Handler with New Derived Type¶
This might be a useful option in some cases:
//Derive a new handler from an overrided handler class
public class CustomDomainEventHandler2 : CustomDomainEventHandler
{ .... }
//Override in DI container
void Initialize(IServiceCollection serviceCollection)
{
...
serviceCollection.AddTransient<CustomDomainEventHandler, CustomDomainEventHandler2>();
...
}