前言
前期了解三层架构主要是由UI层、BLL层和DAL层三部分构成。看到大牛们都采用三层的思想实现了登录,本菜鸟暗暗地站在了他们的肩膀上。
自己理解
对于三层自己的理解是:就像我们对一个大型的公司去找人门口一定会有门卫,假设你就是一个用户,要去公司找该总经理洽谈业务。这时你会来到门口(UI层),要进去找人,门卫(BLL层)说:“你是谁?你找谁?干什么?等等各种问题。”然后接着就去查询档案(DAL层)。确定总经理是否认识这么个人。就这样“UI层——>BLL层——>DAL层”,然后DAL层给予回应见还是不见;传输层为:“DAL层——>BLL层——>UI层”。
这样门卫因为自己根本不认识这个人,所以不能因为不让他见总经理而得罪他,起到了一个“桥”的作用。在软件开发中,这样有利于减少代码之间的耦合度。从而方便了后期的维护,大大减轻了软件危机的到来。很棒吧~~~
三层架构优缺点
优点
1、符合开闭原则,便于用新的实现去替换原有的实现;
2、降低层与层之间的耦合度;
3、便于后期的复用;
4、标准化,看着舒服;
不足点
1、降低了系统的性能;
2、有时候会导致级联修改;还拿上面的例子来说:若你想再向总经理传达一件事情,可能还需要通过门卫也就是说UI层增加一些功能,BLL层和DAL层可能为了附和这个功能去添加相应的代码;
以上得出;在用三层架构的时候,合适的也是最好的,不要滥用。
C#版三层登录
Entity实体层
<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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; }
}
}</span>
D层
<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Login.DAL
{
public class DbUtil
//server是服务器的名称可以使Local,IP地址,计算机名称;
//DataBase是数据库的名称;
//User ID是登录服务器的用户名;
//Password是登录服务器的密码;
{
public static string ConnString = @"Server=ZLT;Database=Login;User ID=sa;Password=123";
下面是用windows身份验证模式链接数据库的;
//public static string ConnString = @"server=.;Database=Login;Integrated Security=ture";
}
}</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace Login.DAL
{
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,Email
FROM USERS WHERE UserName=@UserName AND Password=@Password";//SQL语句,查询表USERS
//cmd.CommandType = Commandtype.Text;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@UserName", userName)); //为参数USERNAME,PASSWORD赋值;
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 (2))
{
user.Email = reader.GetString(2);
}
}
return user;//将实体返回B层;
}
}
}
}</span>
B层
<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Login.BLL
{
public class LoginManager
{
public Login.Model.UserInfo UserLogin(string userName, string password)
{
Login.DAL.UserDAO uDao = new Login.DAL.UserDAO(); //引用D层的UserDAO类
Login.Model.UserInfo user = uDao.SelectUser(userName, password); //引用实体层的SelectUser方法
if (user != null)
{
return user; //将实体user返回到U层
}
else
{
throw new Exception("登录失败。");//给系统错误进行提示;
}
}
}
}
</span>
U层
<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Login.UI;
namespace LoginUI
{
public partial class 系统登录 : Form
{
public 系统登录()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
//LoginBLL. svr = new LoginBLL.LoginService();
string userName = txtUserName.Text.Trim();//向实体层的属性写入数据
string password = txtPassword.Text;
Login.BLL.LoginManager manage = new Login.BLL.LoginManager();//引用实体层
Login.Model.UserInfo user = manage.UserLogin(userName, password);//将UI层的用户名和密码通过B层传到实体层;
MessageBox.Show("登录用户:" + userName);
}
}
}</span>