Web Design Patterns in aspnetforums code

Source: Internet
Author: User
Tags interface key words modify oracle database
web| design has not been very understanding of the design pattern in aspnetforums, a large number of use of custom controls, n-per structure is too complex. Beginning to think it was because of the load user def skin, today I stumbled upon an article in the Microsoft China Community, address http://www.microsoft.com/china/community/Column/93.mspx After reading, I understand some. Compare articles and Aspnetforums code to benefit a lot.

The article reads as follows:

Column Works
Understanding Web Design Patterns
Shangjian
--------------------------------------------------------------------------------

Understanding Web Design Patterns
This article is published in the "Program Spring and Autumn" 2004 1
Summary
This article describes the. NET Framework, some basic methods and key points of improving WebForm programming with web design patterns are presented.
Key words
design mode, Asp.net,webform,mvc,page Controller,front controller,page Cache
Directory
? Introduction
? The classic WebForm architecture
? Design Patterns
? WebForm in the MVC pattern
? WebForm in Page Controller mode
? WebForm in Front Controller mode
? WebForm in Page cache mode
? Reference Resources
? Download Address
? Author information
Introduction
Remember when Microsoft just launched ASP.net, the shock is that the development of Web programs is no longer to write traditional Web pages, but like the construction of applications, so Microsoft called WebForm. But two years later today, a considerable number of developers still use the idea of writing scripts to build one after another webform, but did not play out the advantages of ASP.net, this article hope that through examples can inspire readers some new ideas.
Due to its limited space, it is not possible for this article to show the reader a webform with a design pattern through a complex web application, but if it is only a small program, there is no need to use the pattern. For the sake of understanding, it is hoped that you can imagine it as a small module in a large system (if the code is part of a large system, then the usage pattern becomes very important).
The download address for all source programs is given at the end of this article.
The classic WebForm architecture
First look at a simple application, database design as shown below, the portal is the subject parent table, through the Portalid for One-to-many Association, the program needs to display a different subject list according to Portalid.

According to our custom of writing WebForm, first drag a DropDownList, a DataGrid, a button control on the page:
Interface (webform.aspx):
<form id= "WebForm" method= "POST" runat= "Server" >
<asp:dropdownlist id= "DropDownList" runat= "Server" ></asp:DropDownList>
<asp:button id= "button" runat= "Server" text= "button" ></asp:Button>
<asp:datagrid id= "DataGrid" runat= "Server" ></asp:DataGrid>
</form>

Then the core code written using the Vs.net code-behind feature is as follows:
Post Code (WebForm.aspx.cs):
Page initialization Events
private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
String sql_select_portal = "SELECT * from PORTAL";
Use using using to ensure free database connections
The connection string is stored in the Web.config file for easy modification
using (SqlConnection conn = new SqlConnection (configurationsettings.appsettings["ConnectionString"))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter (Sql_select_portal, Conn);
DataSet DataSet = new DataSet ();
DataAdapter.Fill (DataSet);
Set the data source and text field, range of a drop-down list
Dropdownlist.datasource = DataSet;
Dropdownlist.datatextfield = "Portalname";
Dropdownlist.datavaluefield = "Portalid";
Dropdownlist.databind ();
}
}
}
Button's Click event
private void Button_Click (object sender, System.EventArgs e)
{
String sql_select_subject = "SELECT * from SUBJECT WHERE portalid = {0}";
using (SqlConnection conn = new SqlConnection (configurationsettings.appsettings["ConnectionString"))
{
Replace the pending character {0} in the SQL statement with the value selected by the Drop-down list
SqlDataAdapter dataAdapter = new SqlDataAdapter (string. Format (Sql_select_subject, Dropdownlist.selectedvalue), conn);
DataSet DataSet = new DataSet ();
DataAdapter.Fill (DataSet);
Datagrid.datasource = DataSet;
Datagrid.databind ();
}
}


Execution results as shown, the program binds the DataGrid according to the values selected by the Drop-down list box, a very typical WebForm architecture that embodies the idea of ASP.net event-driven, and enables the separation of interface and code. But take a closer look and find out a few questions:
? Repeat code for database operations is the absolute "bad taste" of software development, often for some reason when you modify a code, but forget to change the same code, so that the program left a bug hidden.
? The post code is completely dependent on the interface, the changes in the interface under the WebForm is much greater than the data storage structure and access changes, when the interface changes you will have to modify the code to adapt to the new page, it is possible to rewrite the entire post code.
? The post code handles not only the user's input but also the processing of the data, and if the requirements change, such as changing the way the data is processed, you will almost rewrite the entire back code.
An excellent design requires every module, each method only focus on doing one thing, such structure is clear, easy to modify, after all, the requirements of the project is always changing, "the only constant is change itself", good procedures must be prepared for change, to avoid "far-reaching", So be sure to find a way to solve the above problems, let's look at the design pattern.
Design Patterns
Design patterns describe a recurring problem and a core solution to the problem, which is a successful framework, design and implementation plan, and is a summary of experience. The concept of design pattern originated from Western architecture, but the most successful case was the "36-meter" in ancient China.
WebForm in the MVC pattern
The MVC pattern is a basic design pattern for separating the user interface logic from the business logic, which divides data processing, interface, and user behavior control into: Model-view-controller.
? Model: Responsible for the current application of data acquisition and change and related business logic
? View: Responsible for displaying information
? Controller: Responsible for the collection of conversion user input

View and controller are all dependent on model, but model is neither dependent on view nor on controller, which is one of the main advantages of separation, so that model can be set up and tested separately to facilitate code reuse, View and controller only need model to provide the data, they do not know, and do not care whether the data is stored in SQL Server or Oracle database or somewhere else.
According to the idea of MVC pattern, we can split the post code of the above example into model and controller, use a specialized class to process the data, and the post code as controller is only responsible for transforming the user's input, the modified code is:
Model (SQLHelper.cs): Encapsulates all operations on the database.
private static string sql_select_portal = "SELECT * from PORTAL";
private static string Sql_select_subject = "SELECT * from SUBJECT WHERE portalid = {0}";
private static string sql_connection_string = configurationsettings.appsettings["ConnectionString"];
public static DataSet Getportal ()
{
Return GetDataSet (sql_select_portal);
}
public static DataSet Getsubject (String portalid)
{



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.