Compiling a multi-layer architecture system with ASP. NET

Source: Internet
Author: User

 

Do not be intimidated by titles. It is actually very simple. Otherwise, you can only write noodle programs for a lifetime. IN particular, most ASP programmers write 3 IN 1 (Super platinum: P) programs, barely able to write hierarchical style is also a master of the master, such prawns are estimated to have long been transformed into a system analyst.

Generally, systems are divided into three layers (it is said that three layers are good): user layer, logic layer, and data layer. These names may not be called, but the functions should be the same in general. For more information, see the following section.

User layer: it mainly deals with users, that is, user interfaces. The input box and prompt information are all on top. Because users are foolish users for the system (not users are fool, but business logic and data relationships are not understood by every user. If users can understand them, they do not need to write programs, directly let the user operate the database), you have to make many restrictions and prompts at this level so that the user can enter the correct data. This layer is generally the ultimate goal of raw data collection and data output. This is similar to the packaging of a product. It has little to do with business logic data processing, and does not participate in logic operation data processing. To put it bluntly, you can only read it! Some people have said that there are still input boxes? Isn't it just watching? In fact, the input box is just an extension of the logic layer. Because the logic layer does not have a user interface, it can only rely on others. Like phishing, hooks are used by the user layer for viewing. fishing rods, fishing lines, and bypassing and phishing are the logic layer. The fish basket is the data layer (the image will be pulled if it is not very good ).

Logic layer: It is carried out in the back, and the user is unknown. The system determines, truncates the original data, performs arithmetic operations on the original data, and outputs error messages to the user layer. That is to say, what the user needs to do when entering the "ABC" logic layer is to determine whether "ABC" meets the business logic needs. If yes, it will continue or else it will be illegal data. The logic layer also determines whether to extract "A" of "ABC" or extract "B" to the data layer to ensure proper operation of the data layer. Of course, the logic layer does not have to do a lot of work. When the logic layer completes the processing and judgment of all data, it passes the data to the data layer.

Data Layer: This is used to deal with databases. for incoming data, the data layer decides whether to write or add data to the data table, add data to Table A or table B. The returned result, error message, or data set is sent to the logic layer. Even if he is done, everything else will be taken care.

Now, the introduction is complete (a sweat :~) For the benefit, the most typical example is that when the business logic changes, you only need to modify the logic layer so that other layers do not need to be changed. For example, the original business logic requires that the user input a be added to B to the database. Later, the business logic changed to require A minus B to be stored in the database. At this time, you only need to modify the business logic layer. In addition, when the field type of the database table changes, if there is no hierarchy, you need to turn all the code out to see where this field is used, you need to change it (after an error is rectified, no error occurs ). It is easy to have a layer, and it is good to directly convert the data imported from the logic layer to the type. Everything is the same as usual. It's really a must-have for home tourism, murder, and arson ~~~ :)

After talking about this, how can we achieve this level of hierarchy? Below is a small example.

In fact, the. NET is directly divided into two layers: the user interface and the logical data.
Create a new project and a file named TEST. ASPX. Create a class named DBTEST. CS. Open TEST. ASPX as follows
<% @ 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: pixel"
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>

That is, a button is submitted for two text boxes.

Open TEST. CS (view the code of TEST. ASPX)
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>
/// Summary 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 user code here to initialize the page
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void button#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 );
}
}
}
Then write the DBTEST. CS class as follows:
Using System;
Using System. Data. SqlClient;

Namespace MovieAdmin
{
/// <Summary>
/// Summary of dbtext.
/// </Summary>
Public class dbtext
{
Public dbtext ()
{
//
// TODO: add the 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 "successful ";
}
}
}


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.