Children like to ask what love is, but adults do not know.
User Interface presentation layer (UI)
Business logic layer (BLL)
Data access layer (DAL)
The so-called three-tier architecture is the most popular topic for white professionals. I think that I am very professional, but I am very professional. No one can explain it clearly.
So what is Layer 3?
Are you on Layer 3 today?
I have seen a lot of white people. I like to write three project files (or three folders) in the Code and name them model, BLL, and Dal,
In Dal, tsql and SP are used,
Bll only calls a single Dal method, for example
Class BLL
Showproducts ()
{
Dal. getproducts ();
}
And so on,
It is completely for three layers, but I don't know why it is for three layers. I didn't give my son the name of the chairman, so I can enter the main Zhongnanhai. The name is just a code name.
I don't know what BLL is doing.
The role of BLL is to follow the rules set by our business and execute the process according to a user's instructions,
Organically associate and constrain entities returned in the Dal,
Then
Or save or present it to the user.
A program is far more than three layers, such
Serialize the result to JSON or XML (used for Ajax Communication With WebService or WebService)
Dependency injection IOC container (for object sharing and persistence)
Entity Framework to be on the stage of history
They all leave the business logic aside and do not participate in tsql. (Entity Framework converts General Entity SQL queries into database query encapsulation)
Entity SQL statements must be written by yourself, and this SQL statement set will become dal
There are also a lot of frameworks that walk between different layers. They are independent of the business, and no matter how low the performance of SQL statements we write, they just complete their own functions.
What layer do they belong?
The solution (framework) is independent of the business, so that it can be layered as a general solution (not a layer-3 ),
Do not create three layers for three layers.
A well-known ERP software developer, whose software operation process is
User button (UI Layer) ---> execute 1000 ~ 5000 rows of statement stored procedures (Database Operations) ----> result filled to dataset (their names are VO) -----> presented to the customer (UI Layer)
Since famous enterprises only have two layers (of course, they are lazy and criticize them here .)
Why are we so disdainful to those who do not strictly abide by the "three-tier law" (or three-tier justice.
Weak Coupling implemented through interface abstraction is essential for every basic solution (framework or function set), not only between layers.
Write so much, To be continued ...............
Today, are you still on Layer 3?
The following are my most frequently-read articles on L3 architecture (I do not recommend that you follow this example)
Code
Bll separates usl from dal and adds Business Rules
Role of each layer
1: Data access layer: mainly refers to the operation layer for raw data (in the form of storing data such as databases or text files), rather than raw data, that is, it refers to data operations, instead of databases, it provides data services for the business logic layer or presentation layer.
2: business logic layer: This layer is mainly used to address specific problems. It can also be understood as a pair of operations on the data layer. If the data layer is a building block, the logical layer is the construction of these blocks.
3: Presentation Layer: It mainly indicates the Web mode or winform mode. The Web mode can also be represented as Aspx. If the logic layer is quite powerful and complete, the logic layer provides complete services regardless of how the presentation layer is defined and changed.
Specific differentiation methods
1: Data access layer: it mainly depends on whether your data layer contains logic processing. In fact, its functions mainly perform operations on data files. You do not have to worry about other operations.
2: business logic layer: mainly responsible for operations on the data layer. That is to say, some data layer operations are combined.
3: Presentation Layer: This layer mainly accepts user requests and returns data to provide client access to applications.
Layer-3 structure explanation
The layer-3 architecture adds an intermediate layer between the client and the database, also known as the component layer. The layer-3 system mentioned here does not refer to the layer-3 physical architecture, instead of simply placing three machines as the layer-3 architecture, or not just B/S applications as the layer-3 architecture, layer 3 refers to the logic layer 3, even if these three layers are placed on one machine. Applications in the layer-3 system put business rules, data access, and legality verification into the middle layer for processing. Normally, the client does not directly interact with the database, but establishes a connection with the middle layer through COM/DCOM communication, and then exchanges with the database through the middle layer.
Developers can place the business logic of an application on the application server in the middle layer and separate the business logic of the application from the user interface. Provides a simple interface while ensuring the client functions. This means that if you need to modify the application code, you only need to modify the intermediate application server, instead of modifying thousands of client applications. This allows developers to focus on the analysis, design, and development of the core business logic of the application system, simplifying the development, update, and upgrade of the application system.
So why should we apply the "intermediate business layer? For example:
Assume that there is a piece of login code, you can process the web program in this way. The appearance layer is responsible for receiving the data on the front-end page, and then passing it to the middle layer. The middle layer processes the data, such as formatting, anti-SQL injection and so on. Such data is then transmitted to the data access layer and then operated with the database, such as matching the database user name and password and other code.
The "intermediate business layer" has many functions, such as verifying user input data and caching data read from the database ...... However, the actual purpose of the "intermediate business layer" is to combine the basic storage logic of the "data access layer" to form a business rule. For example, "a shopping website has such a rule: the system automatically registers users who shop for the first time on the website ". Such business logic is most suitable in the middle layer:
In the "data access layer", it is best not to see any "business logic "! That is to say, to ensure the atomicity of function functions in the "data access layer! That is, the minimum and no score is allowed. The "data access layer" only stores or reads data.
Three-layer structure in ASP. NET
The perfect three-tier structure requires that you modify the presentation layer instead of the logic layer, instead of the data layer. Otherwise, it is hard to say whether your application has a multi-layer structure, or whether there is a problem with the Division and organization of the layer structure. Different applications have different understandings. This is just a conceptual question.
Understanding the three-tier structure in ASP. NET-why should we divide it into three layers?
We use a three-tier structure to make the project structure clearer and the division of labor clearer, which is conducive to later maintenance and upgrade. It may not improve the performance, because when the subprogram module is not executed, the main program module can only be in the waiting state. This shows that dividing an application into layers will cause some loss in its execution speed. However, from the perspective of team development efficiency, we can feel a big difference.
It should be noted that the layer-3 structure is neither a. Net patent nor a technology dedicated to databases. It is a more universal architecture design concept.
In this architecture, we need to pay attention to the relationship between tables in the database design, and try our best to satisfy the relationship between the master and the child. Users must have certain functional limitations. do not exercise caution when deleting sub-tables, in this case, the foreign keys of the primary table that are logically present in the data of the primary table and the sub-table do not have the corresponding values in the sub-table.
The following table query methods are used:
First, query the master table and call the DL corresponding to the master table. Query each sub-table based on the records of the master table. A large query set is formed after the query result of the Self-table is added to the master table.
Operations on tables (add, delete, and modify ):
In this case, only the primary table is operated and the operation method in the DL corresponding to the primary table is called.
The RL layer is the logical judgment layer, mainly used to perform logical judgment on the data uploaded to the page. Above the RL layer is the UI
How to build a three-tier architecture solution
Create a blank solution. Then:
"Add"-"New Project"-"Other Projects"-"Enterprise template project"-"C # generate blocks"-"Data Access" (data layer, hereinafter referred to as layer D)
"Add"-"New Project"-"Other Projects"-"Enterprise template project"-"C # generate blocks"-"Business Rules" (business layer, hereinafter referred to as layer C)
"Add"-"New Project"-"Other Projects"-"Enterprise template project"-"C # generate blocks"-"Web User Interface" (interface layer, (U layer)
Right-click solution> Project dependency, and set u to depend on D, C, and D.
Add references D and C to U, and add references d to C.
So far, a three-tier shelf has been established. What I said above is very specific and "stupid". People who know me think I am nonsense. In fact, during this time, I strongly felt that many people actually did not understand this simple process. Although there is no objection to creating two "Empty Projects" and one "asp net Web Application Project", it can also be used as a three-layer framework, many people think that these "enterprise-level template Projects" are actually empty projects, which is a misunderstanding. Yes, you can see from the solution resource manager that there is nothing in the enterprise-level template project. But you can open the project file in notepad and see the difference ?? You can't see some things behind the scenes, but the system is ready. That is to say, if you "using system data sqlclineit" in a class in the C layer or use a sqlconnection object, no errors will occur during compilation, however, some "policy warnings" will be generated in the "task list" to warn you not to put things that should be placed on layer d in the C layer (although the program is correct, but the readability and maintainability are discounted.) This function cannot be provided to empty projects.
In the new tracelword3, the "enterprise-level template project" is applied ". Place the original lwordtask. CS in a single project named accesstask. In the solution, a new project named interservice is created, which contains an lwordservice. CS program file, which is the "intermediate business layer" program. To avoid repeated names, the tracelword3 website is placed in the webui project. For more complete code, you can find it in the codepackage/tracelword3 directory --
Combination of face object and reality
We know that building a bridge requires bricks. We should first prepare bricks and then build the bridge. However, in order to explain the sequence and consistency, it is simple. We should first build a bridge and reproduce the bricks during the construction process, so that there will be no more "things not needed by the bridge ". In practice, you should prepare bricks first.
The U layer is actually a bridge, the C layer is a brick, and the D layer is a raw material (stone, sand ). This also explains why the U layer references and depends on the D Layer (instead of the U-to-C and C-to-D layers), because the bridge requires both bricks and stones.
Disadvantages of "three-tier structure"
Some netizens raised some questions after reading this article, reminding me that the article has not mentioned the disadvantages of "three-layer structure. The word "three-layer structure" seems to have been popular all the time. The reason may be that this development mode is widely used. However, the "three-layer structure" is not a hundred trials of the "Wan Ling medicine", it also has shortcomings. Let's talk about its shortcomings ......
One obvious disadvantage of the "three-tier structure" development model is that it does not run fast enough. Of course, this "execution speed" is relative to non-hierarchical applications. According to the sequence diagram given in sequence, this disadvantage is also obviously exposed. Tracelword1 and tracelword2 are not layered, And the classes provided by ADO. NET are called directly to obtain data. However, tracelword6 does need to be called multiple times to obtain data. When the subroutine Module Program does not return, the main program module can only be in the waiting state. Therefore, the higher the message board version, the lower the ranking. The "three-tier structure" development model is not applicable to systems that require too much execution speed, such as online ticket booking, online stock trading, and so on ...... It is better at systems where business rules are easy to change.
The "three-tier structure" development mode is difficult to get started and difficult to understand and learn. This is intended for beginners. The amount of code for software developed in this mode is usually slightly higher. This will often overwhelm beginners in the vast amount of code. It is understandable that you are afraid of it ......
In fact, no matter which development mode or method it is, there are advantages and disadvantages. There is no "Universal Method" to solve any problem. Therefore, the word "three-layer structure" is not an exception! Whether to use this mode for system development requires comparison and trade-offs. Do not abuse it!