First, in which book I have seen, I will now build a bridge in 1 meters wide creek, you will be on the top of the wooden board. If you want to build this bridge on the small river in width, you will need to calculate wood materials, prices, etc. If you need someone else to help, you have to make more pictures what to let others understand your thoughts. Now you have to build a bridge over the big river, you need to have a whole plan, including all aspects, such as possible fees and interest allocations in the future.
Here is a 3-layer style, in fact, it is aimed at "the bridge over the big river", for a 1-meter wide creek, it may be used in the actual situation. But now I can't take a long river bridge for example, so here is still using this simple creek, tell how to build a bridge. The reason why so many nonsense is to prevent some people from watching this article. "Little is a thing, doing so troublesome .." In fact, it is not a specific example, but a hierarchical idea, understand this Very important.
Below I will say the most of our daily examples, it is an example of "user login". This example is very simple, but the sparrow is small in full. Access from data to business rules to the interface.
This article is divided into 2 parts. If you only want to study object-oriented ideas, you can skip the first part.
first part
Create a new blank solution. then:
"Add" - "New Project" - "Other Projects" - "Enterprise Template Project" - "C # Generating Block" - "Data Access" (Data Layer, Undergrant D Layer)
"Add" - "New Project" - "Other Projects" - "Enterprise Template Project" - "C # Generating Block" - "Business Rule" (Business Layer, Clay below)
"Add" - "New Project" - "Other Project" - "Enterprise Template Project" - "C # Generating Block" - "Web User Interface" (Interface Layer, Under the Undergrant U]
Right-click "Solution" - "Project Dependency", set U depend on D, C, and C dependent on D.
Add a reference D, C to u, and add a reference D to C.
Here, a three-layer shelf is established. I said it is very specific and very "fool", I know that people think that I am nonsense. In fact, I am very strong in this time. A lot of people don't understand this simple process. Although it is not opposed to 2 "empty projects" and 1 "ASP NET Web Applicate Project" can also be used as a 3-layer frame, and quite a few people think that these "enterprise-level template projects" is actually empty projects. It is a misunderstanding. That's right, enterprise-level template projects you have nothing from the solution resource manager, but you can use Notepad to open the project file, see it different? ? Some things are in the back, you can't see it, but the system has been done. That is, if you are "Using System Data SqlClineit" in a class in the C layer, or use a SqlConnection object, you will not be wrong, but you will generate some "Policy Warning" in the Task List, warning Don't put things on the D layer in the C layer (although it is right, you can make a discount on the program, but the readability is a discount), this feature is unable to give you.
We know that the bridge needs bricks, it should be ready to brick and bridge, but in order to explain the sequential and coherence, simply. We first build a bridge, and the bricks need to be made during the construction process, so there will be no more "there is no need for the bridge." Note that in practice, you should still prepare bricks first.
The U layer is actually the bridge, the C layer is a brick, and the D layer is a raw material (stone, sand). This also explains why the U layer is referenced, depending on the D layer (rather than the hierarchy of C, C, C, C, because the bridge needs bricks, but also need stone sand. We built a login aspx in the u (inserted here, I don't like to change the WebForm1 ASPX generated by the system to login or index or delete it directly, I usually leave it as a test code, wait until the entire system freezes Remove it.) Add 1 TextBox (id = txt), a DropDownList (ID = DDL), a Button (ID = BTN). Where DropDownList is used to select the username, but button is the submit button, and TextBox is used to enter a password.
Now we have to add code to 2 parts: 1. Initialization of DDL during Page_Load. 2, BTN's Click processing.
1:
Private Void Page_Load (Object Sender, System.EventArgs E)
{
IF (! ispostback)
{
this.ddl.datasourse = DataManager.getoneColunm ("User", "UID"); // Try 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 ("empty password!");
Else
{
User theuser;
Try
{
Theuser = new user (uid); // Try 2
}
Catch (Exception E)
{
MessageBox (e. Message); // Try 2
Return;
}
IF (theuser.checkpsw (psw)) // Explain 3
{
Theuser.SetSessionS ();
Response.Redirect ("................"); // Sign in success!
}
Else
{
Messagebox ("Password Error!");
}
}
}
Explain 1: DataManager is a class in the D layer, providing common data operations. GetoneColunm (String Table, String Colunm) Returns a DataTable with only 1 column, the value is a colunm column named Table in the database.
Public Class DataManager
{
Public DataManager ()
{
}
Public Static DataTable getonecolunm (String Table, String Colunm)
{
// omit the relevant code. Returns the specified list
}
}
In fact, this place demonstrates an example of accessing the D layer directly by the U layer, because the structure is logically simple, and the username is not part of the business logic in the real society (only the interface needs, because Here, it doesn't need this step without using 2 TextBox.
Explain 2: Define an instance of a User class. The definition of the User class may be as follows:
Public Class User
{
Public User (String Uid)
{
IF (DataManager.isin ("User", "UID = '" UID ")) throw" user does not exist ";
Else // user () Other initialization;
}
Public Bool CHECKPSW (String PSW)
{
IF (DataManager.isin ("User", "UID = '" UID "' and PSW = '" PSW "'"))
Return True;
Else
Return False;
}
}
It is noted that the user-class constructor is used to throw an exception that the user does not exist. Use MessageBox (E. Message) when Catch is below; the "user does not exist" errors. Here, it is also necessary to demonstrate a means of passing information, an exception is also a means, although there is actually otherwise, such as return values, reference parameters, and a way to obtain information for the user, there is no need. In the construction, I do just to demonstrate the delivery process, in the back, there is a discussion of this usage in the hierarchical mode to solve some problems. This class uses a static method for the DataManager class. Isin (String Table, String Str) this method is actually actually executing "SELECT * from Table Where Str"
This SQL statement is returned to false when returning empty, otherwise returns true. A simple way. Here is a call to the C layer to the D layer.
By the way, because in VS.NET, the name of the project will become the Namespace in the project by default, and the 3 layers can be seamless by change the Namespace in all automatically generated code to the "Solution Name". Free call. Or in the called place Using the space name of the other layers, seeing the individual likes. For example, using is required in the login.aspx.cs above, and using is required in User.cs.
Explain 3: The check user password is also used by the user password. Checkpsw () and this method is used here.
Everyone pays that we use the MessageBox () method in the U layer to pop up dialog boxes, in fact, this method is written in pagebase.cs, is another file of the U layer, inherits the Page class, and inherit it, this method In fact, it is encapsulated response.write ("