Bolt.RequestBus : More On Events – Part 5

This is fifth part of the series. In our previous post we discuss about filters and how to use them in your application. If you don’t know what Bolt.RequestBus is please read the first part of this series. We did discuss about events in part 3 of this series. In this post we gonna find out about default events raised by the library and how you can use them instead of publishing events by yourself from RequestHandler.

The library always publish following events for each request sent.

  • RequestInitiated<TRequest> where TRequest : IRequest
    RequestBus publish this event before start processing the request handler. You can access the request object from “Request” property of the event dto that passed to handler.
  • RequestValidationFailed<TRequest> where TRequest : IRequest
    RequestBus publish this event only when validation of request failed if any validation registered for the request. The request instance and errors passed to handler as property of the event
  • RequestCompleted<TRequest> where TRequest : IRequest OR RequestCompleted<TRequest, TReturn> where TRequest : IRequest
    RequestBus publish this event when the processing of request completed.The request instance and response of request pass to haddler as Request and Response property of event. If the request doesn’t expect any response then the only Request property will be available.

That means in most cases we don’t need to publish an event from request handler. Lets rewrite the example shown in Part3 of the series. In part 3 we are publishing “UserRegistered” event from Requesthandler. Lets comment out that code that not necessary. I am commenting out code rather than removing to get better understanding what changed. Another change I am doing here is returning the new user’s Id instead of void, as I need this information in eventhandlers. This should point out why in built published events are not always enough 🙂

public class RegisterUserRequestHandler : AsyncRequestHandlerBase<RegisterUserRequest, Guid>
{
    private readonly IUserRepository repo;
    private readonly IMapper mapper;
    //private readonly IRequestBus bus;
 
    public RegisterUserRequestHandler(
        IUserRepository repo, 
        IMapper mapper, 
        //IRequestBus bus
        )
    {
        this.repo = repo;
        this.mapper = mapper;
        //this.bus = bus;
    }
 
    protected override async Task<Guid> ProcessAsync(RegisterUserRequest request)
    {
        var record = mapper.Map<RegisterUserRequest, UserRecord>(request);
        record.Id = Guid.NewGuid();
 
        await repo.CreateUserAsync(record);

        return record.Id;
 
        //await bus.PublishAsync(new UserRegisteredEvent
        //{
        //    Id = record.Id,
        //    Email = record.Email
        //});
    }
}

Now now lets see how we can logged the user in in event handler when UserRegistration Completed.

public class LoggedUserInOnUserRegisteredHandler : IAsyncEventHandler<RequestCompleted<RegisterUserRequest, Guid>>
{
    private readonly IAuth auth;
 
    public LoggedUserInOnUserRegistered(IAuth auth)
    {
        this.auth = auth;
    }
 
    public Task HandleAsync(RequestCompleted<RegisterUserRequest, Guid> evnt)
    {
        auth.Authenticate(evnt.Result); //user id
 
        return Task.FromResult(0);
    }
}

That’s it. You can write more eventhandlers to other task as Ishowed in third part of the series. In most cases you most probably not need to publish any event from RequestHandler and can take benefit of inbuilt events that published by the library.

This also simplify the EventSourcing just by writing a generic handler that publish all your events to a Queue system but require another blog post.

Thats all for today. Happy coding:)

Advertisements

2 thoughts on “Bolt.RequestBus : More On Events – Part 5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s