Detailed Explanation of the Three-Tier Architecture
Category Programming Technology
I've always wanted to make a more complete and perfect summary. But after much thought, I didn't know how to start. It's said that the beginning is the most difficult part of everything. Today, I organized my messy thoughts, but they are still not well organized. Let's just talk about whatever comes to mind.
Beginners often don't understand:
- What is the three-tier architecture?
- Why use the three-tier architecture?
- What is the difference between the three-tier and the two-tier architecture previously used? What are its advantages?
- How to learn the three-tier architecture well? How to apply it? ...
In this blog, I will explain each of these points for everyone, and I apologize if my understanding is superficial!!!
Teacher Mi always emphasizes: Combine learning with life, connect learning with life, such a learning is called learning well, living well.
For the three-tier architecture, I pondered how to connect it with reality. Well, I suddenly had an "inspiration" last night. Do you still remember the story of the big bird and the small vegetable eating lamb skewers in the chapter of "Big Talk Design Patterns" - which led to a command pattern (of course, today is not about studying the command pattern). Waiter, chef, and purchaser.
Isn't this a typical three-tier architecture? (⊙ o ⊙) Ah! Haha (this will be explained later)
First, let's understand:
1, What is the three-tier architecture?
UI (Presentation Layer): Mainly refers to the interface that interacts with the user. It is used to receive user input data and display the data that the user needs after processing.
BLL: (Business Logic Layer): The bridge between the UI layer and the DAL layer. It implements business logic. Business logic specifically includes: verification, calculation, business rules, etc.
DAL: (Data Access Layer): Deals with the database. It mainly implements the addition, deletion, modification, and query of data. It submits the data stored in the database to the business layer, and at the same time, saves the data processed by the business layer into the database. (Of course, these operations are all based on the UI layer. User needs are reflected to the interface (UI), UI reflects to BLL, BLL reflects to DAL, DAL performs data operations, and then returns one by one until the data needed by the user is fed back to the user)
Each layer is responsible for its own tasks, so how should the three layers be connected?
Single-direction reference (see the figure below)
At this time, the entity layer (Entity) comes in. (Note: Of course, the role of the entity layer is not limited to this)
Entity (Entity Layer): It does not belong to any of the three layers, but it is an indispensable layer.
The role of Entity in the three-tier architecture:
- Implement the "encapsulation" in the object-oriented concept;
- It runs through the three layers, transmitting data between the three layers; (Note: To be precise, the entity layer runs through the three layers to connect the three layers)
- For beginners, you can understand this way: each data table corresponds to an entity, that is, each field in the data table corresponds to the attributes in the entity (Note: Of course, in fact, it is not like this. Why? 1>, the entity we need may not exist in the entity corresponding to the data table; 2>, we can completely put all the fields of all data tables in one entity)
- The data transmission (one-way) between each layer (UI->BLL->DAL) is passed through variables or entities as parameters, thus constructing the connection between the three layers and completing the implementation of the function.
However, for a large amount of data, using variables as parameters is a bit complicated because there are too many parameters, which are easy to get confused. For example: I want to pass employee information to the next layer, including: employee number, name, age, gender, salary... If we use variables as parameters, then the parameters in our method will be many, and it is very likely that the parameter matching will be confused when using them. At this time, if we use entities as parameters, it will be very convenient, and we don't have to consider the problem of parameter matching. We can directly use any attribute in the entity, which is very convenient. This also improves efficiency.
>
(Note: Why is it said that you can temporarily understand that each data table corresponds to an entity? Answer: Everyone knows that the purpose of our system is to provide services