加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB.NET七层登录

发布时间:2020-12-16 23:51:30 所属栏目:大数据 来源:网络整理
导读:导言: 之前已经用VB.NET将三层的登录窗体实现成功,后听说现在代码基本上很少用三层实现,基本上都是用七层,于是就自己再用了大概一周的时间整理好一个七层的登录窗体,现在将我的学习方法简单介绍一下,希望能给读者带去帮助 介绍: 所谓三层,之前有讲:

导言:

之前已经用VB.NET将三层的登录窗体实现成功,后听说现在代码基本上很少用三层实现,基本上都是用七层,于是就自己再用了大概一周的时间整理好一个七层的登录窗体,现在将我的学习方法简单介绍一下,希望能给读者带去帮助

介绍:

所谓三层,之前有讲:即UI、BLL、DAL这三层

如今在三层的情况下进阶到七层,就将代码之间的联系更加细分化了,现在的七层是:UI、Facade、BLL、Factory、Interface、DAL、Entity, 不过有些人愿意将DAL分开,分成DAL和SQLHelper这两层

可以看出,将DAL分成DAL和SQLHelper两层的华,将代码的细分层度再一次加高,从而使得代码的耦合情况降低,现在我就站在将DAL分成DAL和SQLHelper两层的角度来介绍代码:

分层:

UI(用户界面层)

User Interface的简称,指用户的操作界面。
Public Class Form1
    Private Sub btnNo_Click(sender As Object,e As EventArgs) Handles btnNo.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(sender As Object,e As EventArgs) Handles btnOK.Click
        Dim UserInfo As New Entity.ELogin                                                   '实例化实体层UserInfo对象,用于在各层之间进行传递数据
        Dim strResult As Boolean                                                            '定义变量接受Facade层的返回值
        Dim Facade As New Facade.FLogin                                                     '实例化外观层Facade对象,用于在各层之间进行传递数据 
        If (txtName.Text = "" Or txtPWD.Text = "") Then                                                           '判断用户名是否为空
            MsgBox("用户名或密码不能为空!",MsgBoxStyle.Information + MsgBoxStyle.OkOnly,"提示")
            txtName.Focus()
        End If
        UserInfo.Name = txtName.Text.Trim                                                   '将用户名文本框的内容传递给dataUser对象
        UserInfo.PWD = txtPWD.Text.Trim                                                     '将密码文本框的内容传递给dataUser对象
        strResult = Facade.CheckUser(UserInfo)                                              '将U层的用户信息传入外观层,然后通过外观层传入Facade层
        Try
            If strResult = False Then                                                       '判断是否存在输入的用户  
                MsgBox("用户不存在")
                txtName.Text = ""
                txtPWD.Text = ""
                txtName.Focus()
            Else
                MsgBox("恭喜你,登陆成功!")
            End If
        Catch ex As Exception
            MsgBox("登陆处出错")
            txtName.Text = ""
            txtPWD.Text = ""
            txtName.Focus()
        End Try
    End Sub
    Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load
        Me.ActiveControl = Me.txtName                                                       '使文本框获得焦点
    End Sub
End Class


Fadeca(外观层)

外观层的作用是为各层提供一个简明一致的界面,隐藏B层的代码结构。

Public Class FLogin         '检查用户是否存在
    Public Function CheckUser(ByVal UserInfo As Entity.ELogin) As Boolean '公共 函数 CheckUser(按值传递的Name为实体)为 布尔类型
        Dim IsUserExists As New BLL.BManager                              '定义 IsUserExists 为 新的 业务逻辑层中的BManager模块(实例化业务逻辑层)
        Dim flag As Boolean                                               '定义 flag 为 布尔类型
        flag = IsUserExists.ExistUser(UserInfo)                           'flag(默认为False) 接收 IsUserExists中ExistUser(为实体的Name)
        Return flag
    End Function
End Class


BLL(业务逻辑层)

Business Logic Layer的简称,强大的业务逻辑层能给外观层提供完善的服务。

Public Class BManager       '检查用户是否存在
    Public Function ExistUser(ByRef UserInfo As Entity.ELogin) As Boolean
        Dim Factory As New Factory.FTLogin                   '实例化工厂
        Dim IUser As IDAL.ILogin                             'B层引用了IDAL层的参数
        IUser = Factory.CreateIUser()                        '调用创建用户的工厂方法,调用工厂的CreatIUser方法创建IUser接口实例
        Dim table As New DataTable                           '中间变量,用于存储D层查询到的数据
        table = IUser.selectUser(UserInfo)                   '实现接口的方法
        If Not table Is Nothing Then
            If table.Rows.Count = 0 Then
                Return False
            Else
                Return True
            End If
        Else
            Return False
        End If
    End Function
End Class


Factory(工厂层)

工厂层的主要任务就是为创建对象提供过度接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。
'添加对配置文件的引用
Imports System.Configuration
'添加对反射的引用
Imports System.Reflection
Imports IDAL

Public Class FTLogin

    '数据程序集名称&命名空间DAL
    Private Shared ReadOnly AssemblyName As String = "DAL"

    '与配置文件相连
    Dim strDB As String = System.Configuration.ConfigurationManager.AppSettings("DB")

    Public Function CreateIUser() As ILogin
        Dim IUser As IDAL.ILogin
        Dim className As String = AssemblyName + "." + "DLogin"
        'CType函数将返回表达式显示的转换为指定的数据类型、对象、结构、接口或类后的结果

        IUser = CType(Assembly.Load(AssemblyName).CreateInstance(className),IDAL.ILogin) '反射 给定程序集AssemblyName 或 className.加载程序集
        '类似于 : IUser=(IDAL.ILogin)Assembly.Load(AssemblyName).CrateInstance(className)
        '在className这个类下面
        'Assembly.Load("程序集名") 
        'CreateIntance():一定是命名空间.类名;否则创建的实例为空
        Return IUser
    End Function
End Class


InterFace(接口层)

接口层是面向对象编程语言中接口操作的关键层,它的作用就是吧所需成员组合起来,用来封装一定功能的集合。
Public Interface ILogin
    '判断用户名是否存在
    Function selectUser(ByVal UserInfo As Entity.ELogin) As DataTable

End Interface



DAL(数据访问层)

Data Access Layer的简称,其功能主要是负责数据库的访问,即实现对数据库的增(Add)删(Delete)改(Update)查(Select)插入(Insert)。
Imports System.Data.SqlClient

Public Class DLogin : Implements IDAL.ILogin                                        '实现接口中的方法

    Public Function selectUser(UserInfo As Entity.ELogin) As DataTable Implements IDAL.ILogin.selectUser    '在数据库表Users中查找登陆界面的用户
        '中间变量,用于存储从数据库中查找到的信息
        Dim sqlHelper As New SQLHelper.SqlHelper                    '实例化SQLHelper对象
        Dim sql As String
        Dim table As DataTable                                      '声明一个DataTable
        Dim sqlParams As SqlParameter() = {New SqlParameter("@Name",UserInfo.Name),New SqlParameter("@PWD",UserInfo.PWD)} '声明并实例化参数数组
        sql = "select * from Users where UserName=@Name and Password=@PWD"  '调用SqlHelper类中的GetDataTable()方法来执行查询,并获取返回值
        table = SQLHelper.ExecSelect(sql,CommandType.Text,sqlParams)
        Return table
    End Function
End Class


SQLHelper(数据库连接层)

简化重复的去写那些数据库连接,被SQLHelper封装过后通常是只需要给方法传入一些参数如:连接字符串、SQL参数等,就可以访问数据库。
Imports System.Reflection
'添加对配置文件的引用
Imports System.Data.SqlClient
'添加对反射的引用
Imports System.Configuration

Public Class SqlHelper


    Public  strConnection As String = ConfigurationManager.AppSettings("strConnection") '定义公共 strConnection 为字符串 接收 配置管理下的应用程序设置(strConnection)
    Public conn As New SqlConnection(strConnection)                                    '定义公共 conn 作为 新的 数据库 连接(接收strConnection信息)
    Public cmd As New SqlCommand

    '定义公共 cmd 作为 新的 数据库按钮 实例化SqlCommand

    Public Function ExecSelect(ByVal cmdText As String,ByVal cmdType As CommandType,ByVal paras As SqlParameter()) As DataTable
        Dim adp As New SqlDataAdapter()
        Dim dt As New DataTable
        Dim ds As New DataSet
        cmd.CommandText = cmdText
        cmd.CommandType = cmdType
        cmd.Connection = conn
        cmd.Parameters.AddRange(paras)                                                 'cmd中的Parameters的添加范围为paras

        adp = New SqlDataAdapter(cmd)
        Try     '总是在这个地方抛出错误,无法进行adp.Fill(ds)这步
            conn.Open()
            adp.Fill(ds)                                                              '用数据集(ds)填充数据适配器(adp) 
            dt = ds.Tables(0)                                                         'DataTable是DataSet的第一个表
            cmd.Parameters.Clear()
            Return dt
        Catch ex As Exception
            MsgBox("数据库操作")
        Finally
            Call CloseCmd(cmd)
        End Try
        Return dt
    End Function
    Public Sub CloseCmd(ByVal cmd As SqlCommand)
        If Not IsNothing(cmd) Then
            cmd.Dispose()
            cmd = Nothing
        End If
    End Sub
End Class


Entity(实体层)

在这个层中定义数据类型。
Public Class ELogin
    '用于记录登陆时间的用户名
    Private Shared UserName As String
    Public Property Name() As String
        Get
            Return UserName
        End Get
        Set(value As String)
            UserName = value
        End Set
    End Property

    Private Password As String
    Public Property PWD() As String
        Get
            Return Password
        End Get
        Set(value As String)
            Password = value
        End Set
    End Property
End Class

App.config(映射方法)

以上就是七层登录窗体的代码,当然要实现窗体和数据库的连接还需要配置映射,代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
 
  <appSettings>
    <add key ="strConnection" value="Server=.;Database=Login;User ID=sa;Password=123"/>
    <add key ="DB" value="DAL"/>
  </appSettings>
</configuration>

七层登录窗体的时序图:

下面是七层登录窗体实现时候的时序图:


总结:

在这次敲击七层登录窗体的过程中,犯了很多小错误,有些地方根本就不应该出先的错误也出现了。同时还出现了代码之间的重复,比如说可以一行能解决的事情使用了两到三行,从而使得代码冗余,看起来乱糟糟的。

在身边大神的帮助下,终于将七层的登录敲击完成,接下来就开始第二次机房重构,第一次是在去年的暑假,但是由于自己自律层度还是不够,所以导致在第一遍用VB敲完机房收费系统之后,直到一年多之后的现在才开始进行VB.NET环境下用七层进行第二次机房收费系统。时间是有限的,自律是必须要有的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读