一步一个脚印~
本篇文章仅为七层学习参考,熟悉七层走向,不涉及重构登陆。(缺失判断逻辑)
正确图:
这个包图很清晰的表示七层在三层的基础上增加了facade,factory,IDAL层,Enitity。其实这里的enitity就代表着实体层,在三层里我们已经用过了,就是将User类封装起来。也就是这里所说的实体层。那么我们今天要学习的也就又少了一个。增加的其他三层作用是什么呢?
从图上我们可以很清晰的知道这三层也是为了减少耦合的。
注意:先后问题。如果先写U层,它会涉及到调用,所以在没写B层之前,U层是无法完成的。写代码顺序应从箭头指向的反方向来写。
1. Enitity(实体层)
2. IDAL(接口层)
3. DAL(数据访问层)
→app配置
4. Factory(工厂层)
5. BLL(业务逻辑层)
6. Facade(外观曾)
7. UI(界面层)
Enitity(实体层)
public class userinfo
{
public string UserID { get; set; }
public string PWD { get; set; }
public string UserName { get; set; }
public string Level { get; set; }
public string state { get; set; }
}
IDAL(接口层)
using System.Data;//引用
namespace IDAL
{
public interface LoginIDAL
{
DataTable selectUser(Entity.userinfo user);
}
}
DAL(数据访问层)
代码如下:
<appSettings>
<add key ="connStr" value ="server=LOVQG\SQLEXPRESS;database=Login_sys;uid=sa;pwd= 123"/>
<add key ="DB" value ="DAL"/>
appSettings>
DAL类:
using System.Data; //引用
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
public class LoginDAL:IDAL.LoginIDAL
{
public DataTable selectUser(Entity.userinfo user)
{
SQLHelper sqlHelper = new SQLHelper();
//参数
SqlParameter[] sqlparams = { new SqlParameter("@UserID", user.UserID), new SqlParameter("@PassWord", user.PWD) };
string sql = @"SELECT * FROM [User_Info] WHERE UserID =@UserID AND PWD = @PassWord";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlparams, CommandType.Text); //(sql, CommandType.Text, sqlparams);
return table;
}
}
}
SQLHelper类:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
public class SQLHelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
//----------------------
public SQLHelper()
{
//连接
string connStr = ConfigurationManager.AppSettings["connStr"];
conn = new SqlConnection(connStr);
}
//----------------------
private SqlConnection Getconn() //需有返回值
{
if (conn.State == ConnectionState.Closed)
{
conn.Open(); //打开数据库
}
return conn;
}
//---------------------
///
/// 执行带参数的查询SQL语句或存储过程
///
/// 查询SQL语句或存储过程
/// 参数集合
/// 命令类型
///
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(工厂层)
using System.Reflection;//引用反射
using System.Configuration;
namespace Factory
{
public class LoginFactory
{
//添加引用集
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public IDAL.LoginIDAL CreateUser()
{
//反射+工厂的应用----------添加引用using System.Reflection;
string ClassName = StrDB + "." + "LoginDAL";//DAL的类名 StrDB+
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
}
BLL(业务逻辑层)
using System.Data;
namespace BLL
{
public class LoginBLL
{
public DataTable UserBLL(Entity.userinfo user)
{
Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂
IDAL.LoginIDAL idal = fact.CreateUser();//调用工厂方法创建接口
DataTable table = idal.selectUser(user);//接受D层的返回值
return table;
}
}
}
Facade(外观层)
public class LoginFacade
{
public DataTable SelectUser(Entity.userinfo user)
{
DataTable flag;
BLL.LoginBLL UserBLL = new BLL.LoginBLL();
flag = UserBLL.UserBLL(user);//返回值
return flag;
}
}
UI(界面层)→未判断权限~
private void buttonOK_Click(object sender, EventArgs e)
{
//判空
if (txtUserID.Text.Trim() == "") //1.判断用户名和密码是否为空,一行代码解决
{
MessageBox.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (txtPassWord.Text == "")
{
MessageBox.Show("密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtPassWord.Focus();
}
else
{
Entity.userinfo user = new Entity.userinfo(); //实例化用户
user.UserID = txtUserID.Text.Trim(); //接收信息
user.PWD = txtPassWord.Text; //
Facade.LoginFacade FLogin = new Facade.LoginFacade(); //3.实例化外观将参数传递给外观层
DataTable flag = FLogin.SelectUser(user); //4.调用外观的方法,返回给user
#region//判断登陆
if (flag.Rows.Count != 0) //-------为什么不用布尔值,bool无法返回rowx
{
MessageBox.Show("小主,您可以进来了", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Form a = new Form();
a.Show();
}
else
{
MessageBox.Show("用户名或密码错误");
txtUserID.Text = "";
txtPassWord.Text = "";
txtUserID.Focus();//获得焦点
}
#endregion
}
}
错误集锦链接
完成这个登陆还真是不容易啊。感谢大神们的帮助与指点~