【机房重构】——七层登陆代码分析

来源:互联网 发布:讲诚信 知礼仪 编辑:程序博客网 时间:2024/06/10 19:01

七层登陆进行了有一段时间,也慢慢的对七层有一点了解,也对登陆有了新的认识,把一个简单的登陆通过不同的层来进行“解耦合”,而加入工厂的作用是可以实现SQL Server和Access数据库的切换,在D层中实现接口,而SQLHelper是封装对数据库的增删改查, Entity实体层作为公共模块在不同层之间进行传递,其UML图如下:




U层:实现用户交互界面,数据采集和显示


Imports FacadePublic Class FrmLogin    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click        '定义一个实体层的对象,用于在各层之间进行传递        Dim User As Login.Entity.UserEntity = New Login.Entity.UserEntity        '定义中间变量UIUser,用于承载实体User        Dim UIUser As Boolean        '定义一个外观层的对象,用于将U层内容传递到Facade层中        Dim FacadeObject As New Facade.LoginFacade        '判断用户名和密码是否为空        If txtUserName.Text.Trim = "" Then            MessageBox.Show("请输入用户名!", "温馨提示")            txtUserName.Text = ""            txtPassword.Text = ""            txtUserName.Focus()            Exit Sub        End If        If txtPassword.Text.Trim = "" Then            MessageBox.Show("请输入密码!", "温馨提示")            txtUserName.Text = ""            txtPassword.Text = ""            txtPassword.Focus()            Exit Sub        End If        Try            '将文本框中内容传递给User实体对象            User.UserID = txtUserName.Text.Trim()            User.Password = txtPassword.Text.Trim()            '将User通过U层传给外观层            UIUser = FacadeObject.FactoryUser(User)            If UIUser = False Then                MessageBox.Show("用户名或密码错误", "温馨提示")                txtUserName.Text = ""                txtPassword.Text = ""                txtUserName.Focus()            Else                MessageBox.Show("登陆成功", "恭喜您")            End If        Catch ex As Exception            MsgBox(ex.Message)            'MsgBox("用户不存在或密码错误")            txtUserName.Text = ""            txtPassword.Text = ""            txtUserName.Focus()        End Try    End SubEnd Class


Facade层:作为中间层,只是起到传递实体对象的作用


Public Class LoginFacade    Public Function FactoryUser(ByVal User As Login.Entity.UserEntity) As Boolean        Dim BLLObject As New Login.BLL.UserBLL  '实例化B层中对象        Dim FacadeUser As Boolean '定义中间变量,用于承载User        '将User通过Facade层传给B层        FacadeUser = BLLObject.BLLUser(User)        Return FacadeUser    End FunctionEnd Class

B层:传递和进行逻辑判断的作用


Public Class UserBLL    '检查用户名是否存在    Public Function BLLUser(ByVal User As Login.Entity.UserEntity) As Boolean        Dim IDALObject As Login.IDAL.UserIDAL        IDALObject = Factory.LoginFactory.CreateUser        Dim table As DataTable '定义数据库表table作为中间变量        Dim BUser As Boolean '定义中间变量,用于承载User        table = IDALObject.IDALUser(User)        '判断是否有记录,如果有返回True,否则False        If table.Rows.Count = 0 Then            BUser = False        Else            BUser = True        End If        Return BUser    End FunctionEnd Class


Factory:反射+配置文件来实现数据库访问,可以实现切换数据库的功能


Public Class LoginFactory     '读配置文件,D层的每个类在配置文件里面对应一个key    '把key变成变量,然后在下面这个方法中用这个变量就可以应用D层里面这个类了    Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("DBString")    Public Shared Function CreateUser() As Login.IDAL.UserIDAL        'CType:内联函数,将前面的部分转换成后面的部分        Return CType(Assembly.Load("LoginDAL").CreateInstance("Login.DAL.UserDAL"), Login.IDAL.UserIDAL)        '                      当前“程序集”的名称       当前“命名空间”名称         'String ClassName=Assembly +"."+db+"User"                                                                                'Return (IUser)Assembly.Load(AssemblyName).CreateInstance(ClassName)    End FunctionEnd Class


IDAL层:此接口定义了一个方法,用来检测用户是否存在


Public Interface UserIDAL    Function IDALUser(ByVal User As Login.Entity.UserEntity) As DataTableEnd Interface


D层:连接数据库


Public Class UserDAL : Implements IDAL.UserIDAL '实现接口中的方法    Public Function IDALUser(User As Entity.UserEntity) As DataTable Implements UserIDAL.IDALUser        Dim SqlHelperObject As New SqlHelper.LoginSqlHelper        Dim Sql As String        Dim table As DataTable '定义数据库表table作为中间变量        '连接数据库        Dim SqlConnectStr As String = "server=HOME-LOVE;database=charge_sys;User ID=sa ;Password=1"        '声明并实例化参数数组,将User.UserID赋值给UserName,User.Password赋值给Password        Dim SqlParams As SqlParameter() = {New SqlParameter("@UserName", User.UserID), New SqlParameter("@Password", User.Password)}        '用SQL语句查询信息        Sql = "select * from User_Info where UserID=@UserName and PWD=@Password"        '调用SqlHepler类中的GetDataTable方法来执行查询        table = SqlHelper.LoginSqlHelper.GetDataTable(Sql, CommandType.Text, SqlParams)        Return table '返回值    End FunctionEnd Class


SqlHelper:封装对数据库的增删改查,只需传入参数即可访问


Public Class LoginSqlHelper    'ConfigurationManager表示配置文件  AppSettings获取配置文件的数据    Public Shared SqlConnectStr As String = ConfigurationManager.AppSettings("sqlConnectStr")    'sqlDataAdapter用于填充Data.DataSet和更新SQL Server数据库的一组数据命令和一个数据库连接    Private Shared Property adaptor As SqlDataAdapter    Public Shared Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable        Dim conn As New SqlConnection(SqlConnectStr) '建立数据库连接        Dim adataset As new DataSet '定义并实例化数据库缓冲区对象,即从数据库传入对象        Dim table As New DataTable  '定义数据库表table作为中间变量        Dim cmd As SqlCommand '定义命令变量        Dim adaptor As SqlDataAdapter '定义数据库适配器        cmd = New SqlCommand(cmdTxt, conn) '在conn上实例化命令对象,并执行cmdtype        cmd.CommandType = cmdType  '执行命令类型        cmd.Parameters.AddRange(sqlParams)  '命令执行时参数        adaptor = New SqlDataAdapter(cmd) '执行命令时的参数        adataset = New DataSet  'dataset表示数据库中的缓冲        Try            '如果数据库关闭则打开            If conn.State = ConnectionState.Closed Then                conn.Open()                adaptor.Fill(adataset)                table = adataset.Tables(0)                cmd.Parameters.Clear()            End If        Catch ex As Exception            MsgBox(ex.Message, "数据库操作")        Finally            '如果连接状态为打开则将其关闭,释放内存            If conn.State = ConnectionState.Open Then                conn.Close()            End If        End Try        Return table '以数据库表形式返回    End FunctionEnd Class


Entity层:定义公共的私有属性


Public Class UserEntity    '定义私有属性    Private _UserID As String    Private _Password As String    '定义属性过程,通过这个允许其他类访问,可读写    Public Property UserID() As String        Get            Return _UserID        End Get        Set(value As String)            _UserID = value        End Set    End Property    Public Property Password() As String        Get            Return _Password        End Get        Set(value As String)            _Password = value        End Set    End PropertyEnd Class


配置文件:和反射一起实现数据库访问


<appSettings>             <add key ="SqlConnectStr" value ="server=HOME-LOVE;database=charge_sys;User ID=sa;Password=1"/>            <add key ="DBString" value ="Sqlserver"/>  </appSettings>



0 0
原创粉丝点击