七层是哪七层呢,看一个图。
1、UI:收集输入的数据然传给外观层,再由外观层传给BLL层进行逻辑判断。
2.Facade:降低UI层和BLL层之间的耦合。
3、BLL:B层进行业务逻辑判断,调用工厂中的方法创建相应的接口,接收D层传来的值。
4、DAL:实现接口层定义的接口。
SQLHelper:将D层中连接数据库代码放在一起,因为是可以复用的,可以减少代码量。
5、Factory:通过配置文件和抽象工厂来geng更改要访问的数据库。定义一个接口调用接口层,实现BLL层和DAL层之间的数据传递。
6、IDAL:定义一个借口,减少BLL层和DAL层的耦合。
7、Entity:用来再各层之间传递数据,定义成员变量。
代码部分
1.UI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UI
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void btnLogin_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
{
Facade.LoginFacade Facade = new Facade.LoginFacade(); //实例化一个外观
Entity.UserInfo user = new Entity.UserInfo(); //实例化一个用户
user.UserID = Convert.ToInt32(txtUserName.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("登录成功");
}
else
{
MessageBox.Show("密码或者用户名错误");
txtUserName.Clear();
txtPassword.Clear();
}
}
catch (Exception)
{
throw;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void LoginForm_Load(object sender, EventArgs e)
{
//使窗体居中
this.CenterToScreen();
}
}
}
2.Facade
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Facade
{
public class LoginFacade
{
public Boolean SelectUser(Entity.UserInfo user)
{
bool flag;
BLL.LoginBLL userBLL = new BLL.LoginBLL();
flag = userBLL.UserBLL(user);
return flag;
}
}
}
3、BLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace 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); //接收D层的返回值
bool flag;
if (table.Rows.Count == 0) //返回的DataTable类型,如果它的行数等于0,说明没有符合该帐号密码的用户
{ flag = false; }
else
{ flag = true; }
return flag;
}
}
}
4、DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class LoginDAL : IDAL.LoginIDAL
{
public DataTable selectUser(Entity.UserInfo UserInfo)
{
SQLHelper sqlHelper = new SQLHelper(); //实例化一个SQLHelper
SqlParameter[] sqlParams = { new SqlParameter("@userID", UserInfo.UserID), new SqlParameter("@Password", UserInfo.PassWord) };
string sql = @"SELECT * FROM [user] WHERE UserID=@userID and PassWord =@Password";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;
}
}
}
SQLHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;
}
/// <summary>
/// 执行不带参数的的增删改SQL语句或者存储过程
/// </summary>
/// <param name="cmdText">增删改查SQL</param>
/// <param name="ct">命令类型</param>
/// <returns>返回受影响的行数</returns>
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;
}
/// <summary>
/// 执行带参数的的增删改SQL语句或者存储过程
/// </summary>
/// <param name="cmdText">增删改查SQL</param>
/// <param name="paras">要查询的参数</param>
/// <param name="ct">命令类型</param>
/// <returns>返回受影响的行数</returns>
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;
}
}
}
5、Factory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Reflection;
namespace Factory
{
public class LoginFactory
{
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"]; //接收来自配置文件的数据
public IDAL.LoginIDAL CreateUser()
{
string ClassName = StrDB + "." + "LoginDAL"; //DAL层的类名
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName); //反射+工厂的应用
}
}
}
6、IDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace IDAL
{
public interface LoginIDAL
{
DataTable selectUser(Entity.UserInfo UserInfo);
}
}
7、Entity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 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; }
}
}
}