The main class is Mediator. It is an intermediary that allows you to bind different types of messages and their handlers.
EventCollector allows for delayed sending through an intermediary.
- Add dependency on IMediator to the class.
- We create or register in the DI class Mediator, parameterizing it with collections of synchronous and asynchronous message handlers. The list of handlers should correspond to the expected message types in the service.
- In the service methods, call the methods of the SendMessage or SendMessageAsync interface. The broker will automatically forward the message to the appropriate handler.
public IHttpActionResult Get ()
{
IEnumerable<ProductModel>products = _mediator
.SendMessage<GetProductsRequest, IEnumerable <ProductModel>>(new GetProductsRequest());
if (products is null ||! products.Any())
{
return NotFound();
}
else
{
return Ok(products);
}
}
- Implement in essence the interface IEntityEventPublisher. The standard implementation stores an instance of IEventCollector in an internal variable.
public class Payroll: IEntityKey<int>, IEntityEventPublisher
{
private IEventCollector _eventCollector;
public virtual void SetCollector(IEventCollector eventCollector);
{
_eventCollector = eventCollector;
}
}
- If necessary, publish the event using IEventCollector.
_eventCollector.Enqueue (new PayrollReachesFinalStatusEvent (this));
- In the application service managing domain entities, implement a unit of work for sending domain events.
using (var collector = _eventCollectorFactory.Create())
{
...
collector.Publish();
}
For correct event handling, Handlers must be registered in the Mediator class.