【重构】七层修改密码

关系

七层: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语句的区别了;其实大部分操作都是类似的。

评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值