Hi Friends,
In this section, we are going to continue from the last post and see how to implement API Layer. Having said that, let’s get started.
Source:- https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
This is the place where I will be segregating queries from commands, hence will be implementing CQRS here itself. First of all I will be creating below folders
- Commands
- Handlers
- Mappers
- Queries
- Responses
Now, I will be adding my first command with the name “CreateMovieCommand“. This will issue a command to infrastructure to create a new record in the database. This is going to implement IRequest from MediatR library. And, its going to need one response, which I will be creating next. This will look like as shown below.
As you can see that since this information is going to come API, hence it needs all the required information, which needs to be persisted in the db. That’s why I kept all the required properties in the command itself. Now, let’s go ahead and create MovieResponse class in Responses folder. This will also be like Command but with one extra property Id.
Next, we need to create handler which we will be creating CreateMovieHandler in Handlers folder, which looks like as shown below.
Here, we need to supply IRequestHandler, which takes CreateMovieCommand and MovieResponse. With that, I can go ahead and implement this. Here, first I have mapped Movie entity with request object. Then, I have added the movie entity in the database, which I am mapping back to MovieResponse to fetch the Id from the object and then I am returning movie response here. This also needs implementation of Automapper. Hence, let’s go ahead and create MovieMapper first. Below is the generic implementation for the same.
This means I also need to create Mapping profile as well. Hence, let’s go ahead and create MovieMappingProfile class in Mappers folder. This is also straight forward and looks like as shown below. Here, I have created two maps and mapped both ways.
Next, we need to complete the query part. Here, I will be creating “GetMoviesByDirectorNameQuery” class in Queries folder which will take IRequest object from MediatR and here we need to supply the MovieResponse as shown below. Since, I am expecting list in return, hence I passed as IEnumerable.
This will also need one handler. Here, I have created “GetMoviesByDirectorNameHandler“. Now, its job will be to query the record from Db.This looks like as shown below.
Pretty straight forward. Very much on the lines of command handler itself. Only thing change here is that, its handling queries here.
This is the example of CQRS which we implemented in Application layer via MediatR pattern. Only thing, which I have here that its writing and
querying from the same db rather than using multiple dbs as an example.
But that is fine. At least you guys get the crux of the implementation. Therefore, Application layer now will look like as shown below.
With this, I would like to wrap this here. In the next session, we will implement the API layer and share the code as well. Till then stay tuned and Happy Coding.
Thanks,
Rahul Sahay
Happy Coding