Hi Friends,
In this section, we will continue from the last post and will delve inside API implementation inside clean architecture.
Git Link:- https://github.com/rahulsahay19/azconf-clean-architecture
Here, I have already added required nuget packages in my API project as shown below.
Here, I will adding required dependencies like Automapper, swagger, MediatR etc. This is the finished version of startup.cs file.
Now, let me go ahead and create connection string settings in App Settings. Here is the snippet for the same.
Here, I will be running on docker on port 1445. Here, I have also specified User Id and Password for the same. Now, let me go ahead and create base controller with the name ApiController itself. Here is the snippet looks like
Here, I have applied all the required things like API versioning, Route, Annotation and inherited the same with ControllerBase as well. Then, I have created Movies Controller as shown below
Here, I have first injected the Mediator. Therefore, via this and I will issue both read and write request and will do the bifurcation whether its a read query or write query. But before running that, one last thing we need to do is to update the program.cs file to seed the db. Here is the snippet for the same.
It means while starting the app, I am first seeding the db here. Having said that let’s go ahead and run the SQL in docker first with below command.
docker run -e ‘ACCEPT_EULA=Y’ -e ‘SA_PASSWORD=Docker@1’ -p 1445:1433 -d mcr.microsoft.com/mssql/server:2019-latest
Having said that, it will look like
This SHA value is the representation of that container started successfully. You can verify the same using docker ps command as shown below.
With this in place, now I can go ahead and run my app, it will open like shown below
At the same time, I can now open sql server instance like shown below.
Here, server name will go like localhost,1445 and then provide User ID and Passwordas provided in the app settings and while running the SQL conatiner. Here, you can see that database is successfully seeded.
Now, let’s go ahead and try running the query and let’s put breakpoint in the visual studio to see the workflow.
Then, in Visual Studio, it will come like this
Then, mediator will process the query like shown below.
Having said that, here it produced the final result like this.
Similarly, it goes for command handler as well means for inserting the record.
This will land in command handler
And, upon execution, this will post one new record in the db like shown below.
Having said that, we have concluded this Clean Architecture session. I hope you would have liked this article. You can refer the code as mentioned in the git link.
Git Link:- https://github.com/rahulsahay19/azconf-clean-architecture
With this I would like to wrap this session here. Thanks for joining me.
Thanks,
Rahul Sahay
Happy Coding