机房重构---七层登录

前言:

从C#版三层登录开始,到VB.NET版三层登录,再到VB.NET版七层登录,一步步走向机房重构的正规化,其中也经历了很多的波折。刚开始不知道从哪里下手,不断的查询各种资料,慢慢了解,终于有了进展,还是蛮开心的。我的建议是如果实在不知道如何下手,没有思路,别停滞不前,不防先从简单的代码敲起,不断敲,不断查询学习,敲着敲着就会慢慢培养出感觉来。

七层构成:

七层和三层相比,多了一个外观层(Facade)、一个接口层(IDAL)、还多了一个工厂层(Factory)。关于sqlHelper可以单独放到一个包中(如下图),也可以和DAL层放到一起。


七层登录:

U层:

接受用户传来的数据,传给外观层,再由外观层传给B层进行相应的判断。
'**********************************************

'文件名:frmLogin

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/2/29 19:40:40

'版本号:V1.0.0.0

'**********************************************
Imports Entity
Imports Facade
Public Class frmLogin
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        '进行输入框的判断
        If txtUserName.Text = "" Then
            MsgBox("请输入用户名!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserName.Focus()
            Exit Sub
        End If
        If IsNumeric(txtUserName.Text) = False Then
            MsgBox("用户名请输入数字!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserName.Text = ""
            Exit Sub
        End If
        If txtPassword.Text = "" Then
            MsgBox("请输入密码!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtPassword.Focus()
            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
            Dim strResult As Boolean
            '将U层的信息传入外观层,然后再有外观层传入B层进行判断
            strResult = Facade.CheckUser(UserInfo)
            If strResult = False Then   '判断用户是否存在
                MsgBox("该用户不存在,请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtUserName.Text = ""
                txtPassword.Text = ""
                txtUserName.Select()
                txtUserName.Focus()
            End If
            Dim table As DataTable
            table = Facade.CheckPwd(UserInfo)
            If Trim(txtPassword.Text) = Trim(table.Rows(0).Item(2)) Then
                MsgBox("登录成功!")
                Me.Hide()
                txtUserName.Text = ""
                txtPassword.Text = ""
            End If
        Catch ex As Exception
            MsgBox("用户不存在或密码不正确!")
            txtPassword.Text = ""
            txtUserName.Text = ""
            txtUserName.Select()
            txtUserName.Focus()
        End Try
    End Sub

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

End Class

Facade层:

解耦U层和B层。
'**********************************************

'文件名:LoginFacade

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/2/29 19:11:40

'版本号:V1.0.0.0

'**********************************************
Imports BLL
Imports Entity
Public Class LoginFacade
    '判断用户是否存在
    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
    '判断密码是否正确
    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层:

'**********************************************

'文件名:LoginBLL

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/2/29 17:30:53

'版本号:V1.0.0.0

'**********************************************
Imports IDAL
Imports Entity
Imports Factory
Public Class LoginBLL
    '检查用户是否存在
    Public Function Checkuser(ByVal UserInfo As Entity.LoginUserInfo) As Boolean
        Dim Factory As New Factory.LoginFactory
        Dim IUser As IDAL.LoginIUserInfo
        '调用创建用户的工厂方法
        IUser = Factory.CreateIUser() '调用工厂的CreateIUser方法创建IUser接口实例
        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层:

'**********************************************

'文件名:LoginFactory

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/2/29 17:36:26

'版本号:V1.0.0.0

'**********************************************
Imports System.Reflection '反射
Imports System.Configuration '配置文件
Imports System.Data
Imports IDAL
Public Class LoginFactory
    Private Shared ReadOnly AssemblyName As String = "DAL" '数据程序集名称,命名空间DAL
    Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("DB")
    Public Function CreateIUser() As LoginIUserInfo
        Dim classname As String = "DAL" + "." + strDB + "LoginDAL" '要实例化的D层的名称
        'Dim classname As String = "DAL" + "." + "LoginDAL"
        Dim IUser As LoginIUserInfo
        'CType函数将返回表达式显示的转换为指定的数据类型、对象、结构、类或接口后的结果
        IUser = CType(Assembly.Load(AssemblyName).CreateInstance(classname), LoginIUserInfo) '返回LoginIUserInfo
        Return IUser
    End Function

End Class

IDAL层:

Imports Entity
Public Interface LoginIUserInfo
    '判断用户是否存在
    Function selectUser(ByVal UserInfo As Entity.LoginUserInfo) As DataTable
End Interface

DAL层:

'**********************************************

'文件名:LoginDAL

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/3/1 15:24:24

'版本号:V1.0.0.0

'**********************************************
Imports Entity
Imports IDAL
Imports SqlHelper
Imports System.Data.SqlClient

    Public Class LoginDAL : Implements IDAL.LoginIUserInfo
        '实现接口中的方法
        Private sqlHelper As New SqlHelper.sqlhelper
        '判断用户是否存在
        Public Function selectUser(UserInfo As LoginUserInfo) As DataTable Implements LoginIUserInfo.selectUser
            Dim sql As String '中间变量,用于存储从数据库中查到的信息
        Dim table As DataTable
        '声明并实例化参数数组
        Dim sqlParams As SqlParameter() = {New SqlParameter("@UserName", UserInfo.UserName),
                                              New SqlParameter("@Password", UserInfo.Password)}
        sql = "select * from Users where UserName=@UserName and Password=@Password"
        '调用sqlHelper类中的ExecSelect方法来执行查询并获取返回值
            table = sqlHelper.ExecSelect(sql, CommandType.Text, sqlParams)
            Return table

        End Function

    End Class

sqlHelper层:

'**********************************************

'文件名:sqlhelper

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/2/29 16:07:28

'版本号:V1.0.0.0

'**********************************************
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class sqlhelper
    '数据库连接
    Dim strConnction As String = "Server=YUAN; Database=StudentInfo;User ID=sa;Password=930201"
    Dim conn As New SqlConnection(strConnction)
    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
        '给cmd赋值
        cmd.CommandText = cmdText 'cmdText为所要执行的sql语句
        cmd.CommandType = cmdType '命令执行的类型
        cmd.Connection = conn
        cmd.Parameters.AddRange(paras) '参数添加
        sqlAdapter = New SqlDataAdapter(cmd) '实例化Adapter
        Try
            sqlAdapter.Fill(ds) '用adapter将dataset填充
            dt = ds.Tables(0) 'datatable为dataSet的第一个表
            cmd.Parameters.Clear() '清除参数
        Catch ex As Exception
            MsgBox("数据库操作")
        Finally
            Call CloseCmd(cmd) '销毁cmd命令
        End Try
        Return dt
    End Function
    Public Sub CloseCmd(ByVal cmd As SqlCommand)
        If Not IsNothing(cmd) Then 'cmd命令存在
            cmd.Dispose() '销毁
            cmd = Nothing
        End If
    End Sub
End Class

Entity层:

'**********************************************

'文件名:LoginEntity

'作者:袁甜梦

'小组:袁甜梦

'说明:

'创建日期:2016/2/29 15:41:43

'版本号:V1.0.0.0

'**********************************************
Public Class LoginUserInfo
    '设置用户名属性
    Private _username As String
    Public Property UserName As String
        Get
            Return _username
        End Get
        Set(value As String)
            _username = value
        End Set
    End Property
    Private _password As String
    '设置密码的属性
    Public Property Password As String
        Get
            Return _password
        End Get
        Set(value As String)
            _password = value
        End Set
    End Property

End Class

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="connstring" value="Server=YUAN;Database=StudentInfo;User ID=sa;Password=930201"/>
  </appSettings >
</configuration>

运行结果:


注意:

在运行之前需要更改DAL的编译路径,否则会出现错误。
错误提示:


路径更改方法:






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 43
    评论
评论 43
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值