The multilayer architecture of ASP. NET is mainly to solve the relationship between data layer, logical layer, presentation layer, etc. My approach is this: first build a datacore base class. The base class encapsulates the basic operations of some low-level databases, such as database joins, calling stored procedures, and so on.
Many people find it difficult to develop multi-tier applications. Take a look at an example: for a small company with only one or two people, a person may be at the same time as a boss, cashier, accounting, marketing, sales, development and many other jobs. And for a large company, will be a more rigorous division of labor, each person only to complete part of the work, need to cooperate with each other to ensure normal operation. The previous development program is similar to a small company, from the user interface to database access, all functions are completed in one page, the disadvantages are:
1. Difficult to develop, it is difficult to achieve multi-person collaborative development
2. Once the database or rules have changed, you may want to re-modify the entire page, increase maintenance costs
3. Because all functions are mixed together, the program reusability is poor. If you develop a new project, you almost rewrite the code
In order to solve this problem, people put forward the concept of "multi-layer Application", which is similar to a large company with clear authority, which divides the pages, and puts the functions of data access and business rules into special files. The more popular are two-tier architecture, three-tier architecture, and MVC.
I. Two-tier architecture
The second-tier architecture is to divide the program into user interface layer and data access layer. The essence is to put the code that accesses the database into the data access layer, and the user interface layer operates the database through the data access layer. The interaction relationship is as follows: ("<--->" for two-way arrows)
User Interface <---> data access <---> database
Two. Three-tier architecture
A three-tier architecture that separates the business logic in the two-tier architecture from the data access layer and becomes a separate business logic layer. When the program is divided into three layers, the data access layer is just working on the database, and the business logic layer is responsible for processing the data in various ways.
The top level consists of 4 parts: the DAL (data processing layer), the BLL (business Logic Layer), the UI (user interface layer), the model (the entity model). The first three are the three-storey structure that people often say.
1) Data access layer (Database access Layer,dal): sometimes also known as the persistence layer, its function is mainly responsible for database access. The simple idea is to implement the select,insert,update of the data table, delete operation. If you want to add an ORM element, it will include the mapping between the object and the data table, and the persistence of the object entity;
2) The business logic layer, which is the core of the entire system, is related to the business (domain) of the system;
3) Presentation layer (user interface layer, UIA): Is the UI part of the system that is responsible for user interaction with the entire system. In this layer, the ideal state is that the business logic of the system should not be included. The logic code in the presentation layer is only related to the interface element;
4) Entity Model layer: Contains all of the data information that exists in the form of various entity instances. Is the basic level of the whole system;
The perfect three-tier structure should be: Modify the presentation layer without modifying the logical layer, and modify the logical layer two without modifying the data access layer. To a certain degree of decoupling.
The three-tier architecture is mainly to make the project structure clearer, the division of labor is more clear, conducive to later maintenance and upgrade. It solves the problem of code encapsulation at different stages of the business operations throughout the application, allowing the programmer to focus more on the business logic at a certain stage of the process. However, the performance may not be improved because the main program module can only wait when the subroutine module is not executed at the end. This means that dividing the application hierarchy will result in some loss of execution speed. But from the perspective of Team development efficiency, you can feel a very different effect.
It is important to note that although the three-tier architecture has many benefits, if your program is simple, or will certainly not be reused in the future, or not necessarily adopt a two-tier architecture, it may be faster to develop two-tier or normal programs. Should be dealt with according to the actual situation.
Three. MVC
M, model, is responsible for the interaction of business logic and database;
V is the view layer, which is mainly used for displaying data and submitting data;
C is the controller, which is mainly used to capture the request and control the request forwarding;
MVC is a few modules of different functions that are divided in the view layer of the application (BS structure), mainly to solve the style substitution problem of the application user interface, and to separate the HTML page of the presentation data as possible from the business code.
Four. The difference between a three-layer structure and MVC
The difference between the words, look at the picture can understand:
Figure 2. The difference between MVC and three-tier architecture
The three-tier architecture is composed of the interface layer (UI) business Logic Layer (BLL) and the data Access Layer (DAL), which is composed of the model layer (M) interface layer (View) and the control layer (Controller), and they do not correspond to each other.
If you want to give them a match, then the UI in the three-tier architecture corresponds to the view in MVC, which is used to display and retrieve the interface, and the BLL layer and the DAL layer in the three-tier architecture correspond to the model layer in MVC, which is used to process the data passed from the top and the data obtained from the database. , the controller in MVC is part of the UI in the three-tier architecture.
Five. Three-tier architecture reference relationship
Model layer: Do not refer to any project;
DAL layer: Referencing the model, by reading the assembly in Web. config, loading the instance of the class, and returning it to the BLL;
BLL layer: Citation model,dal;
UI layer: Reference Model, BLL;
You do this by right-clicking the project file in the explorer and adding a reference. Select the Item tab in the Popup dialog box and click OK after selecting the appropriate class library. Then add the using "Reference class namespace" in the project file.
The project has added a reference, but still cannot find the specified class library file, check:
1. If there is a syntax error in the referenced project, whether to add a using "namespace" to the header file;
2. When the class library is added, whether the class library is public.