(absolute original) three-layer hierarchy Division

Source: Internet
Author: User
Tags class definition asp net
discussion on hierarchy division in three-layer development

First, give an example of what you've seen in a book: Now you want to build a bridge over a 1-meter-wide creek, and you'll put a plank on it. If you want to build this bridge on a wider river, you need to calculate the wood materials, prices, and so on, if you need help, you have more drawings or something to let others understand your ideas. Now you have to build a bridge over the river, you need to have a whole plan, including all aspects, such as possible future charges and benefits distribution.

The 3-layer, in fact, is aimed at "the river above the bridge" to the 1-meter-wide creek, in the actual may not have any use. But now I can not go to get a Yangtze River bridge for example, so here is still using this simple brook, talk about how to build a bridge. The reason why so much nonsense, is to prevent some people after reading this article "A small thing, do so troublesome to do." "In fact, this is not a concrete example, but a layered thinking, it is very important to understand this."

The following is the example of "User login", which is the most common example that we all see in our daily life. This example is simple, but though small spite. Everything from data access to business rules to the interface.

This article is divided into 2 parts, if you only want to study the object-oriented ideas, the implementation is already familiar with, you can skip the first part.

Part I.

Create a new blank solution. And then:

Add-New project-Other items-Enterprise Template project-C # building block-data access (data tier, D-tier)

Add-New project-Other projects-Enterprise Template Projects-C # Building blocks-Business rules (business layer, hereinafter referred to as C-tier)

Add-New project-Other items-Enterprise Template project-C # Build block-Web user interface (interface layer, next to u layer)

Right-click "Solution"-"Project Dependencies", setting u depends on D, c,c dependent on D.

Add Reference D, c to u, add Reference D to C.

So far, a three-storey shelf has been set up. I said above is very specific very "fool", know people think I nonsense, in fact, I feel very strongly this time very many people actually to this simple process completely does not understand. Although not opposed to the 2 "Empty Project" and an "ASP NET Web Application project" can also serve as a 3-tier framework, and a considerable number of people think that these "Enterprise Template project" is actually an empty project, this is an error. Yes, Enterprise Template project you see it as nothing in Solution Explorer, but you can open the project file in Notepad and see the difference. There are things behind you that you can't see, but the system is ready. That is, if you have a "using System Data Sqlclineit" In a class in C, or you use a SqlConnection object, compile without error, but generate some "policy warnings" in the task list, Warn you not to put something in the C layer that should be on the D layer (although that's true for the program, but the readability maintainability is discounted) and this feature, empty items are not available to you.

We know that building a bridge needs bricks, should be ready to build a brick before the bridge, but in order to explain the order and consistency of simplicity. We build the bridge first, we need bricks to reproduce in the process, so we don't have to come out with "what the bridge doesn't need". Note that in practice, you should prepare the bricks first.

The U layer is actually the bridge, the C layer is the brick, the D layer is the raw material (stone, sand). This also explains why the U-layer is to be referenced and dependent on the D layer (instead of the U-c,c to D level) because the bridge needs stone sand in addition to bricks.

We build a login aspx on the U layer (insert here, I don't like to change the system automatically generated WebForm1 aspx to login or index or delete directly, I usually keep it when the test code used, wait until the whole system freezes and then remove it. Add 1 textbox (Id=txt), one DropDownList (ID=DDL), one button (ID=BTN). Where DropDownList is used to select the username, the button is the submit buttons, and the textbox is used to enter the password.

Now the code we have to add is divided into 2 parts: 1, Page_Load initialization of the DDL. 2, BTN click Processing.

1:

private void Page_Load (object sender, System.EventArgs e)

{

if (! IsPostBack)

{

THIS.DDL.DATASOURSE=DATAMANAGER.GETONECOLUNM ("User", "UID"); Explanation 1

This.ddl.DataBind ();

}

}

2:

private void Btn_click (object sender, System.EventArgs e)

{

String Uid=this.ddl.selectedvalue;

String Psw=this.txt.text;

if (PSW = "")

MessageBox ("Blank password.") ”);

Else

{

User theuser;

Try

{

Theuser=new User (UID); Explanation 2

}

catch (Exception e)

{

MessageBox (E. Message)/explanation 2

Return

}

if (THEUSER.CHECKPSW (PSW))//Tutorial 3

{

Theuser.setsessions ();

Response.Redirect (".........."); Login successful.

}

Else

{

MessageBox ("Bad password"). ”);

}

}

}

Explain 1:datamanager is a class in the D layer that provides common data manipulation. The GETONECOLUNM (string table,string colunm) method returns a DataTable with only 1 columns, the value of which is the COLUNM column in the database with the table name tables.

public class DataManager

{

Public DataManager ()

{

}

public static DataTable getonecolunm (String table,string colunm)

{

The relevant code is omitted here. Returns the specified table specified column

}

}

In fact, this place demonstrates the example of bypassing C-level access to level D in the U-tier, because the structure is logically simple, and getting the user name is not part of the business logic in the real world (just the interface needs, because there is no need for this in the case of a 2-textbox).

Explanation 2: Define an instance of the user class. The user class definition may be as follows:<

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.