机房重构—七层登录

来源:互联网 发布:系统优化提速 编辑:程序博客网 时间:2024/05/17 02:49

        七层登录,是三层登录演化而来的,它的主要目的是为了解耦和,让程序耦合性底,不必依赖性太强。它比三层多了外观层(Facade),工厂层(Factory)和接口层(IDAL)。多了这几层的应用,让程序更加符合开放封闭原则,大大降低程序的耦合性。


下面就是七层登录的代码:

首先用来连接数据库的配置文件:

       通过配置文件可以很方便的更改数据库,如果需要更改数据库,我只需要把配置文件中的Sqlserver改成其他数据库名字就可以了(比如MySQL等)。

<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>  </configSections>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  </startup>  <appSettings>    <add key="DB"  value="DAL"/>    <add key="strConnection"  value="Server=.;Database=login;User ID=sa;Password=123456" />  </appSettings></configuration>


UI层:

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click        '输入不能为空        If txtUserName.Text = "" Then            MsgBox("请输入用户名", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtUserName.Focus()        End If        If IsNumeric(txtUserName.Text) = False Then            MsgBox("用户名请输入数字", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtUserName.Text = ""        End If        If txtPassword.Text = "" Then            MsgBox("请输入密码", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtPassword.Focus()        End If        Entity.LoginEntity.ID = txtUserName.Text.Trim        Try            Dim Facade As New Facade.LoginFacade            Dim UserInfo As New Entity.LoginEntity            UserInfo.UserID = txtUserName.Text.Trim            UserInfo.Password = txtPassword.Text            Dim strResult As Boolean            '将U层的用户信息传入外观层,然后通过外观层传入B层进行判断            strResult = Facade.CheckUser(UserInfo)            txtUserName.Text = ""            txtPassword.Text = ""            Dim table As DataTable            table = Facade.CheckPwd(UserInfo)            Me.Hide()            txtPassword.Text = ""            txtUserName.Text = ""            frmMain.Show()        Catch ex As Exception            MsgBox("用户不存在或密码不正确")            txtPassword.Text = ""            txtUserName.Text = ""        End Try    End Sub


D层:

Imports System.Data.SqlClient  'system.Data.SqlClient 命名空间是SQLSever的.NET Framework数据提供程序'SQL Sever 的.NET Framework数据提供程序描述了一个类集合,这个类集合用于访问托管空间中的SQL Sever数据库Imports EntityImports IDAL'Imports System.ConfigurationImports SqlHelperPublic Class LoginDAL : Implements IDAL.LoginIDAL    '实现接口中的方法    Private sqlHelper As New SqlHelper.SqlHelper    '判断用户名是否存在    Public Function selectUser(UserInfo As LoginEntity) As DataTable Implements LoginIDAL.selectUser        Dim sql As String  '中间变量,用于储存从数据库中查找到的信息        Dim table As DataTable  '声明一个DataTable        '声明并实例化参数数组        Dim sqlParams As SqlParameter() = {New SqlParameter("@UserID", UserInfo.UserID),                                           New SqlParameter("@Password", UserInfo.Password)}        sql = "select * from User_info where UserID=@UserID and Password=@Password"        '调用SqlHelper类中的GetDataTable()方法来执行查询,并获取返回值        table = sqlHelper.ExecSelect(sql, CommandType.Text, sqlParams)        Return table    End FunctionEnd Class


工厂层:

Imports System.Reflection '添加对反射的引用Imports System.Configuration '添加对配置文件的引用Imports System.DataImports 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 LoginIDAL        Dim classname As String = strDB + "." + "LoginDAL" '要实例化的D层的类的名称        Dim IUser As LoginIDAL        'CType函数将返回表达式显示地转换为指定的数据类型、对象、结构、类或接口后的结果        IUser = CType(Assembly.Load(AssemblyName).CreateInstance(classname), LoginIDAL) '返回LoginIDAL        Return IUser    End FunctionEnd Class

接口层:

Public Interface LoginIDAL    '判断用户名是否存在    Function selectUser(ByVal UserInfo As Entity.LoginEntity) As DataTableEnd Interface

B层:

Imports IDALPublic Class LoginBLL    '检查用户是否存在    Public Function ExistUser(ByVal UserInfo As Entity.LoginEntity) As Boolean        Dim Factory As New Factory.LoginFactory        Dim IUser As IDAL.LoginIDAL        '调用创建用户的工厂方法        IUser = Factory.CreateIUser() '调用工厂的CreatIUser方法创建IUser接口实例        Dim table As New DataTable '中间变量,用于存储D层查询到的数据        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 RightPWD(ByVal UserInfo As Entity.LoginEntity) As DataTable        Dim Factory As New Factory.LoginFactory        Dim IUser As IDAL.LoginIDAL        Dim table As DataTable '中间变量,用于存储D层查询到的数据        IUser = Factory.CreateIUser '调用工厂的方法创建Iuser        table = IUser.selectUser(UserInfo) '调用接口的方法selectUser        If UserInfo.Password = Trim(table.Rows(0).Item(1)) Then            MsgBox("登陆成功!")        End If        Return table    End FunctionEnd Class

外观层:

Public Class LoginFacade    '检查用户是否存在    Public Function CheckUser(ByVal UserInfo As Entity.LoginEntity) As Boolean        Dim IsUserExists As New BLL.LoginBLL        Dim flag As Boolean        flag = IsUserExists.ExistUser(UserInfo)        If flag = True Then            Return True        Else            Return False        End If    End Function    '检查密码是否正确    Public Function CheckPwd(ByVal UserInfo As Entity.LoginEntity) As DataTable        Dim IsPwdExists As New BLL.LoginBLL        Dim table As DataTable        table = IsPwdExists.RightPWD(UserInfo)        Return table    End FunctionEnd Class
实体层:

Public Class LoginEntity    Public Shared ID As String '用于记录登录时的用户名    Private _UserID As String    Public Property UserID As String        Get            Return _UserID        End Get        Set(value As String)            _UserID = 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 PropertyEnd Class

最后是七层登录的顺序图:




0 0