了解
前面了解了三层架构,今天聊聊七层架构。七层架构比三层多出了Facade层、Factory层、IDAL接口、Entity层。
- Facade:外观层,对UI与BLL之间解耦,BLL之间的逻辑判断较多,内部比较复杂,用外观去调用BLL层中UI需要的逻辑,在通过外观将BLL返回的值返回UI。
- Factory:工厂类,这里运用的是工厂+反射,只要有工厂的地方都可以用反射。通过把实例化数据库的操作交给外部容器去完成,这样,我们需要更改数据库的连接时,只需从配置文件中进行改动即可。从而使编程更方便、灵活。
- IDAL:接口,存放DAL数据访问层需要实现的接口。
- SQLHelper:在DAL层中进行调用SQLHelper,主要存放的是封装对数据库调用的一些代码,这样就提高了我们代码的灵活性!SQLHelper不可以当做一层看待,存在的目的是服务于DAL。
- Entity:它与数据库中的表字段相对应,主要是封装一些功能性代码,定义一些实体类型和实体集合,用于各个层次传递参数。
命名空间
- using System.Data:顾名思义,引用这个dll即表示你的命名空间下有需要使用数据、数组的地方,可以直接使用数组类型,而不需要再添加前缀。
- using System.Data.SqlClient:表示在你的代码中引入微软发布的sqlserver数据库的ado.net程序集,引入后,你就可以使用SqlConnection,SqlCommand,SqlDataReader,SqlParameter等数据库对象来访问sqlserver数据库。
- using System.Configuration:使用ConfigurationManager等对象提供对客户端应用程序配置文件的访问。注意:当用ConfigurationManager类时,光引用命名空间是不能使用的,还要在解决方案资源管理器中右击引用->添加引用->程序集->选中system.Configuration.
- using System.Reflection:使用Assembly对象时,一般使用反射机制时引用。
UI
private void btnLogin_Click(object sender, EventArgs e)
{
//接收用户输入字符串
if (txtUserName!=null)
{
string userName = txtUserName.Text.Trim();
}
if (txtPassword!=null)
{
string passWord = txtPassword.Text;
}
try
{
//实例化工厂和实体层
Facade.LoginFacade fa = new Facade.LoginFacade();
Entity.UserInfo user = new Entity.UserInfo();
//将用户名、密码传入实体层user
user.UserId = Convert.ToInt32(txtUserName.Text);
user.PassWord = txtPassword.Text;
//调用外观的方法,返回user
Boolean flag = false;
flag = fa.SelectUser(user);
if (flag != false)
{
this.Hide();
CustomersInfo CI = new CustomersInfo();
CI.Show();
}
else
{
MessageBox.Show("密码或用户名错误");
}
}
catch (Exception)
{
MessageBox.Show("用户名或密码不能为空,请重新输入", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Facade
public class LoginFacade
{
public Boolean SelectUser(Entity.UserInfo user)
{
bool flag;
BLL.LoginManager man = new BLL.LoginManager();
flag = man.UserBLL(user);
return flag;
}
}
BLL
public class LoginManager
{
public bool UserBLL(Entity.UserInfo userInfo)
{
Factory.LoginFactory fact = new Factory.LoginFactory();
IDAL.LoginIDAL idal = fact.CreateUser();//调取工厂中方法,利用反射机制实例化DAL层
DataTable table = idal.selectUser(userInfo);//调取DAL层的方法
bool flag;
if (table.Rows.Count==0)
{
flag = false;
}
else
{
flag = true;
}
return flag;
}
}
Factory
public class LoginFactory
{
//引用配置文件键DB对应的值DAL
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];//ConfigurationManager类需要在引用中添加systems.Configuration
public IDAL.LoginIDAL CreateUser()
{
string ClassName = StrDB + "." + "LoginDAL";
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);//Assembly.Load()在给定程序集的情况下,加载该程序集;CreateInstance()方法:使用默认构造函数创建指定类型的实例;使用反射机制也就是将原来实例化的new DAL.LoginDAL这一步交给了外部容器去做,现在需要更改实例化对象时,只需更改配置文件即可。使用更方便灵活
}
//将DAL属性的路径改为UI/bin/Debug
}
IDAL
public interface LoginIDAL
{
DataTable selectUser(Entity.UserInfo userInfo);
}
DAL
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) };//使用SqlParameter防止Sql注入,更安全;(添加的参数名,添加参数的数据类型)
string sql = @"SELECT * FROM T_User WHERE uid=@UserId and pwd =@PassWord";//查询sql数据
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);//将参数传入sqlHelper类
return table;//返回表到BLL层
}
}
SQLHelper
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) //枚举ConnectionState,用于描述数据源当前的连接状态
{
conn.Open();//连接数据库
}
return conn;
}
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();//实例化数据库内存中的一个表
cmd = new SqlCommand(cmdText, GetConn()); //cmdText:查询的文本;getConn():连接数据库的方法
cmd.CommandType = ct;//指定命令对象的执行形式,默认为text
cmd.Parameters.AddRange(paras);//将参数加入到参数集中
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) //在执行命令时,将关联 Connection 对象已关闭时关联 DataReader 对象已关闭
{
dt.Load(sdr); //将提供的数据源中的值填充到DataTable,如果已存在,则合并
}
return dt; //返回表到DAL层
}
}
Entity
public class UserInfo
{
public int UserId { get; set; }
public string PassWord { get; set; }
}
配置文件
<appSettings>
<add key="ConnStr" value="server=MAX;dataBase=chargesystem;uid=sa;pwd=123456"/>
<add key="DB" value="DAL"/>
</appSettings>