EasyEvs - Series 2
EasyEvs in a few minutes
Consuming events
Now that events are waiting on the stream, we need to do something with them, so let’s dive into the code.
FIrst we need to implement a handler for the event
public class OrderCreatedHandler : IHandleEsvent<OrderCreated>
{
public Task<OperationResult> Handle(OrderCreated @event, IConsumerContext context, CancellationToken cancellationToken)
{
// Do something with your event here
return Task.FromResult(OperationResult.Ok);
}
}
That’s it, now when registering EasyEvs we need to tell in which assemblies to find the handler, the limitation set on purpose is that only 1 handler per event per app will be resolved. This is to ensure single responsibiltly on microservices, you want your microservice to do something with your event that is related to your domain. If you have to do multiple things, then you can do them in the handler.
What we return and why do we need that?
EventStore allows to Ack the message, to retry later or to park it on another stream where you can reply them manually. So you can return any of 3 values (if you have a transient error or an unrecoverable one) to deal with these scenarios.
public enum OperationResult
{
Ok, // The event was handled correctly and should be marked as processed on the subscription.
Retry, // There was a transient error and the event must be retried later.
Park // There was an unrecoverable error, and manual intervention is required, The event will be parked and can be replied manually later.
}
Registering handlers
var configuration = new EasyEvsDependencyInjectionConfiguration()
{
Assemblies = new[] { typeof(OrderCreatedHandler).Assembly }
};
services.AddEasyEvs(configuration);
With the lines above all the handlers in the assembly with be registeered, so if your services is in charge of a read model to build a dashboard you can implemeent IHandlesEvent
for many events and only use the code above to register all of them.
That’s all you need to start with EventStore! And we covered it in only 2 pages, in less than 5 minutes.
<–Next we will see how we can hook actions to be executed before and after handling events in a transparent way.–>