前言
上篇博客刚说完三层架构,那现在来说说七层是怎么回事,刚看到七层的时候自己也是比较懵,就很简单的一个登陆窗体,账号和密码要传过七层在传回来,进过无数次的调试,终于明白了七层了原理。下面我来具体的介绍介绍着七层!
哪七层
- UI(界面层):接收用户输入的数据,经过处理之后再把信息展现给用户。
- Facade(外观层):外观模式,目的是对UI层和B层进行解耦,提供了B层的入口。
- BLL(业务逻辑层):主要对UI层传来的数据进行逻辑判断。
- Factory(工厂层):接收配置文件传来的信息,做到更换数据库只需更改配置文件,实现了B层与D层的数据传递。
- IDAL(接口层):定义了统一接口,对B层和D层进行解耦。
- DAL(数据访问层):主要和数据库打交道,进行相关操作(SQLHelper可以写在DAL层)。
- Entity(实体层):用于各层数据之间的传输。
配置文件
- 找到UI层所在的文件夹下App.config文件打开
- 写上连接语句
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<appSettings>
<add key="DB" value="LoginDAL" />
<add key ="ConnString" value ="Server=.;Database=charge;User ID=sa;Password=123456;"/>
//.代表自己电脑,charge是数据库名字,账号,密码
</appSettings>
</configuration>
如果在UI层所在的文件夹下没有找到App.config文件,不必慌张,只需我们在重新创建一个配置文件即可(如下图)!
这个是SQL Server配置文件。如果是MySQL的话请看我下篇博客!
UI层
private void btnOK_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim ()=="")
{
MessageBox.Show("请输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (txtPassword.Text=="")
{
MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
try
{
LoginEntity.UserInfo user = new LoginEntity.UserInfo(); //实例化实体层
user.UserName = Convert.ToInt32(txtUserName.Text.Trim());
user.Password = txtPassword.Text;
Boolean flag = false; //定义布尔型变量
Facade.LoginFacade FLogin = new Facade.LoginFacade(); //实例化外观层
flag = FLogin.SelectUser(user); //调外观层的方法
if (flag!=false)
{
this.Hide();
this.DialogResult = DialogResult.OK;
Form form = new Form();
form.Show();
}
else
{
MessageBox.Show("用户名或密码不正确","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
catch (Exception )
{
throw;
}
}
外观层
public class LoginFacade
{
public Boolean SelectUser(LoginEntity.UserInfo user)
{
bool flag;
LoginBLL.UserBLL userBLL = new LoginBLL.UserBLL(); //实例化业务逻辑层(B)
flag = userBLL.BLL(user); //调B层的方法
return flag; //返回U层
}
}
BLL层
public class UserBLL
{
public bool BLL(LoginEntity.UserInfo userInfo)
{
Factory.LoginFactory fact = new Factory.LoginFactory(); //实例化工厂层
LoginIDAL.IuserInfoDAL idal = fact.CreatUser(); //实现接口
DataTable table = idal.SelectUser(userInfo); //调SelectUser方法
bool flag;
if (table.Rows.Count == 0) //判断是否有数据
{
flag = false;
}
else
{
flag = true;
}
return flag; //返回外观
}
}
工厂层
public class LoginFactory
{
string StrDB = ConfigurationManager.AppSettings["DB"]; //接收配置文件
public LoginIDAL.IuserInfoDAL CreatUser()
{
string ClassName = StrDB + "." + "UserDAL";
return (LoginIDAL.IuserInfoDAL)Assembly.Load(StrDB).CreateInstance(ClassName); //反射
}
}
接口层
public interface IuserInfoDAL
{
DataTable SelectUser(LoginEntity.UserInfo UserInfo);
}
DAL层
public class UserDAL:IuserInfoDAL
{
public DataTable SelectUser(LoginEntity.UserInfo UserInfo)
{
sqlHelper sqlhelper = new sqlHelper();
SqlParameter[] sqlparams = { new SqlParameter("@UserName", UserInfo.UserName), new SqlParameter("@Password", UserInfo.Password) };
string sql = @"select * from user_info where UserName=@UserName and Password =@Password";
DataTable table = sqlhelper.ExecuteQuery(sql, sqlparams, CommandType.Text); //SQLHelper层查询
return table; //返回B层
}
}
SQLHelper
public class sqlHelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
public sqlHelper()
{
string ConnString = ConfigurationManager.AppSettings["ConnString"];
conn = new SqlConnection(ConnString);
}
private SqlConnection GetConn()
{
if (conn.State == ConnectionState.Closed) //做判断
{
conn.Open();
}
return conn;
}
public int ExecteNonQuery(string cmdText, CommandType ct)
{
int res;
try
{
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return res;
}
public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdText, GetConn()))
{
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
public DataTable ExecuteQuery(string cmdText, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
public DataTable ExecuteQuery(string cmdtext, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdtext, GetConn()); //调用GetConn查看数据库是否连接的方法
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt; //返回D层
}
}
Entity层
public class UserInfo //各层数据传输
{
public int UserName { get; set; }
public string Password { get; set; }
}
总结
通过一步一步调试,慢慢体会七层之间值是怎样传递的以及每层的作用,可能有人会问,难道没出现什么问题或BUG吗?那你大错特错,下篇博客会把自己敲的BUG都总结一篇。
感谢您的阅读!