Three-tier login form

What is a three-tier architecture?

The three layers are physically divided into: display layer, business layer, and data layer

Logically divided into: UI/BLL + DAL/DB

The three-tier structure we are talking about here is:
UI(User Interface): Display and collect user operations.

BLL(Business Login Layer): Process the business logic, determine the business logic by obtaining the operation instructions from the U layer, and directly hand it over to the D layer for processing when accessing the data source, and return the necessary data to the U layer after the processing is completed.

DAL(Data Access Layer): Only provide the most basic data access.

Entity Model layer: Save entities and transfer data. The physical layer is not included in any layer.
Insert picture description here

Specific application: UI→BLL→DAL
①The assembly where DAL is located does not reference BLL and UI
②BLL needs to refer to DAL
③UI directly refers to BLL, indirectly refers to DAL, and cannot directly drink DAL ④Each
layer needs to refer to the entity Model layer
Insert picture description here

How to write a three-tier architecture (take login as an example)

  1. First, we need to establish four class libraries, including three-tier structure and physical layer
    Insert picture description here

  2. Create a From form in the
    Insert picture description here
    U layer , as shown in the U layer code:

private void btnLogin_Click_1(object sender, EventArgs e)
        {
    
    
            //显示层不能直接跟数据源打交道
            string userName = txtUserName.Text .Trim ();
            string password = txtPassword.Text;
            Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();//构造实例
            Login.Model .UserInfo user = mgr.UserLogin(userName ,password );
            MessageBox.Show("登录用户:" + user.UserName);
        }
  1. Layer B code:
public class LoginManager
    {
    
    
        
        //根据用户的需要,使用了用户登录的方便
            public Login.Model.UserInfo UserLogin(string userName, string Password)
            {
    
    
                //throw new NotFiniteNumberException();
                //需要用到数据源,把数据源调来
                Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();
                Login.Model.UserInfo user = uDao.SelectUser(userName, Password);
                if (user != null)//登陆成功
                {
    
    
                    Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();
                    sDao.UpdateScore(userName, 10);
                    return user;
                }
                else
                {
    
    
                    throw new Exception("登陆失败");
                }
            }
      }

4. Establish a connection
between the D layer and the data. First, you need to create three new classes in the D layer, namely the
Insert picture description hereDUtil class

class DbUtil
    {
    
    
        public static string ConnString = @"Server = LAPTOP-4EF4CAKC; Database = Login ; User ID = sa;Password = 123";
       //数据库的账户和密码
   }

②ScoreDAO class

public class ScoreDAO
    {
    
    //积分对象 增加积分
        public void UpdateScore(string userName, int value)
        {
    
    
            using (SqlConnection conn = new SqlConnection(DbUtil .ConnString))
            {
    
    
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"INSERT INTO SCORES(UserName, Score) Values(@UserName,@Score)";
                cmd.Parameters.Add(new SqlParameter("@UserName", userName));
                cmd.Parameters.Add(new SqlParameter("@Score", value));
                conn.Open(); 
                cmd.ExecuteNonQuery();
                
            }
        }
    }

③UserDAO class

 public class UserDAO
    {
    
    //用户
        public Login.Model.UserInfo SelectUser(string userName, string password)
        {
    
    
            using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))
            {
    
    
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT ID,UserName,Password,Email
                                FROM USERS WHERE UserName = @UserName AND Password = @Password ";
                cmd.CommandType = CommandType.Text;
                //添加
                cmd.Parameters.Add(new SqlParameter("@UserName", userName));
                cmd.Parameters.Add(new SqlParameter("@Password", password));
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                Login.Model.UserInfo user = null;
                while (reader.Read())
                {
    
    
                    if (user==null)
                    {
    
    
                        user = new Login.Model.UserInfo();
                    }
                    user.ID = reader.GetInt32(0);
                    user.UserName = reader.GetString(1);
                    user.Password = reader.GetString(2);
                    if (!reader.IsDBNull (3))
                    {
    
    
                        user.Email = reader.GetString(3);
                    }
                }
                return user;
            }
        }
    }

5. Write entities in the Model layer

namespace Login.Model
{
    
    
    public class UserInfo
    {
    
    //所用到的数据库中的字段
        public int ID {
    
     get; set; }
        public string UserName {
    
     get; set; }
        public string Password {
    
     get; set; }
        public string Email {
    
     get; set; }
    }
}

login successful!
Insert picture description here

Benefits of using three-tier architecture

  1. It avoids the presentation layer directly accessing the data access layer, and the presentation layer is only connected with the business logic layer, which improves data security.
  2. High cohesion and low coupling reduce the dependence between layers.
  3. The project structure is clearer and the division of labor is clearer, which is conducive to later maintenance and upgrades.
  4. Each layer can be developed by different people, as long as they follow the interface standards and use the same object model entity classes, which can greatly increase the development speed of the system.
    (Such as computer room cooperation projects)

Guess you like

Origin blog.csdn.net/weixin_44690047/article/details/109443059