包图之间的关系更加形象的描绘了各个层之间的关系。除了D层与接口层之间是实现关系,其他包之间都是依赖关系。同时也表明了在代码中如何编写的问题。(UML包图了解)【此资源来自百度,如有侵权,还请告知,定马上删除】
三层是学习七层的基础,将三层学习的很精,在学习七层的时候就会很容易,七层只是在三层的基础上多了外观层,接口层,工厂层和实体层。外观层的存在是为了解除解除了B层与U层之间的耦合,接口层的存在是为了解除B层与D层的耦合,而实体层在学习三层的时候也出现了,工厂层是为了提高代码的灵活性。
Entity层
//定义变量
private string _userid; //用户ID
private string _username;//用户名字
private string _password;//密码
//封装变量
public string UserId
{
get
{
return _userid;
}
set
{
_userid = value;
}
}
public string Username
{
get
{
return _username;
}
set
{
_username = value;
}
}
public string Password
{
get
{
return _password;
}
set
{
_password = value;
}
}
DAL层
loginDAL
public class LoginDAL : IDAL.LoginIDAL
{
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 SQLHelper()
{
string connStr = System.Configuration.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;
}
IDAL层
public interface LoginIDAL
{
DataTable selectUser(Entity.UserInfo userInfo);
}
Factory层
public class LoginFactory
{
string strDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public IDAL.LoginIDAL CreateUser()
{
string ClassName = strDB + "." + "LoginDAL";
return (IDAL.LoginIDAL)Assembly.Load(strDB).CreateInstance(ClassName);//反射的应用
}
}
BLL层
public class LoginBLL
{
public bool UserBLL(Entity.UserInfo UserInfo)
{
Factory.LoginFactory fact = new Factory.LoginFactory(); //实例化工厂
IDAL.LoginIDAL idal = fact.CreateUser();//调用工厂方法创建接口
DataTable table = idal.selectUser(UserInfo);
bool flag;
if (table.Rows.Count==0)
{
flag = false;
}
else
{
flag = true;
}
return flag;
}
}
Facade层
public class LoginFacade
{
public Boolean selectUser(Entity.UserInfo user)
{
bool flag;
BLL.LoginBLL userBLL = new BLL.LoginBLL();
flag = userBLL.UserBLL(user);
return flag;
}
}
UI层
if (txtUserNme.Text == "")
{
MessageBox.Show("请您输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUserNme.Focus();
}
else if (txtPassWord.Text == "")
{
MessageBox.Show("请您输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtPassWord.Focus();
}
else
{
try
{
//实例化实体类
Facade.LoginFacade loginFacade = new Facade.LoginFacade();
//DataTable UserBack = new DataTable();
Entity.UserInfo User = new Entity.UserInfo();
//将输入的用户名和密码赋值给定义的变量
User.UserId = Convert.ToString(txtUserNme.Text.Trim());
User.Password = Convert.ToString(txtPassWord.Text.Trim());
Boolean flag = false;
flag = loginFacade.selectUser(User);
if (flag != false)
{
this.Hide();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
frmMain frmMain = new frmMain();
frmMain.Show();
}
else
{
MessageBox.Show("用户名或者密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
throw ex;
}
七层登陆是一个简单练手的功能,也是对七层架构的初步认识,通过看“巨人们”的博客,让我对七层有了更多的了解。