ASP.NET Core WebAPI – Clean Architecture
ASP.NET Core WebAPI – Clean Architecture
The Problem this Project Solves
Does it really make sense to Setup your ASP.NET Core Solution everytime you start a new WebApi Project ? Aren’t we wasting quite a lot of time in doing this over and over gain?
This is the exact Problem that I intend to solve with this Full-Fledged ASP.NET Core 3.1 WebApi Solution Template, that also follows various principles of Clean Architecture.
The primary goal is to create a Full-Fledged implementation, that is well documented along with the steps taken to build this Solution from Scratch. This Solution Template will also be available within Visual Studio 2019 (by installing the required Nuget Package / Extension).
- Demonstrate Clean Monolith Architecture in ASP.NET Core 3.1
- This is not a Proof of Concept
- Implementation that is ready for Production
- Integrate the most essential libraries and packages
NEW! fullStackHero is now on Preview!
I am currently working on a newer version of this Clean Architecture project which is on .NET 6 and has even more advanced features like Multitenancy support out of the box. Feel free to check it out!
Everything is done for you.
Get the Complete Source Code of this Project here. Feel free to contribute as well. The Steps to get started are mentioned in the next section.
Why ASP.NET Core WebAPI – Clean Architecture?
While getting started with this Project, I could not find any Clean Architecture implementation that is specifically built for ASP.NET Core 3.1 WebAPI.
Starting with a Clean Architecture in quite Important for the lifetime of your applications. You really don’t want to re-engineer you solutions half-way down the development stages, do you?
Every Major Update will be tagged as versioned Release. Included Features and Changes will be made available within the Release Pages.
- v1.1 – Stable Release – Download ZIP | Extension from Visual Studio MarketPlace
- v1.0-preview – Download here
- Make sure you are running on the latest .NET Core SDK (SDK 3.1 and above only). Get the latest one here.
- Visual Studio 2019 ( You can check out my Installation Guide of Visual Studio 2019 Community which is completely Free to use.) Make sure that ASP.NET and web development workload is installed.
- Install the latest DOTNET & EF CLI Tools by using this command – dotnet tool install –global dotnet-ef
- I Recommend that you read Onion Architecture In ASP.NET Core With CQRS – Detailed article to understand this implementation much better. This project is just an Advanced Version of the mentioned article.
- Once you clear with Onion Architecture Implementation, you will also need to be aware of CQRS MediatR Pattern. I have written a step-by-step guide on how to implement CQRS using MediatR Library. Read it here.
To get started / check out this Boiler Plate Template for Clean Architecture with ASP.NET Core WebApi, there are quite a few ways. Let’s go through them,
1. Using the Visual Studio Project Template
Download the Visual Studio Project Template from Visual Studio Marketplace from here – https://marketplace.visualstudio.com/items?itemName=MukeshMurugan.CleanArchitectureWebApi
Once Installed, Open Visual Studio and Create a New Project. You get to see our newly installed template. Use this Template to Create a new Project.
I created a new Project with the Name, “PointOfSales.Basic”. Here is the Solution Structure that Visual Studio sets up for you. It’s that easy.
After Visual Studio downloads the required packages, all you have to do is to open up appsettings.json, modify your connection string. With that done, Open up Package Manager Console and run the following commands.
That’s it. Now Build and Run your New Application!
2. Or, you can clone the Source Code
Navigate to the Project Repository – https://github.com/iammukeshm/CleanArchitecture.WebApi
Click on the Code -> Download ZIP, as mentioned below. This will download the entire source code for you locally to your machine (OR) Download the Latest Release from here.
Extract the Zip file and navigate to the Root of the WebAPI Project (Not the Solution File). In this directory, open up Power Shell and run the following commands.
Before proceeding do not forget to open up the appsettings.json and change the connection strings / email settings and so on. Note that you can use separation connection strings for both Identity and Application!
dotnet ef database update -Context ApplicationDbContext
dotnet ef database update -Context IdentityContext
dotnet run (OR) Run the Solution using Visual Studio 2019
When you run the application, Swagger Opens up. Here you can find the available endpoint.
Here is how the Solution Folder Looks like.
The Core Layers will never depend on any other layer. Therefore what we do is that we create interfaces in the Application Layer and these interfaces get implemented in the external layers. This is also known and DIP or Dependency Inversion Principle.
It is also important to note that these layers are .NET Standard 2.1 Libraries. This is so because we need to make these layers as comptatible as possible with other projects. Let’s say tommorow your client wants a .NET 4.7 Solutions (quite unlikely though) with the near-same business logics. At such scenarios, .NET Standard Libraries will be of great help.
All the Entities and the most common models are available here. Note that this Layer will NEVER depend on anything else.
Interfaces, CQRS Features, Exceptions, Behaviors are available here.
Whenever there is a requirement to communicate with an external source, we implement it on the Infrastructure Layer. For example, Database or other Services will be included here. To make the separation more visible, We will maintain further sub projects with the naming convention as ‘Infrastructure.xxxxxx’ where xxxxxx is the actual Point of Concern.
In this implementation, we will make use of the already AWESOME Microsoft Identity. Let’s seperate the User Managment Database from the Main Application Database. This is made possible by multiple – DbContext Classes in each of the required Infrastructure Project
An Application Specific Database will be maintained. This is to ensure that there is no relation between the DBContext classes of Application and the Identity.
Now, there are some services that are common to the other Infrastructure Layers and has the possibility of use in nearly all the Infrastructure Layers. This includes Mail Service, Date Time Service and so on. Thus it is a better Idea to have a shared Infrastructure project as well.
This is also known as the Presentation Layer, where you would put in the project that the user can interact with. In our case it is the WebAPI Project.
Support the Project
Your small contribution can help support the project and motivate me to make frequent updates to this Open Source Project. If you find this Project helpful or you learnt something from here, consider supporting.
Did you like this content? Found this article helpful? Consider Supporting by buying me a coffee.
Also, Leave a Star / Review on
Github – https://github.com/iammukeshm/CleanArchitecture.WebApi
Visual Studio Marketplace – https://marketplace.visualstudio.com/items?itemName=MukeshMurugan.CleanArchitectureWebApi
This would help me reach this project to the eyes of many more developers around the world! Thanks
- Onion Architecture In ASP.NET Core With CQRS – Detailed – We will talk about Onion Architecture In ASP.NET Core and it’s advantages. We will also together build a WebApi that follows a variant of Onion Architecture so that we get to see why it is important to implement such an architecture in your upcoming projects.
- CQRS with MediatR in ASP.NET Core 3.1 – Ultimate Guide – Let’s talk about CQRS in ASP.NET Core 3.1 and it’s implementation along with MediatR and Entity Framework Core – Code First Approach. I will implement this pattern on a WebApi Project. The source code of this sample is linked at the end of the post. Of the several design patterns available, CQRS is one of the most commonly used patterns that helps architect the Solution to accommodate the Onion Architecture.
There are a lot of References that I had taken in-order to build up this Solution. Few of them are Jason Taylor, Steve Smith , Nick, and many more. The eShop Repository Series of Microsoft has helped quite a lot.