机房重构---修改密码

来源:互联网 发布:js淘宝购物车脚本之家 编辑:程序博客网 时间:2024/05/16 10:20

前言:


每一个项目里面都需要密码,对于每个人而言,修改密码可能是家常便饭。


内容:


一、下面我们就来说一下要实现这个功能需要注意什么:

1、首先我们需要检查原密码对不对

2、检查两次输入的新密码是否一致


二、实现


1、用到了登录实体(用户名和密码),这里就不多介绍了

2、用到了 SQLHelper ,登录的时候就已经介绍了。

3、接口--工厂和DAl层的桥梁

Imports System.ReflectionImports Entity.EntityPublic Interface LoginIDAL    '检查密码    Function CheckPWD(ByVal UserPWD As Entity.Entity) As DataTable    Function UpdatePWD(ByVal UserPWD As Entity.Entity) As BooleanEnd Interface

4、工厂--在工厂中用到了反射。


Imports System.Configuration   '添加对配置文件的引用Imports System.Reflection        '引用反射Imports IDALImports System.DataPublic Class LoginFactory    Private Shared ReadOnly AssemblyName As String = "DAL"                           '数据程序集名称、命名空间(DAL)    Dim db As String = System.Configuration.ConfigurationSettings.AppSettings("DB")  '配置文件中的名称    Public Function CheckUserInfo() As IDAL.LoginIDAL        'CheckUserInfo()  --新建实例名        'IDAL.LoginIDAL --IDAL.需要实例化的类名        'className需要反射的类型全名(包括命名空间的全路径)        Dim className As String = db + "." + "LoginDAL"  '声明要实例化的D层类的名称          ' Dim classname As String = AssemblyName + "." + db + "DAL"        '将实例化的D层类通过向上转型转换成接口类,然后通过调用接口类中的函数来调用D层中实现该接口的函数。          Return CType(Assembly.Load(AssemblyName).CreateInstance(className), IDAL.LoginIDAL)        'CType函数将返回表达式显示地转换为指定的数据类型、对象、结构、类或接口后的结果     End Function    Public Function ChangPWDFty() As IDAL.LoginIDAL        'CheckUserInfo()  --新建实例名        'IDAL.LoginIDAL --IDAL.需要实例化的类名        'className需要反射的类型全名(包括命名空间的全路径)        Dim className As String = db + "." + "LoginDAL"  '声明要实例化的D层类的名称          ' Dim classname As String = AssemblyName + "." + db + "DAL"        '将实例化的D层类通过向上转型转换成接口类,然后通过调用接口类中的函数来调用D层中实现该接口的函数。          Return CType(Assembly.Load(AssemblyName).CreateInstance(className), IDAL.LoginIDAL)        'CType函数将返回表达式显示地转换为指定的数据类型、对象、结构、类或接口后的结果     End FunctionEnd Class

5、DAL层-- 参数赋值,传参

Imports IDALImports System.Data.SqlClientImports EntityImports SQLHelper'提供对表示ADO.net结构的类的访问Imports System.DataPublic Class LoginDAL : Implements IDAL.LoginIDAL  Public Function CheckPWD(ByVal UserPWD As Entity.Entity) As DataTable Implements IDAL.LoginIDAL.CheckPWD        Dim spl As New SQLHelper.sqlhelper        Dim Dt As DataTable        Dim sqlparas As SqlParameter() = {New SqlParameter("@userID", UserPWD.userID)}        Dim cmdtext As String        cmdtext = "select * from Y_User_Info where UserID =@userID"        Dt = spl.ExecSelect(cmdtext, CommandType.Text, sqlparas)        Return Dt    End Function    Public Function UpdatePWD(ByVal UserPWD As Entity.Entity) As Boolean Implements IDAL.LoginIDAL.UpdatePWD        Dim sql As New SQLHelper.sqlhelper        Dim Dt As New Integer        Dim sqlparams As SqlParameter() = {New SqlParameter("@userID", UserPWD.userID),                                           New SqlParameter("@PWD", UserPWD.PWD)}        Dim sqlV As String = "Update Y_User_Info set PWD=@PWD where userID =@userID "        Dt = sql.ExecAddDelUpdate(sqlV, CommandType.Text, sqlparams)        If Dt > 0 Then            Return True        Else            Return False        End If    End FunctionEnd Class

6、BLL层--进行逻辑判断

Public Class LoginBLL    Public Function CheckOldRWD(ByVal UserPWD As Entity.Entity) As Boolean        Dim Factory As New Factory.LoginFactory        Dim IuserPWD As IDAL.LoginIDAL        Dim table As New DataTable        '调用检查用户的工厂方法        IuserPWD = Factory.ChangPWDFty()        table = IuserPWD.CheckPWD(UserPWD)        If UserPWD.PWD = table.Rows(0).Item(1) Then            Return True        Else            Return False        End If    End Function    Public Function UpdataPWD(ByVal UserPWD As Entity.Entity) As Boolean        Dim Factory As New Factory.LoginFactory        Dim IuserPWD As IDAL.LoginIDAL        Dim table As Boolean        '调用检查用户的工厂方法        IuserPWD = Factory.ChangPWDFty()        table = IuserPWD.UpdatePWD(UserPWD)        If table = True Then            Return True        Else            Return False        End If    End FunctionEnd Class

7、Facade层 ---类似于接口,解耦和

Imports BLLImports EntityPublic Class LoginFacade    Public Function CheckPWD(ByVal UserPWD As Entity.Entity) As Boolean        Dim isUserPWD As New BLL.LoginBLL        Dim table As Boolean        table = isUserPWD.CheckOldRWD(UserPWD)        If table = True Then            Return True        Else            Return False        End If    End Function    Public Function UpdatePWD(ByVal UserPWD As Entity.Entity) As Boolean        Dim chagePWD As New BLL.LoginBLL        Dim table As Boolean        table = chagePWD.UpdataPWD(UserPWD)        If table = True Then            Return True        Else            Return False        End If    End FunctionEnd Class

8、UI层--可进行简单的判断 ,了解一下下面三个语句。

        break(跳出总上一层循环, 不再执行循环(结束当前的循环体))
        continue 跳出本次循环,继续执行下次循环(结束正在执行的循环 进入下一个循环条件)
        return 程序返回,不再执行下面的代码(结束当前的方法 直接返回)

Imports Entity.EntityImports Facade.LoginFacadePublic Class frmChangePWD    Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click        Me.Close()    End Sub    Private Sub cmdOK_Click(sender As Object, e As EventArgs) Handles cmdOK.Click        Dim facpwd As New Facade.LoginFacade        Dim userPWD As New Entity.Entity        '判断原密码是否为空        If txtyuan.Text = "" Then            MsgBox("请输入原密码!", vbOKOnly + vbInformation, "提示")            txtyuan.Select()  '激活控件            txtyuan.Focus()  '设置输入焦点            Return            '判断新密码是否为空        ElseIf txtnew.Text = "" Or txtnew2.Text = "" Then            MsgBox("请输入新密码!", vbOKOnly + vbInformation, "提示")            txtnew.Select()            txtnew.Focus()            Return        End If        ' break(跳出总上一层循环, 不再执行循环(结束当前的循环体))        'continue 跳出本次循环,继续执行下次循环(结束正在执行的循环 进入下一个循环条件)        'return 程序返回,不再执行下面的代码(结束当前的方法 直接返回)        userPWD.userID = Entity.CommonVariable.CommonUserID        userPWD.PWD = txtyuan.Text.Trim()        Dim table1 As Boolean        table1 = facpwd.CheckPWD(userPWD)        If table1 = False Then            MsgBox("旧密码输入错误,请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtyuan.Text = ""            txtyuan.Focus()            txtnew.Text = ""            txtnew2.Text = ""            Return        Else            If Trim(txtnew.Text) <> Trim(txtnew2.Text) Then                MsgBox("输入的两个新密码不相同,请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                txtnew.Text = ""                txtnew2.Text = ""                txtnew.Focus()                Return            Else                userPWD.PWD = txtnew.Text.Trim()                table1 = facpwd.UpdatePWD(userPWD)                If table1 = True Then                    MsgBox("密码修改成功!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                    txtnew.Text = ""                    txtnew2.Text = ""                    txtyuan.Text = ""                    Me.Close()                    Return                Else                    MsgBox("修改密码失败!", 0, "提示")                    ' txtnew.Text = ""                    txtnew2.Text = ""                    txtyuan.Text = ""                    txtnew.Focus()                End If            End If        End If    End Sub    Private Sub frmChangePWD_Load(sender As Object, e As EventArgs) Handles MyBase.Load        txtUserID.Text = Form1.txtUserID.Text    End SubEnd Class


总结:


如果UI层,每一条if语句没有return会出现什么情况,大家试过就知道了。只有尝试之后就会发现其中的好处,不试一试永远发现不了其中的奥秘。





原创粉丝点击