C# N-Tier Architecture

Today, when I talk about a layered architect, I will go through an example at the same time. I will go through a sample frequently used in almost every major project.

Layered Architecture is a structure that enables our projects to be more compact, improve code readability, increase teamwork, and make error management easier. In fact, with this structure we have made the project writing a standard. This structure can be considered as a multi-layered architectural structure as it is formed from 3 main layers. But it actually stops on 3 main layers. These layers are;

  • Data Layer
  • Business Layer
  • Presentation Layer

Now let’s go on to explain these things.

Data Layer

This layer is also known by its name and we will deal with the database part. Database connections are made in this layer and classes are created for the tables in the database. Of course, we need to have our database for this. A sample database is available for this. I will use the Northwind database.

After adding the database, we will go to the code. For this we need to make a database connection first. After we write our link address, we open the link. Then we need to create a query to retrieve the data. We can extract data from different tables. That’s why we create such a function.

We performed the database bind operations. We also made preparations to write queries when needed. Now we are going to work on which table in the database. If you are going to deal with all the tables, you have to add all the tables. I am adding this only because I will deal with the customer chart. I am adding this column to the database in the same way as it would be here. You can easily create it by typing “prop“. I later add a constructor by creating a Constructor.

Business Layer

In this layer, the main events begin. How we read the data from the database, what questions we will read, how we read the data, what we will do after reading the data. In fact, it provides a bridge between our database and our project.

In this example I will just do data extraction. If you want to do other things later, we can go together. After creating a function called data check, I create a list so that I can keep the data in a list. Then I get the linking and query writing functions in the data layer to open the connection and query that we created in the data layer. Then I read the data. After I read the data, I am assigning a class called Customer. I add a customer list afterwards. That’s all my business here. You can perform basic CRUD (Create – Read – Update – Delete ) operations on this layer.

Presentation Layer

In this layer, we present data to the user in our database. Dilerseniz mobile application, you do not want to do a console application in the windows form application. This is up to you. This is the layer where you will have more pages. We will reach the user in this layer. The user will start the data extraction process from here and will do basic operations here. I did a simple bit console application in this application, but as I said, you can do whatever you want.

Here I first get help from my class at the Job Layer to take the data. We already did everything we needed to attract data at the business layer. I’m making a list. This listenin type will be in Customer style. To retrieve the Customer list from the business layer. Then we get the customers coming from the business layer to this list. We need to go through this list after we get it. We navigate with Foreach and print it on the screen. That’s our process.

Our display is as below.

The reason for the layered architecture is that each process is performed in different layers. I went through this project in one project and stored them in different folders. You can create different projects for each layer you are baking.

If you want to reach the project you can reach here.

If you have any questions, you can contact me by email or comment.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.