Welcome to the twenty episode of my course “Becoming a software developer” in which we will implement our custom “handler” that will be responsible for executing the given methods, dealing with exceptions etc.
In the last 2 episodes, we dealt with exceptions. It’s high time to actually complete this story and also extend the rather simple extension handling middleware. In order to achieve that, we will implement our custom handler using the so-called Fluent API technique that will allow defining the flow of the methods (e.g. some more or less complicated business logic) into a nicely written method chain. Such method chaining is another programming technique and the goal of this episode is to actually learn 2 things – how the Fluent API can be implemented, but also how you can switch from a typical, procedural style of writing the code into the more expressive one.
public async Task HandleAsync(Login command)
=> await _handler
.Validate(async () => ...)
.Run(async () => await _userService.LoginAsync(command.Email, command.Password))
.Run(async () =>
var user = await _userService.GetAsync(command.Email);
var jwt = _jwtHandler.CreateToken(user.Id, user.Role);
.OnCustomError(async exception => ...)
.OnError(async exception => ...)
.Run(async => ...)
In the next episode, we will store the application data using MongoDB which is a NoSQL database.