Three-tier structure development mode

Source: Internet
Author: User

18.2 three-tier structure development model

The three-tier structure is based on modularityProgramThe design concept is a standard model of modular division method gradually formed to meet the needs of decomposing applications. The advantage of the three-tier architecture is that you do not need to modify the entire program for minor changes in the business logic. You only need to modify one function or process in the business logic layer.CodeTo facilitate the cooperation between developers at different levels, as long as they follow certain interface standards, they can carry out parallel development, in the end, you only need to splice the parts to form the final application. This section describes the basic concepts of a three-tier structure and how to build a three-tier structure.

18.2.1 three-layer structure concept

The layer-3 structure is usually the data access layer, business logic layer, and presentation layer. The relationship between layer-3 structures is 18-2.

The presentation layer is located at the top layer. It is used to display and receive data submitted by users and provides users with an interactive interface. The presentation layer is generally a Windows form application or web application.

The business logic layer serves as a bridge between the presentation layer and the data access layer, and is mainly responsible for data transmission and processing.

The data access layer allows you to read, save, and update data.

 

In a layer-3 structure, layers depend on each other. The presentation layer depends on the business logic layer, and the business logic layer depends on the data access layer.

 

 

18.2.2 how to build a three-tier structure

 

This section describes how to build a three-tier structure through a simple login function.

1. Build a data access layer

(1) Open the VS 2005 development environment and select "file"> "new"> "project", as shown in 18-3.

 

(2) In the displayed "new project" dialog box, select "Visual Studio solution" as the project type and select "blank solution" as the template ". Enter the solution name "mybookshop" and specify the storage location, as shown in Figure 18-4.

 

(3) In Solution Explorer, right-click the solution name and choose add> new project from the shortcut menu, 18-5.

 

(4) In the open "new project" dialog box, select "Visual C #" as the project type and select "class library" as the template ". Enter the project name "mybookshop. Dal" to implement the data access layer. At this time, the project's storage location has been entered by default, which is the path generated when a blank solution is created, as shown in 18-6.

 

 

2. Build the business logic access layer

The steps for building the business logic layer are similar to those for building the data access layer. The difference is that you need to reenter the project name "mybookshop. BLL", as shown in 18-7.

 

3. Build the presentation layer

(1) In Solution Explorer, right-click the solution name and choose add> New website from the shortcut menu, 18-8.

 

(2) In the "Add new website" dialog box that appears, select "ASP. NET Website", select the location as "File System", and set the website path, as shown in 18-9.

 

In layer-3 structure development, the model layer is usually used. The model layer contains all entity classes corresponding to tables in the database. The presentation layer, business logic layer, and data access layer transmit object to achieve data transmission.

The procedure for creating a model layer is similar to that for building a business logic layer and a data access layer. The difference is that the project name must be "mybookshop. Models", as shown in 18-10.

 

In Solution Explorer, the basic framework of the layer-3 architecture is shown in 18-11.

4. Add dependencies between layers

At this time, although the basic framework of the three-layer structure has been successfully established, the layers are independent. Only by adding dependencies can they collaborate with each other.

(1) Add the dependency of the presentation layer on the business logic layer and model layer. In Solution Explorer, right-click the presentation layer and choose add reference from the shortcut menu, as shown in Figure 18-12.

 

 

 

(2) In the displayed "add reference" dialog box, select the "project" tab and select the project name "mybookshop. bll and mybookshop. click "OK" for two models projects, as shown in Figure 18-13.

(3) use the preceding method to add dependencies on the data access layer and model layer in the business logic layer, as well as the dependencies between the data access layer and the model layer.

 

The tutorial position of this part is 18-14.

 

So far, the three-tier structure and the dependency between the layers have been created. The following uses logon as an example to test how the layers work collaboratively.

 

1) Write entity class user. CS

In the model layer, rename the default class name class1.cs to "user. cs", which corresponds to the users table in the database bookshop. Some code in Entity class user. CS is as follows:

 
 

2) compile the data logging userservice

For each entity class in the model layer, the data access layer has a corresponding data entity class. For example, create a userservice class for the user object class to process data in the users table.

Add a query method in the userservice Class Based on the login name. The Code is as follows:

 

 

In the code above, the Data Handler class uses the dbhelper class, which contains common methods for database operations. Since the length limit does not list specific code here, you can viewSource code.

3) write business logic usermanager

For each entity class in the model layer, the business logic layer also has a corresponding class. For example, create a usermanager class for the user object class.

Add the Business Method Used for Logon verification in the usermanager class. The Code is as follows:

 
 

4) Compile the presentation layer

(1) Create the page login. aspx, as shown in the design view 18-15.

 

(2) double-click the "Log on" button to generate the Click Event and write the event handler in the login. aspx. CS file after the Code. The Code is as follows:

 

Before running the program, you must set the startup project. Right-click the presentation layer in Solution Explorer, and select "set as startup project" from the shortcut menu to set the presentation layer as startup project, as shown in Figure 18-16.

Run the program, enter the login name "admin", enter the password "123456", and click "Log On". The page jumps to default. aspx, And the logon is successful.

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.