Three-tier logon practice C #

Source: Internet
Author: User

Three-tier logon practice C #

I had no clue when I first came into contact with Layer 3. Although I knew the reference relationship between them, I still felt very confused. So I decided to start with drawing a picture when I went online and looked at my blog. From the package diagram --> class diagram --> time sequence diagram (logic) --> code, re-do step by step!


I. Package Diagram


Why is there an entity layer? Is there no entity layer? Why is it not Layer 4, but Layer 3?


The role of the entity layer is to facilitate data transmission between the layer and the layer. Each entity object corresponds to a one-to-one representation in the database. For a large amount of data, you can directly extract fields using the Get () and Set () Methods of the object, which is easier than creating variables temporarily. It is equivalent to using the object-oriented idea to encapsulate a large amount of data and then transfer it.


For example:

AddUser (UserID, UserName, UserPassword)

The entity layer is used to encapsulate these fields into the UserInfo class, which can be written as AddUser (UserInfo). This saves a lot of trouble.


It is not necessary to have a physical layer. if there is only a small amount of data, such as the data center charging system, you can use CardID as a parameter to query the student corresponding to a card number, so there is no need to use the entity layer for transmission.


The entity layer provides convenience for data transmission. It is only a class that has no practical effect and is dispensable. Therefore, it cannot be used as a layer like UI, DAL, and BLL.


Ii. Category chart


1. entity layer UserInfo class





2. layer D: DbUtil class and UserDAO class






3. layer B: LoginManager class




4. U layer: FrmLogin class




Iii. Sequence Diagram




Iv. Code
 
 

1. Entity layer: UserInfo class


Defines a field, obtains the Get () and Set () Methods of the field, and corresponds to the Users table in the database, facilitating data transmission.

Namespace LoginModel {// public ing public class UserInfo {private string username; public string UserName {get {return username;} set {username = value;} private string password; public string PassWord {get {return password;} set {password = value;} public UserInfo (string username, string password) {this. username = username; this. password = password ;}}}



2. layer D:


(1) DbUtil class:


Namespace LoginDAL {////// Create a database connection string ///Class DbUtil {public static string ConnString = @ "server = bill \ BILLSQL; dataBase = Users; user id = sa; password = 123456 ";}}



(2) UserDAO class:


It is mainly used to provide basic database access for BLL to call.


Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data; using System. data. sqlClient; namespace LoginDAL {public class UserDAO {// query the user's public LoginModel. userInfo SelectUser (string username, string password) {using (SqlConnection conn = new SqlConnection (DbUtil. connString) // create a sqldatabase to open the connection {SqlCommand cmd = conn. createCommand (); // create and return a SqlCommand object cmd associated with SqlConnection. commandText = @ "select username, password from user_Info where username = @ username and password = @ password"; // obtain the query statement run by the data source cmd. commandType = CommandType. text; // set a value to explain the CommandText attribute // Add the specified SqlParameter to SqlParameterCollection cmd. parameters. add (new SqlParameter ("@ username", username); cmd. parameters. add (new SqlParameter ("@ password", password); conn. open (); // Open the database connection SqlDataReader reader = cmd. executeReader (); // send CommandText to Connection and generate a SqlDataReader LoginModel. userInfo user = null; // move SqlDataReader to the next record while (reader. read () {if (user = null) {user = new LoginModel. userInfo ();} user. userName = reader. getString (0); user. passWord = reader. getString (1) ;}return user ;}}}}



3. layer B: LoginManager class


Processing business logic mainly includes determining whether the user name and password are empty, and passing the data obtained from layer D to the entity layer.


Namespace LoginBLL {public class LoginManager {public LoginModel. userInfo UserLogin (string username, string password) {// throw new NotImplementedException (); LoginDAL. userDAO uDAO = new LoginDAL. userDAO (); // The username cannot be blank. if (username = null) {throw new Exception ("Enter the username ");} // The password cannot be blank. if (password = null) {throw new Exception ("Enter password");} LoginModel. userInfo user = uDAO. selectUser (username, password ); If (user! = Null) {return user;} else {throw new Exception ("Logon Failed ");}}}}



4. U layer: FrmLogin class


It mainly includes interface commands and operations to receive data from layer B.


Namespace LoginUI {public partial class FrmLogin: Form {public FrmLogin () {InitializeComponent ();} private void btnLogin_Click (object sender, EventArgs e) {string username = txtUserName. text. trim (); string password = txtPassWord. text; try {LoginBLL. loginManager mgr = new LoginBLL. loginManager (); LoginModel. userInfo user = mgr. userLogin (username, password); MessageBox. show ("Login user:" + user. userName);} catch (Exception err) {MessageBox. show (err. message. toString () ;}} private void btnCancel_Click (object sender, EventArgs e) // unmount the FORM {Close ();}}}



5. Although the theory understands the theory, some code is not quite familiar with it, especially the data transfer module. This was found during debugging: enter the user name and password --> B layer --> D Layer --> entity layer --> D Layer --> B layer --> the logon is successful!
After the user enters the user name and password, execute the B-Layer Code to determine whether the user name and password are empty, instantiate the LoginManager class, call its UserLogin method, jump to the B-layer, instantiate the UserDAO class, and call its SelectUser method. adjust it to layer D to create a database connection object ,...... This is a mess at the Entity layer. I am reading this blog. Could you help me!



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.