七层登录实现

七层登录实现


          机房重构刚开始的七层登录,刚开始其中有很多的地方都不理解,后来通过查资料,看博客,问同学慢慢的明白一点了。

          七层就是在三层的基础之上,添加了外观层——设计模式的外观模式,抽象工厂加反射,接口层。

          外观层:主要是把UI和BLL层分离。

          抽象工厂加反射:主要是为了更换数据库的方便性,同时把DAL层中的类转换成为IDAL层中的接口,从而使BLL层实现通过调用IDAL从而调用DAL层。

          接口层:把BLL和DAL分离。

          这样,当需要改动系统的时候,非常的方便,使用七层做到了大大的解耦和,当有新的需求出现的时候或者在后期的系统维护时,仅仅需要改动某一层就可以,或者具体到某一层中的某一个类就可以,而不至于牵一发而动全身,提高了系统的可扩展性,可维护性和灵活性。


七层登录实现_第1张图片


          UML中一共有四大关系,即关联、依赖、泛化和实现,这里用到了三种,即关联、依赖和实现。

          七层从UI层开始,逐层依赖于下一层,除了SqlHelper和Factory之外的所有的层,都要添加对Entity层的引用,这里是关联关系,因为所有的层在使用实体层的时候,首先都先要对实体进行实例化,即New一个实体,所以暂时理解为关联关系(最普通的关联关系,不是聚合也不是组合关系),而DAL层实现的是IDAL层中的方法,同时依赖于SqlHelper实现它自己的功能。

          各层的具体代码:

          UI层

<span style="font-size:24px;"><span style="font-size:24px;">Public Class FrmLogin

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

        '判断文本框是否为空
        If txtUserID.Text = "" Then
            MessageBox.Show("请您输入用户名!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserID.Focus()
            Exit Sub
        ElseIf txtPWD.Text = "" Then
            MessageBox.Show("请您输入密码!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtPWD.Focus()
            Exit Sub
        End If

        '实例化实体类
        '把用户名和密码放到实体中
        Dim User As New Entity.T_UserEntity
        User.UserID = txtUserID.Text.Trim()
        User.PWD = txtPWD.Text.Trim()

        Dim Result As String
        Dim facade As New Facade.LoginFacade
        Result = facade.Login(User)

        '用户名和密码验证的几种情况
        Select Case Result
            Case "此用户不存在"
                MsgBox("此用户不存在,请重新输入用户名!")
                txtUserID.Focus()
                txtUserID.SelectAll()
            Case "密码错误"
                MsgBox("密码错误,请重新输入密码!")
                txtPWD.Focus()
                txtPWD.SelectAll()
            Case "登陆成功"
                MsgBox("恭喜您,登录成功!")
                NowUser = txtUserID.Text.Trim()
                FrmMain.Show()
                Me.Hide()
        End Select

    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        End
    End Sub

End Class</span></span>

          Façade层

<span style="font-size:24px;"><span style="font-size:24px;">Public Class LoginFacade
    Public Function Login(ByVal User As Entity.T_UserEntity) As String
        Dim Userbll As New BLL.LoginBLL

        '调用B层的方法验证用户名和密码是否正确
        If Userbll.CheckUser(User) = False Then
            Return "此用户不存在"
        Else
            If Userbll.CheckPWD(User) = False Then
                Return "密码错误"
            Else
                Return "登陆成功"
            End If
        End If
    End Function

End Class</span></span>

          BLL层

<span style="font-size:24px;"><span style="font-size:24px;">Public Class LoginBLL
    '验证用户名是否正确
    Public Function CheckUser(ByVal user As Entity.T_UserEntity) As Boolean
        Dim Iuser As IDAL.LoginIDAL
        Dim factory As New Factory.LoginFactory
        Iuser = factory.CreateUserDAO()

        Dim mylist As New List(Of Entity.T_UserEntity)
        mylist = Iuser.SelectUserID(user)
        If mylist.Count = 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '验证密码是否正确
    Public Function CheckPWD(ByVal password As Entity.T_UserEntity) As Boolean
        Dim Iuser As IDAL.LoginIDAL
        Dim factory As New Factory.LoginFactory
        Iuser = factory.CreateUserDAO

        Dim mylist As New List(Of Entity.T_UserEntity)
        mylist = Iuser.SelectPWD(password)
        If mylist.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

End Class</span></span>

          Factory层

<span style="font-size:24px;"><span style="font-size:24px;">Imports System.Reflection
Imports System.Configuration

'连接数据库时采用了抽象工厂加反射
'抽象工厂,负责数据库的选择和创建接口
Public Class LoginFactory
    Dim DB As String = System.Configuration.ConfigurationSettings.AppSettings("DBss")
    Private Shared ReadOnly assemblyName = "DAL"


    Public Function CreateUserDAO() As IDAL.LoginIDAL
        '返回接口
        Dim className As String = assemblyName + "." + DB + "LoginDAL"
        Return CType(Assembly.Load(assemblyName).CreateInstance(className), IDAL.LoginIDAL)

    End Function
End Class</span></span>

          IDAL层

<span style="font-size:24px;"><span style="font-size:24px;">'接口IDAL
Public Interface LoginIDAL
    Function SelectUserID(ByVal User As Entity.T_UserEntity) As List(Of Entity.T_UserEntity)
    Function SelectPWD(ByVal User As Entity.T_UserEntity) As List(Of Entity.T_UserEntity)
End Interface</span></span>

          DAL层

<span style="font-size:24px;"><span style="font-size:24px;">Imports System.Data
Imports System.Data.SqlClient
Imports Entity

'D层用来将接口实例化
Public Class SqlServerLoginDAL : Implements IDAL.LoginIDAL

    Public Function SelectUserID(User As Entity.T_UserEntity) As List(Of Entity.T_UserEntity) Implements IDAL.LoginIDAL.SelectUserID

        Dim sqlParams As SqlParameter() = {New SqlParameter("@UserID", User.UserID)}
        Dim strSql As String = "select*from T_User where UserID=@UserID"

        '实例化SqlHelper
        Dim helper As New SqlHelper

        Dim table As New DataTable
        Dim mylist As New List(Of Entity.T_UserEntity)
        table = helper.GetDataTable(strSql, CommandType.Text, sqlParams)
        mylist = ConvertHelper.ConvertList(Of Entity.T_UserEntity)(table)

        '返回泛型
        Return mylist

    End Function

    Public Function SelectPWD(User As Entity.T_UserEntity) As List(Of Entity.T_UserEntity) Implements IDAL.LoginIDAL.SelectPWD

        Dim sqlParams As SqlParameter() = {New SqlParameter("@UserID", User.UserID), New SqlParameter("@PWD", User.PWD)}
        Dim strSql As String = "select*from T_User where UserID=@UserID and PWD=@PWD"

        '实例化SqlHelper
        Dim helper As New SqlHelper

        Dim table As New DataTable
        Dim mylist As New List(Of Entity.T_UserEntity)
        table = helper.GetDataTable(strSql, CommandType.Text, sqlParams)
        mylist = ConvertHelper.ConvertList(Of Entity.T_UserEntity)(table)

        '返回泛型
        Return mylist

    End Function
End Class</span></span>

          Entity层

<span style="font-size:24px;"><span style="font-size:24px;">Public Class T_UserEntity
    '定义变量
    Private _UserID As String
    Private _PWD As String
    Private _Level As String
    Private _UserName As String
    Private _Head As String

    Public Property UserID As String
        Get
            Return _UserID
        End Get
        Set(value As String)
            _UserID = value
        End Set
    End Property

    Public Property PWD As String
        Get
            Return _PWD
        End Get
        Set(value As String)
            _PWD = value
        End Set
    End Property

    Public Property Level As String
        Get
            Return _Level
        End Get
        Set(value As String)
            _Level = value
        End Set
    End Property

    Public Property UserName As String
        Get
            Return _UserName
        End Get
        Set(value As String)
            _UserName = value
        End Set
    End Property

    Public Property Head As String
        Get
            Return _Head
        End Get
        Set(value As String)
            _Head = value
        End Set
    End Property
End Class</span></span>


          这里虽然使用了泛型,但是,并没有任何的好的作用,就是完全没有必要,直接定义一个Datatable返回一个table即可,使用并返回一个Boolean值也可以解决问题,因为用户登录仅仅需要判断和验证输入的用户名和密码是否正确就行。

          泛型返回的是整张数据表,比如查询正在上机的所有学生等等,需要的是一个数据表中所有的数据时,才使用泛型。


你可能感兴趣的:(七层登录实现)