Let’s create a WebAPI solution following some guidelines, in order to practice and improve our architecture skills, along with the understanding of the structure of the Application, and common features to be included.
If you are not familiar with Clean Architecture, I suggest you to read this article first:
https://mulzatech.com/mastering-clean-architecture-in-net-a-comprehensive-guide-for-c-developers/
Clean Architecture C# Exercise
In this exercise you will create a blank and basic solution using the conventional project structure for the usage of Clean Architecture. You are tasked to add each project into the solution, add the required references for each one of them following the architecture standards.
When creating the Web API project you must enable support for Docker, and also use the template which contains the “WeatherController” boiler plate code, so we can easily compile the solution and make sure everything is working correctly.
Also recommended, but optional for this exercise, create a blank “Presentation” project which would be used to have the Frontend code of an application, if any. This layer could be used to hold a Web App using Blazor, for example, or perhaps could be removed completely in the case the web project is handled on a different solution using a different technology (such as Angular, next.js, etc)
You must also add the following nuget packages, and set them up correctly on the solution:
- MediatR
- FluentValidation
- Serilog
And lastly, to practice your skills with git, you will create a blank repository on your Github personal profile (if you don’t have one, make sure to create it since it will become an extremely useful resource for your professional career!), and push the code there. Don’t forget to add the readme.md file!
And that’s it! The exercise will make you think about the correct solution structure, the different projects, the different layers and the connection between them. Also configuration of different nuget packages, including logging. And last but not least, you’ll use git to push the solution into your profile’s repository 😉
Proposed C# Exercise Challenges:
For this exercise I propose the following challenges to be considered, to maximize learning opportunities during our coding session.
- Create a blank solution, so to add projects by hand as needed, and configure them properly, step by step.
- Create a separate Dependency Injection class on each project, so we follow good coding practices at the time of the services registration. Then configure them properly on the WebAPI Project’s program.cs.
TIP: MediatR and FluentValidation are services to be registered on the Application layer 😉 - Configure Serilog on program.cs, and also provide the necessary settings on the configuration file for it to function properly. You can test this by running the WeatherController basic endpoint that is present when you create the WebAPI project.
- Pay attention to good coding practices, and also the folder structure of your solution!
- Get into the good habit of using git through the CLI, and publish the repo to your personal Github profile.
Solution
I’ll share below my suggested approach to structure this solution, so you can use as example if you get stuck at some point. Make sure to use Google whenever needed and also Chat-GPT 😉 before looking for the solution, so you enhance your problem solving skills.
Github Repo: https://github.com/marcelomusza/BaseCleanArchitectureExample/
Happy coding!