Set Up 3-tier architecture in Asp.Net Core Web API Application

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:

Windows OS

Visual Studio Community 2019

Project Set-Up:

First of all, we will set up a web API project.

Open Visual Studio and follow the below steps.

Web API Application

Now name your project and select a location.

Web API Application

Select Version of .Net Core. We are working on the latest one so we will go with the .Net Core 5.

Web API Application

Now your screen will look like this.

Web API Application

After Setting up your project you have to add new projects to achieve multi-tier architecture.

Add 3 Class Library Projects:

  1. Services
  2. Model
  3. Data

Services:

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:

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.

Data:

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

Web API Application

After Clicking on Add new project. You will have a screen like this

Web API Application

Click on class library, Give a name to your project.

Web API Application

Now select the .Net Core version we are working on version 5 so I will go with .Net Core 5.

Web API Application

Repeat the above-mentioned steps and Add a total of 3 Projects.

Add Reference:

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.

Following below-mentioned.

Right-click on dependencies and click on Add Project Reference.

Web API Application

Now Select Service and press ok.

Web API Application

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.

Web API Application

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.

Web API Application

Now we are done with the references and now our 3-tier or multi-tier application is ready for the work.

Now our Application will look like this:

Web API Application

I will create a database using the Entity Framework Core code-first approach and implement Identity on this project in my next post. Will also continue with RESTful APIs. Stay Connected.

Leave a Reply

Your email address will not be published.