前言
恐惧源于无知
尤其是在我们学习中,你之所以感觉它难!并不是因为它真的难,而是因为你不了解它!
所以七层登陆,始于三层,循序渐进,未觉其难
七层
所谓七层,其实只是在基础的三层上面又详细划分了三层,加上一直都在的实体层,一共七层!
1. 显示层(UI)
2. 外观层(Facade)
实现了外观模式,作为UI和BLL的桥梁,解除了它们之间的耦合
3. 逻辑层(BLL)
4. 接口层(IDALL)
在这个层次上,定义了一个统一的接口,解除BLL层和DAL层之间的耦合
5. 数据访问层(DAL)
在此有拓展一个SQLHelper层,主要使封装对数据库调用的一些代码,这样就提高了我们代码的灵活性!
6. 工厂层(Factory)
实现了工厂模式+反射,使数据库的修改更加简便,通过修改配置文件实现数据库的修改
7. 实体层(Entity)
主要是封装一些功能性代码,定义一些实体类型和实体集合,用于各个层次传递参数
UML图
代码
实践出真知,上代码!
UI
private void btnOK_Click(object sender, EventArgs e)
{
//判断输入不能为空
if (txtUserID.Text.Trim() == "")
{
MessageBox.Show("没有输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (txtPassWord.Text == "")
{
MessageBox.Show("没有输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
try
{
//实例化一个外观
Facade.LoginFacade Facade = new Facade.LoginFacade();
//实例化一个用户
Entity.UserInfo user = new Entity.UserInfo();
//接收控件传来的用户名信息
user.UserID = Convert.ToInt32(txtUserID.Text.Trim());
//接受空间传来的密码信息
user.PassWord = txtPassWord.Text;
Boolean flag = false;//定义要给bool型的变量
Facade.LoginFacade FLogin = new Facade.LoginFacade();//实例化外观
flag = FLogin.SelectUser(user);//调用外观的方法,返回给user
if (flag != false)
{
MessageBox.Show("登陆成功!");
this.Hide(); //隐藏当前窗体
this.DialogResult = System.Windows.Forms.DialogResult.OK;
FrmMain a = new FrmMain();//实例化一个窗体
a.Show(); //显示实例化的窗体
}
else
{
MessageBox.Show("密码或者用户名错误");
}
}
catch (Exception)
{
throw;
}
}
Facade
public Boolean SelectUser(Entity.UserInfo user)
{
bool flag;
BLL.LoginBLL userBLL = new BLL.LoginBLL();
flag = userBLL.UserBLL(user);
return flag;
}
BLL
public bool UserBLL(Entity.UserInfo UserInfo)
{
//实例化工厂
Factory.LoginFactory fact = new Factory.LoginFactory();
//调用工厂方法创建接口
IDAL.LoginIDAL idal = fact.CreateUser();
//接受D层的返回值
DataTable table = idal.selectUser(UserInfo);
bool flag;
//返回的DataTable类型,如果它的行数等于0,说明没有符合该帐号密码的用户
if (table.Rows.Count == 0)
{ flag = false; }
else
{
flag = true;
}
return flag;
}
IDAL
public interface LoginIDAL
{
//定义一个选择用户的抽象方法
DataTable selectUser(Entity.UserInfo UserInfo);
//定义一个选择学生的抽象方法
DataTable selectStudent(Entity.StudentInfo StudentInfo);
}
DAL
public DataTable selectUser(Entity.UserInfo UserInfo)
{
//实例化一个数据查询的对象
SQLHelper sqlHelper = new SQLHelper();
//选择要查询的数据
SqlParameter[] sqlParams = { new SqlParameter("@userID", UserInfo.UserID), new SqlParameter("@PassWord", UserInfo.PassWord) };
//插叙语句
string sql = @"SELECT * FROM [User_Info] WHERE userID=@UserID and passWord =@PassWord";
//将参数放入语句中
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
//得到返回值
return table;
}
SQLHelper
public class SQLHelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
}
Factory
public class LoginFactory
{
//接收来自配置文件的数据
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public IDAL.LoginIDAL CreateUser()
{
//DAL层的类名
string ClassName = StrDB + "." + "LoginDAL";
//反射加工厂的应用
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
Entity
public class UserInfo
{
//定义 用户ID 字段
private int userid;
public int UserID
{
get { return userid; }
set { userid = value; }
}
//定义 用户名 字段
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
//定 密码 字段
private string password;
public string PassWord
{
get { return password; }
set { password = value; }
}
//定义 等级 字段
private string level;
public string Level
{
get { return level; }
set { level = value; }
}
//定义 状态 字段
private bool stat;
public bool state
{
get { return stat; }
set { stat = value; }
}
App.config
<appSettings>
<add key ="ConnStr" value="server=SQL的登陆名字或用.表示本地(可能不行); database=数据库的名字;user ID = sa ; pwd=123456"/>
<add key ="DB" value="DAL" />
</appSettings>
注:
- 在调用系统函数的时候,一定要调用命名空间
- 各个层次之间引用一定理清,防止出现循环依赖
- 类和方法的命名一定要有自己的规则,现在类和方法少,等以后多了,你就会感受规则的好处了
- 细节决定一切
美好
希望本文对您有所帮助!
有什么不足!欢迎指正!
感觉不错可以赞一下哦!