关系
七层:Entity、IDAL、DAL、Factory、BLL、Facade、UI,其引用关系如下图所示:
重构新建文件的顺序:Entity->IDAL->DAL->Factory->BLL->Facade->UI
示例
下边已修改密码为例,了解七层架构
Entity
public class UserInfo
{
public string UserID { get; set; }
public string PassWord { get; set; }
}
IDAL
public interface PasswordIDAL
{
int updateUser(Entity.UserInfo UserInfo);
}
DAL
public class PasswordDAO:IDAL.PasswordIDAL
{
public int updateUser(Entity.UserInfo UserInfo)
{
SQLHelper sqlHelper = new SQLHelper();
SqlParameter[] sqlParams = { new SqlParameter("@UserId", UserInfo.UserID), new SqlParameter("@Password", UserInfo.PassWord) };
string sql = @"update Users set Password=@Password where UserID=@UserId";
int res = sqlHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text);
return res;
}
}
Factory
public class LoginFactory
{
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public IDAL.PasswordIDAL UpdateUser()
{
string ClassName = "DAL" + "." + "PasswordDAO";
return (IDAL.PasswordIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
BLL
public class PasswordManager
{
public bool UserPassword(Entity.UserInfo UserInfo)
{
Factory.LoginFactory fact = new Factory.LoginFactory();
IDAL.PasswordIDAL idal = fact.UpdateUser();
int res = idal.updateUser(UserInfo);
bool result;
if (res > 0)
{
result = true;
}
else
{
result = false;
}
return result;
}
}
Facade
public class PasswordFacade
{
public Boolean updateUser(Entity.UserInfo user)
{
bool flag;
BLL.PasswordManager pwdBll = new BLL.PasswordManager();
flag = pwdBll.UserPassword(user);
return flag;
}
}
UI
if (txtOldPassword.Text == "")
{
MessageBox.Show("请输入原密码");
}
else
{
if (txtNewPassword.Text == "")
{
MessageBox.Show("请输入新密码");
}
else
{
if (txtNewPasswordConfirm.Text == "")
{
MessageBox.Show("请确认新密码");
}
else
{
if (txtOldPassword.Text != frmLogin.password)
{
MessageBox.Show("原密码输入错误");
}
else
{
if (txtNewPassword.Text != txtNewPasswordConfirm.Text)
{
MessageBox.Show("新密码俩次输入不一致");
}
else
{
Facade.LoginFacade Facade = new Facade.LoginFacade();
Entity.UserInfo user = new Entity.UserInfo();
user.UserID = frmLogin.userid;
user.PassWord = txtNewPassword.Text;
Boolean result = false;
Facade.PasswordFacade FPassword = new Facade.PasswordFacade();
result = FPassword.updateUser(user);
if (result != false)
{
MessageBox.Show("修改密码成功");
}
}
}
}
}
}
总结
关于重构的操作,其实主要分为俩类,一种是查询,是用的DataTable;一种是增删改,使用的int;然后就是SQL语句的区别了;其实大部分操作都是类似的。