Computer Room Reconfiguration-Seven-Layer Login

Posted by we4freelance on Sat, 29 Jun 2019 00:40:03 +0200

[Preface]

The seven-tier login has gone through a lot of hardships, but now it has been achieved, the mood is still happy do not want to drop.

[text]

Why?

In fact, the seven layers add four layers on the basis of the three layers, mainly for the purpose of decoupling, so that the data between the layers is not directly connected, but transmitted through the seven layers.

What?

UI Layer: Receive and display user's requirements, pass parameters to the appearance layer.

Facade layer: Pass parameters, separate UI layer from BLL layer. In fact, it uses a look-and-feel mode to decouple layer B from layer U.

BLL layer: judgment and processing of main business.

Factory layer: Use Abstract Factory + reflection + configuration file, so that the database can be replaced at any time. As well as converting the class of the DAL layer into the interface implementation of the IDAL layer, the BLL layer can access the DAL layer through the IDAL layer.

IDAL: Data access layer, decoupling BLL layer and DAL layer. It's about providing interfaces.

DAL Layer: Data Layer, which checks the addition, deletion and modification of data.

Entity: The entity layer runs through the layers and connects them. It stores data, facilitates the transfer of data between layers, and is mostly used as a parameter of methods in classes.

SqlHelper layer: mainly DAL encapsulation and abstraction.

How?

Let's look at the seven-tier package first.

Next, take a look at the sequence diagram of the seven-tier login


Next, take a look at the implementation code

U level

Public Class frmLogin
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        'Judge whether it is empty
        If txtUserName.Text = "" Then
            MsgBox("enter one user name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserName.Focus()
            Exit Sub 'Selective midway roll-out of in-process code execution
        End If
        If txtPassWord.Text = "" Then
            MsgBox("Please input a password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If IsNumeric(txtPassWord.Text) = False Then
            MsgBox("please enter a number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Try
            Dim Facade As New Facade.LoginFacade
            Dim UserInfo As New Entity.LoginUserInfo
            UserInfo.UserName = txtUserName.Text.Trim()
            UserInfo.PassWord = txtPassWord.Text.Trim()
            Dim strResult As Boolean
            'take U Layer user information is passed into the appearance layer, and then through the appearance layer. B Judgment by layers
            strResult = Facade.CheckUser(UserInfo)
            If strResult = False Then
                MsgBox("user name does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtUserName.Text = ""
                txtPassWord.Text = ""
                txtUserName.Select()
                txtUserName.Focus()
                Exit Sub
            End If

            Dim table As DataTable
            table = Facade.CheckPWD(UserInfo)

            If Trim(txtPassWord.Text) = Trim(table.Rows(0).Item(1)) Then
                MsgBox("Successful login")
                Me.Hide()
                txtPassWord.Text = ""
                txtUserName.Text = ""
            End If
        Catch ex As Exception
            MsgBox("Username or password incorrect")
            txtPassWord.Text = ""
            txtUserName.Text = ""
            txtUserName.Select()
            txtUserName.Focus()
            Exit Sub
        End Try
    End Sub 'Exit of a process

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

Public Class LoginFacade
    'Check if the username exists
    Public Function CheckUser(ByVal UserInfo As Entity.LoginUserInfo) As Boolean
        Dim IsUserExists As New BLL.LoginBLL
        Dim flag As Boolean
        flag = IsUserExists.CheckUser(UserInfo)
        If flag = True Then
            Return True
        Else
            Return False
        End If
    End Function

    'Check if the password exists
    Public Function CheckPWD(ByVal UserInfo As Entity.LoginUserInfo) As DataTable
        Dim IsPWDExists As New BLL.LoginBLL
        Dim table As DataTable
        table = IsPWDExists.CheckPWD(UserInfo)
        Return table
    End Function

End Class
BLL level

Public Class LoginBLL
    Public Function CheckUser(ByVal UserInfo As Entity.LoginUserInfo) As Boolean
        Dim Factory As New Factory.LoginFactory
        Dim IUser As IDAL.LoginIUserInfo
        'Call the factory method that creates the user
        IUser = Factory.CreateIUser()
        Dim table As New DataTable
        Dim flag As Boolean
        table = IUser.selectUser(UserInfo)
        If table.Rows.Count = 0 Then
            flag = False
        Else
            flag = True
        End If

        Return flag
    End Function

    Public Function CheckPWD(ByVal UserInfo As Entity.LoginUserInfo) As DataTable
        Dim Factory As New Factory.LoginFactory
        Dim IUser As IDAL.LoginIUserInfo
        Dim table As DataTable

        IUser = Factory.CreateIUser
        table = IUser.selectUser(UserInfo)

        Return table

    End Function

End Class
Factory layer

Imports System.Reflection 'Add a reference to reflection
Imports System.Configuration 'Add a reference to the configuration file
Imports System.Data
Imports IDAL 'Reference interface layer

'reflex+configuration file+Data access by abstract factories

Public Class LoginFactory


    Private Shared ReadOnly AssemblyName As String = "DAL" 'Assembly name
    'You can change the type of database by reading configuration files
    Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("DB")


    Public Function CreateIUser() As LoginIUserInfo
        Dim classmate As String = AssemblyName + "." + strDB + "LoginDAL" 'To instantiate D The name of the class of the layer
        Dim IUser As LoginIUserInfo
        'CType Function returns the result of an expression's conversion to a specified data type, object, structure, class, or interface
        IUser = CType(Assembly.Load(AssemblyName).CreateInstance(classmate), LoginIUserInfo) 'Return LoginIUserInfo
        Return IUser
    End Function

End Class
IDAL Layer

Public Interface LoginIUserInfo
    'Determine whether the username is correct
    Function selectUser(ByVal UserInfo As Entity.LoginUserInfo) As DataTable

End Interface
DAL level

Imports System.Data.SqlClient
Public Class LoginDAL : Implements IDAL.LoginIUserInfo
    'Method in Implementing Interface
    Private sqlHelper As New SqlHelper.SqlHelper
    'Determine whether the user is correct
    Public Function selectUser(UserInfo As Entity.LoginUserInfo) As DataTable Implements IDAL.LoginIUserInfo.selectUser
        Dim sql As String 'Intermediate variables, used to store information found from the database
        Dim table As DataTable
        'Declare and instantiate parameter arrays
        Dim sqlParams As SqlParameter() = {New SqlParameter("@userID", UserInfo.UserName), New SqlParameter("@PWD", UserInfo.PassWord)}
        sql = "Select * from User_Info where userID=@userID and PWD=@PWD"
        'call SqlHelper In the class GetDataTable()Method to execute the query and get the return value  
        table = sqlHelper.ExecSelect(sql, CommandType.Text, sqlParams)

        Return table


    End Function
End Class
SqlHelper layer

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class SqlHelper

    'Database Connection  
    Dim strConnection As String = "Server=qq;Database=JF;User ID=sa;Password=123456"
    Dim conn As New SqlConnection(strConnection)
    Dim cmd As New SqlCommand

    Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable
        Dim sqlAdapter As SqlDataAdapter
        Dim dt As New DataTable
        Dim ds As New DataSet
        'to cmd assignment  
        cmd.CommandText = cmdText
        cmd.CommandType = cmdType
        cmd.Connection = conn
        cmd.Parameters.AddRange(paras)  'Parameter addition  
        sqlAdapter = New SqlDataAdapter(cmd)  'instantiation Adapter  
        Try
            sqlAdapter.Fill(ds)  'dataset Fill adapter use 
            dt = ds.Tables(0)   'datatable by dataSet First table   
            cmd.Parameters.Clear()  'Clearance parameters  

        Catch ex As Exception
            MsgBox("Database operation")
        Finally
            Call CloseCmd(cmd)  'Destruction cmd command  
        End Try

        Return dt

    End Function
    Public Sub CloseCmd(ByVal cmd As SqlCommand)
        If Not IsNothing(cmd) Then  'If cmd Command exists  
            cmd.Dispose()           'Destruction  
            cmd = Nothing
        End If
    End Sub

End Class
Entity layer

Public Class LoginUserInfo
    Private _username As String
    Public Property UserName As String  'Define user name attributes
        Get
            Return _username
        End Get
        Set(value As String)
            _username = value
        End Set
    End Property

    Private _password As String
    Public Property PassWord As String   'Define password attributes
        Get
            Return _password
        End Get
        Set(value As String)
            _password = value
        End Set
    End Property
End Class

[Summary]

The Road on the seventh floor has just begun, and there is still a long way to go, but once a line is completed, it will be simple in the future.













Topics: Database SQL