Three-tier basis

What are three

Three-tier architecture (3-tier architecture) is the entire business application is divided into:

  • Interface layer (User Interface Layer)

role : to show to the user-specific traffic data acquisition and operation of the user's input
[PS: design principles: customer first, taking into account the simplicity]

  • The business logic (Business Logic Layer)

role: mainly responsible for the operation of the data layer, data layer it some operations combined.

  • Data Access Layer (Data Access Layer)

effect: and dealing with data sources, for Select, Insert / Update, Delete operation.

Three purpose
  • High cohesion, low coupling
When to use three

When to use three :
to a certain extent, when you need to concentrate when complex business when your program stored in a separate data storage corresponding database: the data access from the business, the business from the UI. In this case: UI only need to call the service access layer can be used.

Specific applications of three
  1. D layer provides only basic data access, does not contain any business logic related.
  2. U layer is only responsible for collecting and displaying a user operation, does not contain any business logic related to
  3. B layer is responsible for processing service logic. UI by obtaining operation instruction transmitted, decides to perform logic operations, when required to directly access the data source layer D process. When processing is complete, return the necessary data to the U layer.
Three advantages and disadvantages

advantage:

★ developers can focus on the entire structure in which a layer
★ can easily be replaced with a new realization of the original level implementation.
★ can reduce the dependence between the layers
★ favor of standardized
★ favor of layers of logic multiplexing
★ structure clearer
★ when post-maintenance, greatly reducing maintenance costs and maintenance space

Disadvantages:

☆ reduce the performance of the system. It is self-evident. If you do not use a hierarchical structure, many businesses can directly visit the database, in order to obtain the corresponding data, but now it must be done through an intermediate layer.
☆ sometimes lead to a cascade of modification. This modification is reflected in the direction from top to bottom. If you need to add a function in the presentation layer, as their design hierarchy, you may need to have increased the corresponding code in the corresponding business logic and data access layer.
☆ increase development costs.

References between three

Here Insert Picture Description

Three of the code

U level code:

private void BTLogin_Click(object sender, EventArgs e)
{ 
    try
    //对try块代码进行异常捕捉,
    //如无异常则进行直try块结束,
    //如有异常则跳转进入catch块。
    {
        string userName = txtUserName.Text.Trim();
        string password = txtPassword.Text;
        Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();
        //将用户输入的数据传给BLL,在下面一句的使用中,就直接从BLL调用数据
        Login.Model.UserInfo user = mgr.UserLogin(userName, password);
        //在这里就可以返回model里的userinfo
        MessageBox.Show("登陆用户:" + user.UserName);
    }
    catch (Exception ex)
    //处理异常。如下进行处理
    {
        MessageBox.Show(ex.Message);
    }
}

Code B layer

public class LoginManager
    {
        public Login.Model.UserInfo UserLogin(string userName,string password)
            //获取UI传来的指令UserLogin和数据Username还有password,可以返回model里的userinfo了
        { 
            
            Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();
            Login.Model.UserInfo user = uDao.SelectUser(userName, password);
            //调用userDao的selectUser方法,返回Model里的UserInfo

            if (user != null)//登录成功
            {
                Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();
                sDao.UpdateScore(userName, 10);
                //调用ScoreDAO里的UpdateScores增加积分的方法,选择用户名,增加十个积分
                return user;
            }
            else
            {
                throw new Exception("登陆失败。");
                //抛出异常
            }
        }
    }

Layer D Code

public class ScoreDAO
    {
        public void UpdateScore(string userName, int value)
            //写一个增加积分的方法,从B层获取数据
        {
            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();

            }
        }
    }

public class UserDAO
    {
        public Login.Model.UserInfo SelectUser(string userName, string password)
            //写一个方法,根据用户名和密码判断,返回一个数据模型,
        {
            using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))
            //有了上面的这个using ,connection就可以自动关闭了,
            {
                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);
                    //将数据库第一列的数据以Int32的格式返回
                    user.UserName = reader.GetString(1);
                    //将数据库第二列的数据以Int32的格式返回
                    user.Password = reader.GetString(2);
                    //将数据库第三列的数据以int32的格式返回

                    if (!reader.IsDBNull(3))
                    {
                        user.Email = reader.GetString(3);
                    }

                }
                return user;

            }
        }
    }
Published 64 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/YaraRen/article/details/103393116