A system that uses ASP.net to write multi-tier architectures

Source: Internet
Author: User
Tags trim visual studio
Asp.net| Architecture

Do not be intimidated by the title, in fact, very simple. Or you can only write the noodle-style program for life. In particular, ASP programmers are mostly written in the 3 in 1 (Ultra-platinum version: P) of the program, can be written as a layered type is also a master of the master, this kind of prawns estimated early transformation when the system analyst.

Generally speaking, the system is divided into three layers (three layers are said to be good): User layer, logical layer, data layer. May not call these names, but in any case the function should be the same. Speaking of features, please look below.

User layer: Is the main interface with users, that is, user interfaces. What the input box ah, the message AH is on the top. Because users for the system are foolish users (not users are fool, but the business logic, data relationship is not every user can understand, if you can understand can not write programs, directly to the user to manipulate the database is good), You have to make a lot of restrictions and hints on this level so that users can enter the data correctly. This level is generally the original data acquisition and the final output target. This is the same as the packaging of a commodity for others to see. With the business logic data processing is not much relationship, do not participate in logical operation data processing, plainly can only see Ah! Did someone say there was an input box? Isn't that just a look? Actually the input box is just the extension of the logical layer. Because the logic layer has no user interface, can only rely on others. Like fishing, the hook is the user layer is used to see, fish rods, fish lines and winder and fishing is the logical layer. The fish basket is the data layer (not very image will be used to pull).

Logic layer: Is secretly carried out, the user is not known. System to do the judgment ah, truncated raw data ah, the original data for arithmetic, ah, output error information to the user layer Ah, and so on. In other words, the user enters the "ABC" Logic layer to do is to determine whether "ABC" conforms to the business logic needs, is to continue otherwise is illegal data. The logical layer also decides whether to extract "A" from "ABC" or "B" to the data layer to ensure the proper operation of the data layer. Of course, the logic layer is still doing a lot of things will not be wordy. When the logic layer completes the processing of all the data, she passes the data to the data layer.

Data layer: This is to deal with the database, for incoming data, the data layer decided to write or add to the datasheet, add to a table or B table. Completes the return result or error message or data collection to the logical layer. If he's finished, everything else will be in his charge.

All right, the introduction is over (a sweat: ~) Now said the benefits, the most typical example is when the business logic changes, only need to modify the logic layer can be other layers do not have to change. For example, the original business logic requires that user input a plus b to the database. Later the business logic changed to require a minus B to be stored in the database. This is the only time you need to modify the business logic layer. And when the database table field type is changed, if there is no layering, you have to turn all the code out to see where the field is used. There is a layering is simple, the logical layer of data passed directly to the type is good, everything as usual. Really home travel, arson must AH ~ ~ ~ ~:

What do you mean by saying so much to be layered? Let's write a little example

actually used. NET is directly divided into layers, the user interface and logical data levels.
Create a new project and create a file named Test.aspx. Create a class named DBTEST.CS. Open the test.aspx into the following way
<%@ Page language= "C #" codebehind= "Test.aspx.cs" autoeventwireup= "false" inherits= "Movieadmin.test"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>test</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:textbox id= "TextBox1" style= "Z-INDEX:101; left:296px; Position:absolute; Top:104px "
runat= "Server" ></asp:TextBox>
<asp:textbox id= "TextBox2" style= "z-index:102; left:296px; Position:absolute; Top:136px "
runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" style= "z-index:103; left:504px; Position:absolute; top:120px "runat=" Server "
text= "button" ></asp:Button>
</form>
</body>
</HTML>

is two text boxes one submit button

Open TEST.CS (that is, view test.aspx code)
Write as follows

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

Namespace Movieadmin
{
 ///<summary>
 ///The summary description of test.
 ///</summary>
 public class test:System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.TextBox TextBox2;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load (object sender, System.EventArgs e)
  {
    //Place the user code here to initialize the page
  }

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

  private void Button1_Click (object sender, System.EventArgs e)
  {
    String Text1 = TextBox1.Text.Trim ();
   string text2 = TextBox2.Text.Trim ();
   string Text3 = Text1 + test2;
   dbtext td = New Dbtext ();
   td.insert (TEXT3);
  }
 }
}
and then write the DBTEST.CS class as follows
using System;
Using System.Data.SqlClient;

Namespace Movieadmin
{
<summary>
Summary description of the dbtext.
</summary>
public class Dbtext
{
Public Dbtext ()
{
//
TODO: Add constructor logic here
//
}
public string Insert (String tempstr)
{
SqlConnection Conn = DB. Dbopen ();
strSQL = "INSERT into [table] (AAA) VALUES (' +tempstr+ ')";
PubLib.DB.DBExecute (Conn, strSQL);
Conn.close ();

Return "Success";
}
}
}

Now, congratulations, you've finally written a useless layered program. : P
This is just a simple example of powder, the structure of the representation. There are many routines in the real application, I am also groping ....
If you have any questions, leave a message, but don't expect too much. Half a barrel of water: P




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.