A brief talk on the practical skills of multilayer architecture in asp.net

Source: Internet
Author: User

Many people feel certain difficulties in developing multi-tier applications. Look at an example: for a small company with only one or two people, a person may also be a boss, cashier, accountant, marketing, sales, development and many other tasks. And for a large company, will be a more rigorous division of labor, each person to complete a 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 and other functions are completed in one page, such shortcomings are:

1. It is difficult to develop, it is difficult to achieve collaborative development of many people

2. Once the database or rules have changed, it may be necessary to modify the entire page, increase maintenance costs

3. Because all functions are mixed together, program reusability is poor. If you are developing a new project, you will almost rewrite your code

In order to solve this problem, people put forward the concept of "multi-layer application", which is similar to a large company with a clear mandate, the Division of the page, the data access, business rules and other functions are placed in a special file. More popular are two-tier architecture, three-tier architecture, and MVC.

I. Two-tier architecture

Two-tier architecture is to divide the program into user interface layer and data access layer. The essence is to put the access to the database code into the data access layer, and the user interface layer through the data access layer to the database operation. The interacting relationships are as follows: ("<--->" means two-headed arrows)

User Interface <---> data access <---> database

Two. Three-layer architecture

The three-tier architecture separates the business logic in the two-tier architecture from the data access layer and becomes a separate business logic layer. After dividing the program into three layers, the data access layer operates on the database, and the business logic layer is responsible for all kinds of data processing.
There are 4 main components from the top layer: DAL (data processing layer), BLL (business logic Layer), UI (user interface layer), model (entity model). The first three are the three-storey structure that people often say.
1 The Data Access layer (Database access Layer,dal): Sometimes referred to as the persistence layer, its function is primarily responsible for database access. The simple argument is to implement the select,insert,update of the datasheet, delete operation. If you want to add an ORM element, you will include the mapping between the object and the datasheet, and the persistence of the object entity;
2 The Business Logic layer (business logic layer, BLL): It is the core of the whole system, it is related to the business (domain) of the system;
3) Presentation layer (user interface layer, UIA): Is the UI part of the system, responsible for the user's interaction with the entire system. In this layer, the ideal state is not to include the system's business logic. The logical code in the presentation layer is concerned only with the interface element;
4 Entity Model layer: Contains all the data information, which exists in the form of various entity instances. is the whole system foundation level;

The perfect three-layer structure should be: Modify the presentation layer without modifying the logical layer, modify the logical layer two without modifying the data access layer. To a certain degree of decoupling.

Three-tier architecture is mainly to make the project structure clearer, more clear division of labor, conducive to later maintenance and upgrades. It solves the problem of code encapsulation at different stages of the business operation throughout the application, and allows programmers to focus more on the business logic of a particular phase. However, performance may not be elevated because the main program module is only waiting when the subroutine module is not finished. This means that dividing the application hierarchy can result in some loss of execution speed. But from the team development efficiency point of view but can feel a very different effect.

It's important to note that while the three-tier architecture has many benefits, if your program is simple, or will certainly not be reused in the future, or not necessarily with a two-tier architecture, it may be faster to develop two or more common programs. According to the actual situation of specific treatment.

Three. MVC

M is model layer, mainly responsible for the business logic and database interaction;
V is the view layer, which is used primarily for displaying data and submitting data;
C is the controller (Controller), mainly used as a capture request and control request forwarding;

MVC is a few modules of different functions divided in the view layer of the application (BS structure), mainly to solve the problem of style substitution of the application user interface, and to separate the HTML pages of the presentation data as much as possible from the business code.

Four. The difference between three-layer structure and MVC

The difference, look at the picture can understand:

Figure 2. The difference between MVC and three-tier architecture

The three-tier architecture is composed of interface layer (UI) business Logic Layer (BLL) and data Access Layer (DAL), and MVC is the Model layer (M) interface layer (View) and Control layer (Controller), and they do not correspond.
If you want to give them a response, then the UI in the three-tier architecture corresponds to the view in MVC, which is used to display and obtain the data of the interface; the BLL layer in the three-tier architecture and the DAL layer correspond to the model layer in MVC, which is used to handle data from the upper layer and data obtained from the database. The controller in MVC is at best part of the UI in the three-tier schema.

Five. Three-tier schema reference relationships

Model layer: Do not refer to any project;
DAL layer: Reference Model, by reading the web.config in the assembly, loading class instances, return to BLL use;
BLL layer: quoting Model,dal;
UI layer: Reference Model, BLL;

You add a reference by right-clicking the project file in Explorer. Select the item tag in the pop-up 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 to check:
1. Whether there is a syntax error in the referenced project, and whether to add a using "namespace" to the header file;
2. When adding a class library, whether the class library is public.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.