Three-layer design model for ASP. NET Applications (learning)

Source: Internet
Author: User

Review ASP

ASP (Active Server Page)Functions:

You can embed server-side scripts compiled by VBScript into web pages to dynamically generate page content on the server;

The COM component can be used to connect to the database to provide powerful transaction processing functions.

ASPDisadvantages:

(1) because the control logic written in scripting language needs to be embedded in HTML tags, the development efficiency of ASP pages is low and it is difficult to maintain them in the future;

(2) the dynamic part is written in scripting language, and its functions are restricted, making it difficult to develop complex programs;

(3) programmers must maintain data transmission between pages themselves;

(4) It is difficult for ASP to develop programs using object-oriented methods;

About ASP. NET

The webform programming model can be used. The underlying system automatically performs tedious interaction between the client and server, and provides the status management function, page data can be automatically maintained between different page requests.

About B/SStructure

Unlike the traditional C/S (ciient/Server) structure, most of the functions in the B/S structure are implemented on the server side. The server sends a web page to the user's browser after calculation and Processing Based on the requests sent by the user's browser. The page consists of standard HTML text and JavaScript client scripts.

ASP. NETThree-tier Development and Design Model

Advantages of layered models:

Each layer only implements relatively independent functions. When any layer changes, as long as the interface relationship between the layers remains unchanged, other layers will not be affected.

(1)User Interface Layer(User interface tier ):

Displays the data information dynamically transmitted by the business logic layer, and is implemented by using the corresponding HTML Tag and CSS (cascade style sheet) mode. At the same time, it is also responsible for the acquisition and verification of user input data and transfer it to the business logic layer.

(2)Business logic layer(Business logic tier ):

Provides function calls for the user interface layer, and also calls the functions provided by the data access layer to access the database. It also constructs several key objects in the project based on the system design to implement most of the logic control functions in the project.

(3)Data access layer(Data Access tier ):

It is mainly used to achieve interaction with the database, that is, to complete the query, insert, delete and modify functions. It extracts or modifies data from the database according to the requirements of the business logic layer. Database access is the most frequent and resource-consuming operation in the system. Therefore, database access needs to be optimized to improve system performance and reliability.

BBSEngineering instance

The entire project is divided into four functional modules:

Directory management (directory): implemented using a "Tree" structure;

Article Management (Article): stores the article in the corresponding directory;

User Management: User Management and authorization;

Permission management (right): authorization is refined to directories at all levels and each article;


The system uses the SQL Server 2000 enterprise database, Visual Studio development tools, and C # language.

The entire project is a solution, and each layer in the layered model is a project ), each project corresponds to its own namespace, and each project belongs to the solution.

This project includes four projects, where the Web, bussinessfacade, and dataaccess projects correspond to three layers of the design model, and a project Common is used to define the data interfaces between layers. Structure:

(1)WebProject

The web project corresponds to the "user interface layer". In this layer, the display part of each webform page is stored in the aspx file.

(2)BusinessfacadeProject

The businessfacade project corresponds to the business logic layer, which includes the definition of Four "classes:

Directorysystem, articlesystem, usersystem, and rightsystem correspond to four functions of the system.

For example, the following is provided in the rightsystem class:

Checkdirectoryright Method Used for directory permission authentication;

The filterdirectorylist method used to filter the directory list;

Authorizeuser Method Used for user authorization;

And other member functions used for permission management. These member functions can be called directly at the user interface layer.

In this layer, you must introduce the dataaccess project namespace and use the functions provided by the project to access the database.

(3)DataaccessProject

The dataaccess project corresponds to the "data access layer", which also includes the definition of Four "classes:

Directoryaccess, articleaccess, useraccess, and rightaccess. The member functions of each class access the corresponding Stored Procedure (Stored Procedure) in SQL Server according to the requirements of the business logic layer ).

For example, the loaddetailbyid method in the articleaccess class is used to call the getarticledetailbyidstatus stored procedure in SQL Server, so as to obtain the detailed information of the article through the Article ID and status.

Program. net on the CLR platform. Generally, garbage collect is used by the system to eliminate objects that are no longer used. However, database access consumes a lot of system resources, so the four classes at this layer are derived from system. the idisposable interface (referenced using the using statement) allows you to release the occupied resources in a timely manner when you do not use this layer object.

(4)CommonProject

In order to provide a unified data interface for data transmission between layers, a common project is developed in addition to the corresponding projects of the three layers. Four classes are defined in this project:

Directorydata, aricledata, userdata, and rightdata. These classes are derived from the system. Data. dataset class. In each dataset, some datatables are defined to store the corresponding data in a fixed format.

For the classes defined in the Web, businessfacade, and dataaccess projects, the parameter and return value types of the member functions can use the classes defined in the common project, these classes are the standard interfaces for data transmission between different layers.

Additional code:

In ASP. net support, using ADO. net can easily access web-based databases, regardless of whether the data source is a relational database, a non-structured database, a text Database (such as an XML file), or a table database like Microsoft Excel, you can use ADO.. net.

To implement remote databases, you must integrate ADO with RDS to achieve high-performance and high-reliability remote operation.

The update code of the DataGrid Control is as follows:

Void editcommand (Object sender, datagridcommandeventargs E)
{
Grid1.edititemindex = (INT) E. Item. itemindex;
// Re-read and bind the data
Bindgrid ();
}
Void cancelcommand (Object sender, datagridcommandeventargs E)
{
Grid1.edititemindex =-1;
// Re-read and bind the data
Bindgrid ();
}
Void updatecommand (Object sender, datagridcommandeventargs E)
{
// Update data using SQL statements
String XM = grid1.datakeys [(INT) E. Item. itemindex];
String [] Cols [4];
Int numcols = E. Item. cells. count;

For (INT I = 2; I <numcols; I ++)
{
// Retrieve the value of each edit box
String colvalue = (textbox) E. Item. cells [I]. controls [0]). text;
Cols [I-2] = colvalue;
}
String updatecmd = "Update Reg set name = '" + Cols [0] + "',";
Updatecmd + = "Sex = '" + Cols [1] + "', ADDR = '" + Cols [2] + "',";
Updatecmd + = "DH = '" + Cols [3] + "'where name =" + XM;

Sqlcommand mycommand = new sqlcommand (updatecmd, myconnection );
Mycommand. Connection. open (); // open the data connection
Mycommand. executenonquery (); // execute the connection
Grid1.edititemindex =-1; // The connection is successful and the initial status is returned.
Mycommand. Connection. Close (); // close the connection
Bindgrid (); // re-read data and bind
}
Void bindgrid ()
{
// Data Binding
Sqldataadapter mycommand = new sqldataadapter ("select * from Reg", myconnection );
Dataset DS = new dataset ();
Mycommand. Fill (DS, "Reg ");
Grid1.datasource = Ds. Tables ["Reg"]. defaultview;
Grid1.databind ();
}

Learning Source: http://www.cnki.net

Related Article

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.