The project we are about to set up is a 3-tier architecture in Asp.Net Core Web API Application and using the .Net core 5 version.
In this tutorial, we will create a main front-tier Asp.Net Core Web API project and create the other 3-tiers for Services, Model, Data. All these tiers and project is completely explained below.
Tools that I’m using:
First of all, we will set up a web API project.
Open Visual Studio and follow the below steps.
Now name your project and select a location.
Select Version of .Net Core. We are working on the latest one so we will go with the .Net Core 5.
Now your screen will look like this.
After Setting up your project you have to add new projects to achieve multi-tier architecture.
Add 3 Class Library Projects:
In this project we will create 3 projects one project for services, we will implement our business logic in services and call the services from our main API project to get the tasks done. Services will interact with the data and perform the operations on data and return the response to Apis. Our Main Api project will not directly interact or link with our database.
Model so that we can add our database classes to it. We are using Entity Framework Core so after making our data classes in Model using the code first approach we will run the migration and our database will be generated. In the Model layer, we will also put our ViewModels and create models for API responses.
In Data Layer We will put our ApplicationDbContext Class, Our Seeding Classes, and constants if our application does have any. Migrations will also be in this layer.
For adding a new project click on File -> Add -> New Project
After Clicking on Add new project. You will have a screen like this
Click on class library, Give a name to your project.
Now select the .Net Core version we are working on version 5 so I will go with .Net Core 5.
Repeat the above-mentioned steps and Add a total of 3 Projects.
Now add a reference to the other projects so we can work with these tiers.
Add a reference in your Main Api project to the services. Because We will call our services through Apis and our services will have all the business logic, not our front API.
Right-click on dependencies and click on Add Project Reference.
Now Select Service and press ok.
Now in the Services project add a reference to Model and Data project like this. Because we will manipulate data and perform operations on data through services. Without adding the reference we will not be able to do this.
Now in Data Project add a reference to Model Project. Because Model Layer contains all our data classes/Model. And with reference, we will use it in our ApplicationDbContext.
Now we are done with the references and now our 3-tier or multi-tier application is ready for the work.