Sunday, August 18, 2019

HOW TO SETUP 3 TIER ARCHITECTURE PROJECT IN ASP.NET C#

Description: 3-Tier architecture is also called layered architecture. In 3 tier architecture, the application development is broken into three main parts:
  1. Presentation Layer or Application layer or User Interface (UI). 
  2. Business Access Layer (BAL) or Business Logic Layer. 
  3. Data Access Layer (DAL).
This separation into layers ensures independent development of each layers and any changes in one layer doesn't affect the other layers.

Application Layer or Presentation Layer or user Interface (UI): It is the part of the 3 tier architecture with which the end user actually interacts. This layer contains pages like .aspx ( in case of web application) or windows form where data is presented to the user or input is taken from the user e.g. Registration form or any user input form. We can say that it is the front end of the application.

Business Access Layer (BAL) or Business Logic Layer (BLL) : This Layer is the class in which we write functions that accepts data from Presentation Layer and send that data to database through Data Access Layer. Presentation layers must not directly access the data access layers. So this layers acts as a intermediate between Presentation Layer and Data Access layer.

Data Access Layer (DAL) : This layer also contain classes that actually deals with the database and is responsible for performing database operation like insertion, deletion, updation and Binding etc.

DAL accepts the data from BAL and sends it to the database for processing or we can say DAL gets the data from the database and returns it to the business layer (BAL) to be sent to the front end.

We will also create a Business Entity layer (BEL) or we can say property layer having properties for each column in the table.   So if we have 5 columns in table then we declare 5 property/parameters in this layers so that we can set the value taken from UI in these parameters and then sent to DAL via BLL. 

For example, from the form in application layers, we take the user input and put those values in parameters declared in BEL and then they will be passed to the DAL through the BLL. 

Main benefit of creating this Entity layer is that we declare the properties/parameters only once in this layer and these properties/parameters can be accessible throughout the project by creating the object of BEL class.  Don't get confused, it will be cleared when we will implement it.

Implementation: Let's implement the concept of 3 tier web application.

Launch Visual Studio -> File Menu -> New -> Project -> go to Other Project Types -> Select Visual Studio Solutions and Name it "3TierBookApp" or whatever you want as shown in image below.

3 tier architecture in asp.net
Click on image to enlarge
Adding Presentation layer
In Solution Explorer, right click on the solution just created -> Add -> New Project. A dialog box will appear. Select the language Visual C# -> Web -> ASP.NET Empty Application. Name it "Presentation_BookApp" or whatever you want as shown in image below.

3 tier architecture in asp.net
Click on image to enlarge

Adding Business Entity Layer (BEL)
Again right click on the solution in the solution explorer -> Add -> New Project -> Visual C# -> Class Library -> Name it "BEL_BookApp" or as whatever you want as shown in image below 

3 tier architecture in asp.net
Click on image to enlarge

Adding Business Login layer (BLL)
Again right click on the solution in the solution explorer -> Add -> New Project -> Visual C# -> Class Library -> Name it "BLL_BookApp" or whatever you want as shown in image below.

3 tier architecture in asp.net
Click on image to enlarge

Adding Data Access Layer (DAL)
Again right click on the solution in the solution explorer -> Add -> New Project -> Visual C# -> Class Library -> Name it "DAL_BookApp" or whatever you want as shown in image below

3 tier architecture in asp.net
Click on image to enlarge

That's way we have configured our 3 tier architecture. It will look like as shown in image below.
3 tier architecture in asp.net
Click on image to enlarge
Remove the default class1 from all the BEL, BLL and DLL layers. We can create our own classes as per application requirement.

Adding required References
Now we need to add reference of BEL and BLL in the Presentation layer so that presentation tier can communicate with BEL and BLL.  So right click on the "Presentation_BookApp" in the solution explorer -> Add Reference -> A dialog box will appear. Select BEL and BLL as shown in image below.

3 tier architecture in asp.net
Click on image to enlarge

Similarly add reference of BEL in DAL using the same process as mentioned above. And also add reference of BEL and DAL in BLL. We have setup our 3 tier project. But actual implementation will be clear when we create an application using this setup. 

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...