有种开窍的感觉,自己敲的时候按层次走了一遍,思路清晰了很多
一、Entity:实现业务实体,修改密码用到了UserInfo表
namespace Entity
{
public class UserInfo
{
private string userid;
private string password;
private string level;
private string username;
public string Userid { get => userid; set => userid = value; }
public string Password { get => password; set => password = value; }
public string Level { get => level; set => level = value; }
public string Username { get => username; set => username = value; }
}
}
二、IDAL(接口层):定义一个统一的接口,解除B层和D层的耦合,方法是查询和更新
namespace IDAL
{
public interface IModpasswordIDAL
{
DataTable SelectUserInfo(UserInfo UserInfo);//定义接口
//DataTable:可计算的 SelectUserInfo:查询用户信息
DataTable UpdateUserInfo(UserInfo UserInfo);
}
}
三、DAL:实现接口里的方法,直接操作数据库,对数据进行增删改查,只是和数据库直接交互。
namespace DAL
{
public class ModpasswordDAL:IModpasswordIDAL
{
public DataTable SelectUserInfo(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;//返回一个table类型的结果
}
public DataTable UpdateUserInfo(UserInfo UserInfo)
{
SqlHelper sqlhelper = new SqlHelper();
SqlParameter[] sqlParams = { new SqlParameter ("@userid",UserInfo .Userid ),
new SqlParameter("@Password",UserInfo .Password )
};//传参
string sql = "UPDATE User_Info set PassWord=@Password WHERE UserID=@userid";//更新语句
DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;
}
}
}
四、Factory:创建接口,返回接口,用到了抽象工厂+反射+配置文件,实现数据库的连接,方便换数据库,进一步解耦合。
namespace Factory
{
public class ModpasswordFactory
{
string strDB = ConfigurationManager.AppSettings["DB"];//读配置文件
public IDAL.IModpasswordIDAL CreateUser()
{
//DAL层的类名
string ClassName = strDB + "." + "ModpasswordDAL";
//反射+工厂应用
return (IDAL.IModpasswordIDAL)Assembly.Load(strDB).CreateInstance(ClassName);//反射的应用
}
}
}
五、BLL:调用Factory,得到程序集指定类的实例,完成数据操作方法
namespace BLL
{
public class ModpasswordBLL
{
public bool SelectUserInfo(UserInfo UserInfo)
{
bool f;
Factory.ModpasswordFactory fact = new Factory.ModpasswordFactory();//实例化工厂
IDAL.IModpasswordIDAL idal = fact.CreateUser();
DataTable table = idal.SelectUserInfo(UserInfo);//接收D层返回值
if (table.Rows.Count == 0)
{
f = false;
}
else
{
f = true;
}
return f;
}
public DataTable UpdateUserInfo(UserInfo UserInfo)
{
Factory.ModpasswordFactory fact = new Factory.ModpasswordFactory();
IDAL.IModpasswordIDAL idal = fact.CreateUser();
DataTable table = idal.UpdateUserInfo(UserInfo);
return table;
}
}
}
六、Facade:调用BLL,得到BLL层的处理结果返回值。这里用到了外观模式,定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度。
namespace Facade
{
public class ModpasswordFacade
{
public bool SelectUserInfo(UserInfo UserInfo)
{
bool f;
BLL.ModpasswordBLL modpassword = new BLL.ModpasswordBLL();
f = modpassword.SelectUserInfo(UserInfo);
return f;
}
public DataTable UpdateUserInfo(UserInfo UserInfo)
{
BLL.ModpasswordBLL modpassword = new BLL.ModpasswordBLL();//实例化B层
DataTable f = modpassword.UpdateUserInfo(UserInfo);
return f;
}
}
}
七、UI:调用Facade里的数据操作方法,实现功能
自己走了一遍七层,感觉思路清晰了很多,首先是想一想实现功能需要涉及到哪些表,用到什么方法什么逻辑,然后就是一层一层敲代码,发现问题,解决问题