In the previous article, we introduced the basic knowledge of the layer-3 architecture. This time, let's explore its operating mechanism.
1. What is the object Layer (1 )?
Entity class-- The entity class actually stores data. The data we read in the database is stored in the Entity layer (it is the ing of the auxiliary database and can be understood as the carrier for data transmission ). Entity classes exist at the data management and business logic processing levels. They primarily differentiate entity classes in the analysis phase. Their main responsibilities are to store and manage information within the system, it can also have behaviors or even complex behaviors, but these actions must be closely related to the Entity objects it represents. The entity class corresponds to a table in the database, and each attribute of the entity class corresponds to the corresponding fields in the table. The entity class conforms to the object-oriented programming idea and encapsulates a table into a class.
(2) When a program is running, data transmission (unidirectional) between each layer (UI-> BLL-> DAL) depends on variables or entities as parameters, in this way, the link between the three layers is constructed, and the function implementation is completed. However, for a large amount of data, it is complicated to use variables for parameters, because there are too many parameters and it is easy to mix them up. For example, I want to transmit employee information to lower layers, including employee ID, name, age, gender, salary .... if we use variables as parameters, there will be a lot of parameters in our method, and it is very likely that we will mix the parameters during use. At this time, if you use an object as a parameter, it will be very convenient. You do not need to consider the parameter matching problem. You can use the attribute in the object to directly use it ..
Therefore, the entity class has a role 1 to implement the "encapsulation" in the object-oriented thinking ";
2. Data is transmitted between three layers;
3, improve efficiency; 2. To run the prerequisite program, you must first connect the Layer 3 and Entity, that is, add references to the project. Reference relationships: UI references BLL and Entity; BLL references DAL and Entity; DAL references only Entity.
3. Running Mechanism 4. Running instance (1) the UI Layer for login is mainly provided to the user interface for interaction with the user
Code of the login button
Private void button#click (object sender, EventArgs e) {// instantiate a model object Login. model. userInfo user = new Login. model. userInfo (); // pass the parameter to the user of the object. userName = txtUserName. text. trim (); user. password = txtPassword. text; // instantiate layer B Login. BLL. userManager mgr = new Login. BLL. userManager (); // transmits the object to layer B to process Login. model. userInfo User = mgr. userLogin (user); // judge MainUI Based on the returned information. form1 frm = new MainUI. form1 (); frm. show (); this. hide ();}
(2) login to the BLL-layer UserManager class
Public class UserManager {public Login. model. userInfo UserLogin (Login. model. userInfo user) {// call layer D Login. DAL. userDAO udao = new Login. DAL. userDAO (); // Let the D Layer determine whether the information is correct Login. model. userInfo User = udao. selectUser (user); if (User = null) {// if the user name is incorrect, throw new Exception will be prompted ("the User name is incorrect! ");} Else if (User. Flag = false) {throw new Exception (" Incorrect password, login failed! ");} Else {// login successful, return User information return User;} <pre name =" code "class =" csharp "style =" font-size: 18px; line-height: 25px; ">}}
(3) login to the DAL layer DBUtil class
Class DBUtil {// Database connection statement public static string ConnString = @ "server = localhost; Database = Login; User = sa; Password = 123456 ;";}UserDAO class
Public class UserDAO {public Login. model. userInfo SelectUser (Login. model. userInfo user) {// connect to the database and verify the information using (SqlConnection conn = new SqlConnection (DBUtil. connString) {SqlCommand cmd = conn. createCommand (); // used to process the database // determine whether the user name exists cmd. commandText = @ "SELECT * from users where UserName = @ UserName"; // obtain the query language cmd. commandType = CommandType. text; // Add the cmd parameter in the query language. parameters. add (new SqlParameter ("@ UserNam E ", user. userName); // cmd. parameters. add (new SqlParameter ("@ Password", user. password); conn. open (); // query and return the result SqlDataReader reader = cmd. executeReader (); // declare a returned instance. If the user name is not saved, an empty instance Login is returned. model. userInfo User = null; while (reader. read () {// if the User name exists, verify that the password is correct if (User = null) {User = new Login. model. userInfo ();} // returns the user information if (user. password = reader. getString (2) {User. flag = true; // a Flag indicating that the user password is correct User. ID = reader. getInt32 (0); User. userName = reader. getString (1); User. password = reader. getString (2); // if the mailbox is not empty, retrieve if (! Reader. isDBNull (3) {User. email = reader. getString (3) ;}} else {User. flag = false; // Flag indicating that the User password is incorrect} return User ;}}}
(4) login to the Model layer UserInfo class
public class UserInfo { public int ID { get; set; } public string UserName { get; set; } public string Password { get; set; } public string Email { get; set; } public bool Flag; }Last login successful