三层架构结合外观模式实现登录

登录功能在设计的时候利用三层架构来分析整个业务实现,同时在UI层和BLL层之间增加一个业务外观层。这样子让两层明显的隔离,表示层的任何变化,比如是用客户端软件还是浏览器方式表示都不会影响到业务与数据的设计。

外观模式:为子系统中一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这个子系统更加容易使用。

这个模式中的高层接口是:Façade类(外观类)需要了解所有子系统的方法和属性,进行组合,以备外界调用

Window应用程序(客户端软件)的登录功能三层中的类分别是:

UI包:LoginForm类

BLL包:LoginBLL类

DAL包:UserInfoDAL类

除此之外加上

Entity包:UserInfoEntity类

Façade包:LoginFaçade类

UML类图:

 

登录界面:


代码实现:

UI层:

Imports System.Data
Imports System
Imports Facade
Imports Entity

Public Class LoginForm
  
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        '实例化useInfo对象.并且设置userInfo的属性
        Dim enUserInfo As UserInfoEntity = New UserInfoEntity()
        enUserInfo.T_UserID = TxtUserName.Text.ToString()
        enUserInfo.T_UserPWD = TxtPWD.Text.ToString()
        '实例化LoginFacade对象,调用login函数来完成登录的工作
        Dim userLoginFacade As New LoginFacade
        If (userLoginFacade.Login(enUserInfo) = True) Then
            MsgBox("登录成功")
        Else
            MsgBox("登录失败")
        End If
    End Sub

End Class

Facade层:

Imports BLL
Imports Entity

Public Class LoginFacade
    '初始化时实例化一个LoginBLL
    Dim userLogin As New LoginBLL

    '调用LoginBLL对象的登录函数
    Public Function Login(ByVal enUser As UserInfoEntity) As Boolean
        Return userLogin.UserLogin(enUser)
    End Function
End Class

BLL层:

Imports Entity               '引用的是命名空间不是具体的类
Imports System.Data.SqlClient
Imports DAL
Imports System.Linq
Imports System.Text

Public Class LoginBLL

    Public Function UserLogin(ByVal enUser As UserInfoEntity) As Boolean

        '实例化一个userinfoDAL对象
        Dim d As UserInfoDAL = New UserInfoDAL()
        '调用userinfDAL的check方法(将前面设置的参数传入check方法中)
        Return d.Check(enUser)

    End Function
End Class

DAL层:这一层中最好写一个DBHepler类将与数据库的连接和修改数据的等对数据库的操作进行封装

Imports System.Data           ' 创建表类型的变量需要引用的空间
Imports System.Data.SqlClient '创建sql数据库连接需要引用的空间
Imports Entity

Public Class UserInfoDAL

    Function Check(ByVal enUser As UserInfoEntity) As Boolean

        '连接数据库
        Dim sqlconnection As SqlConnection
        Dim strConnect As String = "Data Source=PF-PC;Initial Catalog=P_Charge ;User ID=sa;PassWord=123456;"
        sqlconnection = New SqlConnection(strConnect)

        '设置SqlCommand的两个参数
        '存储过程名称
        Dim str As String = "UserPROC"
        '存储过程需要传入的参数
        Dim sqlParameter() As SqlParameter = {
            New SqlParameter("UserID", enUser.T_UserID),
            New SqlParameter("UserPwd", enUser.T_UserPWD)}

        '实例化SqlCommand对象调用ExecuteScalar方法执行存储过程
        Dim com As SqlCommand = New SqlCommand(str, sqlconnection)
        com.CommandType = CommandType.StoredProcedure
        com.Parameters.AddRange(sqlParameter)
        Return Convert.ToString(com.ExecuteScalar())

    End Function


End Class

Entity层:

Public Class UserInfoEntity
    Private userID As String
    Private userName As String
    Private userPWD As String
    Private userLevel As String
    Private loginTime As String
    Private loginDate As String

    Public Property T_UserID As String
        Get
            Return (userID)
        End Get
        Set(ByVal value As String)
            userID = value
        End Set
    End Property

    Public Property T_UserName As String
        Get
            Return (userName)
        End Get
        Set(ByVal value As String)
            userName = value
        End Set
    End Property

    Public Property T_UserPWD As String
        Get
            Return (userPWD)
        End Get
        Set(ByVal value As String)
            userPWD = value
        End Set
    End Property

    Public Property T_UserLevel As String
        Get
            Return (userLevel)
        End Get
        Set(ByVal value As String)
            userLevel = value
        End Set
    End Property

    Public Property T_LoginTime As String
        Get
            Return (loginTime)
        End Get
        Set(ByVal value As String)
            loginTime = value
        End Set
    End Property

    Public Property T_LoginDate As String
        Get
            Return (loginDate)
        End Get
        Set(ByVal value As String)
            loginDate = value
        End Set
    End Property


End Class



你可能感兴趣的:(UML,VB.NET,设计模式,三层架构)