C # actual operations of the Middle-layer architecture UI, BLL, DAL, and Model
Three-tier architecture: presentation layer (UI), business logic layer (BLL), data access layer (DAL), and object class library (Model)
Reprint please indicate from Zhu jiayuan http://blog.csdn.net/zhgl7688
1. The object library (Model) Stores Table fields in the database.
Operation:
(1) first, create an object class library Model, open the project, right-click solution> Add> new project> select class library> rename Model> OK.
(2) Select "Model class library --" Shift + ALT + C -- "to create an object class. UserInfo class
namespace Model{ public class UserInfo { public string UserName { get; set; } public string Password { get; set; } }}
2. Data access layer (DAL) is mainly used to store access to data classes, that is, basic operations such as adding, deleting, modifying, and updating databases.
Operation:
(1) first, create the data access layer class library DAL, open the project, right-click solution> Add> new project> select class library> change name to DAL> OK
(2) Add a reference to the Model in the DAL, and select DAL -- Alt + P + R -- Solution -- project -- select MOdel -- and OK.
(3) Add a reference to system. configuration in the DAL file, and select "DAL --" Alt + P + R -- "assembly --" framework -- "and select" System. configuration -- ".
(4) create a data category. Select "DAL --" Shift + ALT + C -- "to create a data category. UserDB class
Using System. configuration; using Model; using System. data; using System. data. sqlClient; namespace DAL {class UserDB {private string connString = ConfigurationManager. connectionStrings [connString]. toString (); public int AddUser (UserInfo userInfo) {// Add a user operation to the database string commandText = insert into UserInfo (userName, Password) values (@ userName, @ Password); SqlParameter [] paras = new SqlParameter [] {new SqlParameter (@ userName, userInfo. userName), new SqlParameter (@ Password, userInfo. password)}; return SqlHelper. executeNonQuery (connString, CommandType. text, commandText, paras );}}
// Add other database operations}
3. the business logic layer (BLL) performs logical judgment and discount on transmitted data and transfers the correct value.
(1) first, create the business logic layer class library BLL, open the project, right-click solution -- add -- new project -- select the class library -- rename BLL -- and OK
(2) Add a reference to Model and DAL to BLL, and select BLL -- Alt + P + R -- Solution -- project --> MOdel and DAL --.
(3) create a business logic class. Select BLL --> Shift + ALT + C --> create a business logic class. LoginManager class
Using DAL; using Model; namespace BLL {public class LoginManager {private UserDB userDB = new UserDB (); public bool Add (UserInfo userInfo, out string messageStr) {messageStr =; // return interface layer add user return information bool isSuccess = false; if (userInfo. userName. trim (). length! = 0) // determine whether the passed username is null {if (userDB. isEquals (userInfo) // pass it to The DALl operation to determine whether there are duplicate values in the database {userDB. addUser (userInfo); // Add a new user isSuccess = true;} else messageStr = has the same value;} else {messageStr = cannot be blank;} return isSuccess; // return whether the interface layer is successfully added }}}
5. The presentation layer (UI) is the user interface layer.
(1) Add a reference to Model and BLL in the UI. Select UI -- Alt + P + R -- Solution -- project -- and select MOdel and BLL --.
(2) write code to pass data to the BLL layer.
UserInfo userInfo; LoginManager lm = new LoginManager (); private void btnAdd_Click (object sender, EventArgs e) {userInfo = new UserInfo () {UserName = txtUserName. text. trim (), Password = txtPassword. text. trim ()}; string messageStr =; if (lm. add (userInfo, out messageStr) {MessageBox. show (added successfully);} else {MessageBox. show (messageStr); txtUserName. focus ();}}}