Three-tier login
Preface
What is a three-tier architecture
The various functional modules, such as the presentation layer (UI), business logic layer (BLL), and data access layer (DAL)
, use interfaces to access each other, and use the entity class (Model) of the object model as data transfer Carrier.
①, presentation layer (UI)
Interface that interacts with the user, used to receive the data entered by the user and return the data that the user needs
②, business logic layer (BLL)
Responsible for connecting the UI layer and the DAL layer can act as a bridge, processing business logic, making logical judgments, calculations, and so on.
③, data access layer (DAL)
Dealing with the database, mainly for adding, deleting, modifying and checking data
Why use a three-tier architecture
The purpose of using the three-tier architecture is: decoupling and reducing the degree
of coupling . The division of labor between each layer is clear, and changes in any one layer will not affect the other layer.
For example:
Waiter (UI layer) asks for leave-find another waiter, chef (BLL layer) resigns-find another chef, buyer (DAL layer) resigns-find another buyer
Advantages :
1. Clear structure, between each layer Low coupling
2. Strong ability to adapt to changes,
3. Maintainable, expandable, and reusable
text
Three-tier login example
Figure
Code
UI layers
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim(); //将txtUserName文本框中去除空格的内容赋值个userName变量
string password = txtPassWord.Text; //将txtPassWord文本内容赋值给password变量
string msg; //声明字符串变量msg
msg = ""; //变量msg为空
Login.BLL.LoginManager mgr = new Login.BLL.LoginManager(); //实例化BLL层
Login.Model.UserInfo user = mgr.UserLogin(userName, password, out msg); //mgr调用UserLogin方法获取用户名和密码对user赋值并跳转到BLL
MessageBox.Show(msg);
}
BLL layer
public class LoginManager
{
public Login.Model.UserInfo UserLogin(string userName, string password, out string msg)//创建一个返回值为Login.Model.UserInfo类型参数为userName,password,msg的UserLogin方法
{
//throw new NotImplementedException();
Login.DAL.UserDAO uDao = new Login.DAL.UserDAO(); //实例化DAL层的UserDAO
Login.Model .UserInfo user= uDao.SelectUser(userName, password); //跳转到DAL层,把当前用的userName,password传给实体层user对象
if (user != null)//login successfully. //如果user为空
{
//Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO(); //实例化DAL层的ScoreDAO
//sDao.UpdateScore(userName, 10); //调用sDao的UpdateScore方法添加userName和10个积分
msg = "登录成功"; //给msg赋值 登陆成功
}
else //否则
{
msg = "登录失败"; //给msg赋值 登陆失败
}
return user; //返回 user
}
}
Physical layer (Moudel)
public class UserInfo
{
public int ID {
get; set; }
public string UserName {
get; set; }
public string Password {
get; set; }
public string Email {
get; set; }
}
DAL layer
public class DbUtil
{
public static string Connstring =@"Server=wangwei;Database=Login;User ID=sa;Password=wangwei";//数据库链接
}
public class UserDAO
{
public Login.Model.UserInfo SelectUser(string userName,string password)
{
using (SqlConnection conn = new SqlConnection(DbUtil.Connstring)) //使用using,完成之后自动关闭SqlConnection。
//通过SqlConnection连接具体数据库
{
SqlCommand cmd = conn.CreateCommand(); //创建一个数据库的操作对象cmd
cmd.CommandText = @"SELECT ID,UserName ,Password,Email
FROM USERS WHERE UserName=@UserName AND Password=@Password"; //获取需要执行的sql语句
cmd.Parameters.Add(new SqlParameter("@UserName", userName)); //设置参数用户名
cmd.Parameters.Add(new SqlParameter("@Password", password)); //设置参数密码
cmd.CommandType = CommandType.Text;
conn.Open(); //打开数据库连接
SqlDataReader reader = cmd.ExecuteReader(); //将 CommandText 发送到 Connection,并生成 SqlDataReader,并且进行逐条读取
Login.Model.UserInfo user = null; //构造user默认值使null
while (reader.Read()) //循环读取数据,当没有数据推出循环
{
if (user == null) //如果user=null
{
user = new Login.Model.UserInfo(); //实例化实体层
}
user.ID = reader.GetInt32(0); //将返回结果集第一个字段并且为整型赋值给user的ID属性
user.UserName = reader.GetString(1); //读取用户名并赋值给user的UserName属性
user.Password = reader.GetString(2);
if (!reader.IsDBNull(3)) //是否为空
{
user.Email = reader.GetString(3); //
}
}
return user; //返回 user
}
}
}
public class ScoreDAO
{
public void UpdateScore(string userName,int value) //创建UpdateScore方法参数为userName和value
{
using (SqlConnection conn = new SqlConnection(DbUtil.Connstring)) //创建数据库连接对象
{
SqlCommand cmd = conn.CreateCommand(); //创建数据库的操纵对象cmd
cmd.CommandText = @"INSERT INTO SCORES (UserName,Score)Values(@UserName,@Score)"; //将需要执行的sql语句赋值个cmd
cmd.Parameters.Add(new SqlParameter("@UserName", userName)); //获取userName
cmd.Parameters.Add(new SqlParameter("@Score", value)); //获取value
conn.Open(); //数据库连接打开、
cmd.ExecuteNonQuery(); //对连接执行 Transact-SQL 语句并返回受影响的行数。
}
}
}