Layer 3 learning ------ practice

Source: Internet
Author: User

Practice is the only criterion for testing truth. Next we use a three-tier architecture to practice a simple "login" business. Learn how to use it and feel the breath of the three-tier architecture.


The above is the startup interface, which belongs to the U layer, in the form of Winform and web. After you enter the user name and password, the system verifies that the password is correct. Note: For ease of use, the User name entered must exist in the User table of the database. Otherwise, an error is reported. The following is the overall code structure.


Next, let's take a look at the code in each layer:

Entity layer:

Public class User {private string userName = string. empty; // string. empty is equivalent to "", which is generally used for string initialization private string pwd = string. empty; public string UserName {get; set;} public string Pwd {get; set ;}}
UI:

Private void btnOK_Click (object sender, EventArgs e) // click "OK" to verify the user information {// declare a bool type variable to receive the logon status, logon successful/failed bool result = false; // check whether the username and password are empty if (txtUserName. text = "") {MessageBox. show ("the user name cannot be blank! "); TxtUserName. Focus (); return;} if (txtPassword. Text =" ") {MessageBox. Show (" the password cannot be blank! "); TxtPassword. focus (); return;} // declare a User object and assign the User name and password entered by the User to the User Object User euser = new User (); euser. userName = txtUserName. text. trim (); euser. pwd = txtPassword. text; // access layer B LoginServer blogin = new LoginServer (); result = blogin. BLogin (euser); // a Boolean value is returned after the password is verified at Layer B. if (result = true) is passed here. // if true is returned, the logon succeeds {MessageBox. show ("Logon successful! ");} Else // returns false, incorrect password input, Logon Failed {MessageBox. Show (" Logon Failed! ") ;}} Private void btnCancel_Click (object sender, EventArgs e) // click" cancel "TO Close the form {this. Close ();}
BLL:

Public class LoginServer // verify that the User password is correct {public bool BLogin (User) {UserDAL duser = new UserDAL (); // access layer D, connection database User euser = new User (); euser = duser. DLogin (User); // The subsequent tasks are performed in layer D. Obtain the password of this User name from the data table. // if the password entered by this User is the same as that in the data table, it is true, logon successful; otherwise, false. If (euser. pwd = User. pwd) // The euser is the entity class returned by layer D. It carries the User information stored in the database, and the User carries the User information entered by the User, compare the passwords carried by the two to {return true;} else {return false;} // return to the U layer and return the operation result to the user }}
DAL:

Public class UserDAL {// Connection Database public static string conString = @ "Server = CYL-PC; Database = Login; User ID = sa; Password = 123456"; SqlConnection cnn = new System. data. sqlClient. sqlConnection (conString); // obtain the User Password public User DLogin (user User) {string sqlString = "select * from Users where UserName = '" + User. userName + "'"; User euser = new User (); SqlCommand cmd = new SqlCommand (sqlString, cnn); // open the database cnn. open (); SqlDataReader read; read = cmd. executeReader (); read. read (); // grant the User information in the User table to the object class and return it to layer B euser. userName = read [0]. toString (); euser. pwd = read [1]. toString (); return euser ;}}
User table in database Login:

After the system is enabled, the system logon interface is displayed. The user enters the user name and password and performs password verification. (If you do not enter the user name, click "OK". The system detects that the password is empty and returns to the logon interface ). The specific verification process is as follows. All three layers need to instantiate a User object in the Entity layer, where the User in the U layer carries the User information (User name and password) entered by the User ); layer D finds the User information in the database based on the User name entered by the User, so that the User in layer D carries the information and passes it to the User in layer B. Perform a specific verification process on layer B. The password carried by the User on the U layer is compared with the password carried by the User on the B layer. If the two are the same, the "Logon successful" message is displayed to the User, otherwise, the system prompts "Logon Failed ". My presentation skills are limited. I have a clear understanding of the execution details and can debug statements by statement (F11 ).

In these three layers, DAL only provides basic data access, and the UI is only responsible for displaying and collecting user operations. They do not contain any business-related logic processing. BLL is responsible for processing the business logic. By obtaining operation commands from the UI, it executes the business logic and hand it over to the DAL for processing when the data source needs to be accessed. After the processing is completed, the necessary data is returned to the UI. The entity layer jumps up and down between them, and the three layers do not know the specific content in the User. The advantage of this is that it achieves high cohesion and low coupling. Division of labor and collaboration at all layers, performing their respective duties, and proceeding in an orderly manner. Enhances the scalability, reusability, and maintainability of the software system.


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.