Three three-tier architecture --- Login

  • What are the three?

  Division is divided into three physical and logical partitioning

Physical partitioning Logical partitioning
Display layer / business layer / data layer UI, BLL, DAL
  • What is the function of each layer?

UI layer (presentation layer):

The main display page used to store the user's interaction with the principles: customer first, taking into account simple

Note: In normal use, the UI layer need to add a reference to the object BLL layer, such that the UI layer and the connection is established between the BLL.

The BLL (business logic):

       For storage of the data logic processing code for specific issues

       Obtained from the DAL data for the UI display, acquiring user instructions and data from the UI, execute business logic

The DAL (Data Access Layer):

Mainly used to store code for operating the raw data  

DAL only provides basic data access, does not contain any business logic related

  • The relationship between the three

In the three-layer structure, the interdependence between the layers, the presentation layer dependent on the business logic, business logic depends on the data access layer

 

 

  •  Why use three, what good is it?

Three coupling is very low, regardless of which layer changes need to occur, is only necessary to change the layer, you do not need to change the entire system

Level clear, low coupling between each layer.

  • for example:

Waiter : only used to receive guests 

Chef : Just point the guests responsible for food

Buyer : according to the requirements of the guests a la carte food procurement

They are responsible for their own work, the waiter just need to know the needs of the guests do not need to know how the chef is cooking, you do not need to know the buyer

Chefs do not need to know which customers received only need to know what to do dishes 

Buyers do not need to know the specific needs of the customers do not know how to cook, just need to know need to buy ingredients.

Specific process:

 

Customers and staff directly dealing with customers and waiters (UI layer) said: I want a fried eggplant, fried eggplant and is not responsible for the waiter, she took up the request submitted,

Passed to cook (BLL layer), need to cook eggplant, put up the submitted request is transmitted to the buyer (DAL layer),

After doing fried eggplant, then back to the waiter, waiter eggplant presented to the customer.

I do not know we have not found a thing in the video is this: While our study is three-tier architecture, but baffling more of a Model Template

In fact, this template play "Shangcuan jump" role, throughout the three-tier, passing parameters between three

 

--------- three landing analysis:

Studied video, then follow the teacher knocked a three-login form

1. First look at the UI layer :

public void button1_Click(object sender, EventArgs e)
        {
            
            //用来获取登陆界面的信息  
            string userName = txtUserName.Text.Trim();
            string password = txtPassword.Text;//密码就不需要添加trim 因为密码里可能会有空格
            
            //需要和逻辑B层建立连接
            LoginManager mgr = new LoginManager();//相当于构造了一个实例,实例化
            //把信息传递给建立的模型中
            UserInfo user = mgr.UserLogin(userName, password);
            
            //uI层返回业务层传递得数据给用户
            MessageBox.Show("登录用户:" + user.UserName);
        }

UI layer:

Need to be loaded in the namespace when we use the MessageBox.Show method:

 using System.Windows.Forms;

If this statement is an error, then we need to add a plug-in project references:

2. BLL layer :

 public class LoginManager
    {
        public  LoginModel.UserInfo UserLogin(string userName, string password)
        {
           
            //实例化D层对象,传输数据
            UserDAO uDao = new UserDAO();
            //通过user表去查询是否可以登陆成功,是否有用户信息
            LoginModel.UserInfo user=uDao.SelectUser(userName, password);
            //如果用户信息不为空,那么就需要在ScoreDAO表中更新数据
            if (user != null)// login successfully;
            {
                //实例化一个sDao的实例
                LoginDAL.ScoreDAO sDao = new LoginDAL.ScoreDAO();
                //通过sDao类的UpdateScore函数来实现更新的操作
                sDao.UpdateScore(userName, 10);

                return user;
            }
            else
            {
                throw new Exception("登陆失败");//作用是抛出错误,用这个抛出错误的原因让使用者知道为什么
               
            }
            

        }
    }

Meaning specific code has been written in the comments.

throw new Exception ( "login failed"), when we enter a user name or password does not meet the requirements of the time will jump here, an error occurs

Of course, we also can be replaced ( "login failed") with Messagebox.show.

 

3. DAL layer :

using System.Data.SqlClient;

/*
表示在你的代码中引入微软发布的sqlserver数据库的ado.net程序集,引入后,
你就可以使用SqlConnection、SqlCommand等数据库对象来访问sqlserver数据库。 
 */
namespace LoginDAL
{//登陆积分
   public  class ScoreDAO
    {
        public void UpdateScore(string userName, int value)
        {
            //SqlConnection 用来连接数据库
            using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))
            {
                //实例化一个cmd类对象来对数据库进行存储操作
                SqlCommand  cmd = conn.CreateCommand();
                cmd.CommandText = @"INSERT INTO SCORES(UserName,Score) Values(@UserName,@Score)";
                //cmd.Parameters.Add(new SqlParameter(@"ID", ID));
                //将 userName 传递给 userName
                cmd.Parameters.Add(new SqlParameter(@"userName", userName));
                //将value传递给Score;
                cmd.Parameters.Add(new SqlParameter(@"Score", value));
                conn.Open();
                //开始执行 cmd  开始插入
                cmd.ExecuteNonQuery();

            }

        }
    }
 public  class UserDAO
    {
        public  LoginModel.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();

                LoginModel.UserInfo user = null;
                while (reader.Read())
                {
                    if (user == null)
                    {
                        user = new LoginModel.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;
            }
        }

    }

Knowledge points:

 

  • In 1.C # sql statement written before why should add an @ symbol

 

sql statements preceded by @ After wrapping prevents C # error. Back @ change no matter how many lines, have said is a string.

 

  • 2.using System.Data.SqlClient; // What is the role of this statement?

/*

Redistributes ado.net assemblies Microsoft released sqlserver database in your code, after the introduction,

You can use the SqlConnection, SqlCommand and other database objects to access sqlserver database.

*/

 

  • 3. using () action:

 

In using () in the construction of the object will automatically shut down and release when the end of {}, you do not need to manually.

这样你不用每次都要去调用conn.Close(),或者conn.Dispose()了,这样能减少人为引入Bug的机会。相当于定义了一个范围,当代码执行完毕就会跳出大括号,而跳出大括号之后就等于出了using的这个区域,

 

4.模板Model

namespace LoginModel
{
    public class UserInfo

    {
        //一系列属性
        public int ID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
    }
}

 

 用来接受参数,并且传参。

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_30631063/article/details/90810175