I. Basic knowledge of three-layer architecture
During project development, the entire project is sometimes divided into three layers, including presentation layer (UI), business logic layer (BLL), and data access layer.
Data Layer (DAL ). The role of Layer 3 is as follows:
Presentation Layer:
Provide Interactive Operation interfaces for users. This is true for both Web and WinForm.
. The interface that our website displays to users.
Business logic layer:
Responsible for key business processing and data transmission. Complex logic judgment and database-related data verification are required here
Make the processing. Return the desired value or process related logic based on the input value.
Data access layer:
Is responsible for accessing database data. It mainly provides data for the business logic layer and operates data based on input values.
Library, add, delete, modify, or others.
The following describes the next user management module:
To facilitate the development of the entire project, we will create several Class Libraries Common, BLL, DAL, and Model in the project.
To name the project clearly, we can name the three projects (Class Libraries added in the solution) as follows ):
Business logic layer (BusinessLogicLayer): BLL. By default, the namespace is set to BLL data access layer (DataAccessLayer): DAL. The namespace is set to DAL by default.
Tool class: for example, Common. The namespace is set to Common by default.
In addition, we usually add another class library to facilitate data transmission. This class library runs through the entire three-tier architecture. Entity class.
The namespace is usually named "Model" and the default value is "Models. Each encapsulated class corresponds to an entity, usually a database.
. For example, if the user table (custom) in the database is encapsulated as (custom), each field in the table is encapsulated as a common owner.
.
In this way, the construction of the three-tier architecture is basically complete. These three layers have very strong dependencies:
Presentation Layer ‑ business logic layer ‑ data access layer
Data transmission between them is bidirectional, and data is usually transmitted through the entity class.
What are the advantages of the three-tier architecture:
1. Easy Project Modification and maintenance.
During project development or after development, or even during project migration.
This three-tier architecture is very convenient. For example, to migrate a project from Web to Form, we only need to repeat the presentation layer.
The remaining two layers do not need to be changed. You only need to add them to the existing project. If this architecture is not used, the code is only written to the presentation layer. So
Almost all codes have to be re-encoded.
2. Easy to expand.
This is also true for function extensions. If you want to add a function, you only need to add the method of the original class library.
3. Easy code reuse.
This does not need to be explained.
4. Easy division of labor and collaboration
You can also add an interface class library Iinterface to the design mode to make your code more flexible and of higher quality.
Ii. System logon Example a: Overall
Using System; using System. collections. generic; using System. linq; using System. text; using HRMSys. DAL; namespace HRMSys. BLL {// <summary> /// insert operation records /// </summary> public class OperationLogBLL {OperationLogDAL dal = new OperationLogDAL (); public void InsertOperationLog (Guid id, string name, string ActionDesc) {dal. insertLog (id, name, ActionDesc );}}}C, OperatorBLL. cs
Using System; using System. collections. generic; using System. linq; using System. text; using HRMSys. DAL; using HRMSys. model; using Common; namespace HRMSys. BLL. cs {public class OperatorBLL {OperatorDAL = new OperatorDAL (); /// <summary> /// get the data of the operator with the specified name /// </summary> /// <param name = "name"> </param> // /<returns> </returns> public Operator getOperatorByName (string name) {Operator op = new Operator (); Op = dal. loginUser (name); return op ;}/// <summary> /// the password is incorrect more than three times, locked by /// </summary> /// <param name = "op"> </param> public void updateOperator (Operator op) {dal. updateOperator (op);} // <summary> // log on // </summary> // <returns> </returns> public bool login (string name, int errorTime, string pwd, out string message, out Operator op) {bool r = false; if (name. length <= 0) {message = "Enter the user name "; Op = null; return r;} if (pwd. length <= 0) {message = "Enter the user name"; op = null; return r;} if (errorTime> = 3) {message = "the user has been locked "; op = null; return r;} else {op = dal. loginUser (name); if (op = null) {message = "the user name does not exist";} else {pwd = CommonHelper. getMD5 (pwd + CommonHelper. getPasswordSalt (); if (op. password! = Pwd) {message = "Incorrect password"; op. isLocked = true; updateOperator (op); return r;} else {if (op. isLocked = true) message = "User locked"; else {r = true; message = "Logon successful" ;}} return r ;}}}}
D, OperationLogDAL. cs
Using System; using System. collections. generic; using System. linq; using System. text; using HRMSys. model; using System. data; using System. data. sqlClient; namespace HRMSys. DAL {public class OperationLogDAL {// <summary> /// insert an operation record /// </summary> /// <param name = "OperatorId"> </param >/// <param name = "ActionDesc"> </param> public void InsertLog (Guid OperatorId, string loginUser, string ActionDesc) {sqlhelper. executeNon (@ "insert into T_OperationLog (Id, OperatorId, MakeDate, ActionDesc, LoginUser) values (newid (), @ OperatorId, getdate (), @ ActionDesc, @ LoginUser )", new SqlParameter ("@ ActionDesc", ActionDesc), new SqlParameter ("@ OperatorId", OperatorId), new SqlParameter ("@ LoginUser", loginUser); // values, not value }}}
E, OperatorDAL. cs
Using System; using System. collections. generic; using System. linq; using System. text; using HRMSys. model; using System. data. sqlClient; using System. data; namespace HRMSys. DAL {public class OperatorDAL {/// <summary> /// convert the table format to, to the object /// </summary> /// <param name = "row"> </param> /// <returns> </returns> private Operator ToOperator (DataRow row) {Operator op = new Operator (); op. id = (Guid) row ["Id"]; // you can check whether the user name is repeated op without adding it. userName = (string) row ["UserName"]; op. password = (string) row ["Password"]; op. isLocked = (bool) row ["IsLocked"]; op. isDelete = (bool) row ["IsDelete"]; op. realName = (string) row ["RealName"]; return op ;} /// <summary> /// query a piece of data of the specified username /// </summary> /// <param name = "name"> </param> /// <returns> </returns> public Operator loginUser (string name) {DataTable table = sqlhelper. datatable ("select * from T_Operator where UserName = @ UserName and IsDelete = 0 and IsLocked = 0", new SqlParameter ("@ UserName", name); if (table. rows. count <= 0) return null; else if (table. rows. count> 1) throw new Exception ("Duplicate username"); else {DataRow row = table. rows [0]; return ToOperator (row );}} /// <summary> /// lock the Administrator to update the Administrator table /// </summary> /// <param name = "op"> </param> public void UpdateOperator (operator op) {sqlhelper. executeNon ("update T_Operator set IsLocked = @ IsLocked where Id = @ Id", new SqlParameter ("@ IsLocked", op. isLocked), new SqlParameter ("@ id", op. id ));}}}
F, login. cs
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using HRMSys. model; using HRMSys. BLL; using HRMSys. BLL. cs; namespace HYMSys. UI {public partial class login: Form {public login () {InitializeComponent ();} int errorTime = 0; string message; Operator op; OperationLogBLL logbll = new OperationLogBLL (); operatorBLL bll = new OperatorBLL (); // <summary> // log on, and save the logon Operation Records /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </ param> private void button#click (object sender, eventArgs e) {// return the bool type of Successful Logon, and return the message and administrator object bool result = bll. login (tb_name.Text, errorTime, tb_pwd.Text, out message, out op); if (result = false) {errorTime + = 1; MessageBox. show (message); return;} else {// insert an operation record logbll. insertOperationLog (op. id, op. userName, message); MessageBox. show (message );}} /// <summary> /// cancel /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void button2_Click (object sender, eventArgs e) {this. close ();}}}
Iii. Summary
This is a very simple but very typical small example of a three-tier architecture, hoping to help beginners like me. Welcome to the discussion.