Introduction:
The three layer or the three tier Architecture in c# is an architecture or design pattern, which is based upon the three layers, the Data layer, the Business logic layer and the presentation layer.
The graphical representation in the below image illustrates it further,
The three tier Architecture principle defines three basic layers in an application, which are actually responsible to perform specific tasks or operations, The Data Access Layer deals with all the database related operations which includes all the crud operations along with making a request to perform any query or a database operation.
The Business Logic Layer or the Business Access Layer is the central layer of this principle, which acts like a bridge between the two other layers and is mainly responsible for to receive the requests from the presentation layer and forwards it to the data access layer, by enlarge the business access layer separates the entire dependencies of modules with in the application.
The Presentation Layer is also known as the View and it is actually the layer which completely separates its self from any database related or any business related calculations or computation, it actually decouples the modules of the application, as it since becomes responsible to forward the business related or database related tasks to the business layer, which then sends it to the model or the Data Access layer, the data access layer after making a database operation responds back to the business layer which in reply backtracks the result to the presentation layer and then the presentation layer displays the desired result to the end user.
As the above image shows that there is one layer, which is common among the other three layers is called the Business Entity Layer, or BEL, we use this layer to initializing the public c# properties which are used to map the class properties on the database fields or the columns, and are reusable through out the application, this layer is spread over the entire three tier Architecture.
Advantages of Three Tier Architecture:
The architecture clearly decouples the modules and the dependencies among the components within an application, with decoupled components, the chances of changing one section of the code enforces the other section to change became very less,
The code pattern then seems to be in a much better shape and cleaned, further on the architecture also minimizes too much of a structured or mix coding, each layer does its own job makes it a more formidable architecture principle in C#.
Here, we will consider an example with a Three tier Architecture Principle in a console based application, since the principle is more known to be used in Windows Forms Application Or in a Web Forms Application in Asp.Net, but here we will fetch the data from database using this concept of three tier architecture in order to understand the work flow.
We will create three different Projects in this console based C# application to demonstrate the flow of three tier architecture and to fetch the data from the database, in our previous article, we fetched the data in a more conventional way from a database table Students but here we will do the same using this three tier architecture principle.
Step 1:
First up create a new console based application project in Visual Studio 2017, we will name this project as ThreeTierExample.
Step 2:
Now lets create two more projects but remember, this time two Class Library projects are going to be created in Visual Studio 2017, we will name them as BusinessAccessLayer and DataAccessLayer and the BusinessEntityLayer respectively.
Step 3:
So, now we have 4 projects we have just created in this current solution, 3 of them are the Class Library projects and 1 of them is out main C# console based application, which happens to act as a presentation layer for us in this current scenario.
Step 4:
Renaming the class library projects which we have just created, and add references, we will references into the Presentation layer which is our main console application and the business layer which is the BusinessAccessLayer class library project and to the BusinessEntityLayer project, so to add a reference, right click the class library project and click add-reference and select the projects option from the sidebar.
Step 5:
Add this code to the BEL.cs or to the business access layer, here we are creating that reusable student class which we will access across the application.
Step 6:
Add those references as being shown in the above images, it clearly shows the dependencies between the presentation layer and other layers.
Now its time to code inside the DAL.cs to fetch the data from the SQL server database,
DAL.cs
Step 7:
Now, add this code into BAL or the business access layer.
BAL.cs
Here we are just calling the method of the Data Access layer to get the students data.
Step 8:
Here, we are just going to add the code for the presentation layer, which in this case is our main console application,
So here we import the required namespaces and called the method of business access layer and we have seen that we have just separated the code logic and the way to access the database from the presentation layer, the actual code has been abstracted in a way that it did not mixed up with the rest of the application, In this way we can implement this architecture principle, which does enhance the application's performance and displays the code in a more neater and cleaner way.
Just press crtl+f5 to run the application to see the output window.