七层登录

来源:互联网 发布:js embed 属性 编辑:程序博客网 时间:2024/05/01 10:42

    这篇文章好长时间都没有更新了,欠下的总是要还的。

    无一例外,先放一张七层登录的包图


    他们之间的关系,通关包图就能一目了然。

    一开始的时候也很奇怪为什么要加上外观层和抽象工厂,于是又翻阅了设计模式这本书,重新又体会了一把抽象工厂模式的定义:提供创建一系列相关或相互依赖对象的接口,而无需制定它们具体的类。七层登录的难点就是反射和配置文件,现在的理解的也比较浅。记得机房合作验收的时候,听师哥师姐说过反射,把数据库比作一个柜子,柜子上有个小孔,就是咱们这时候的接口,柜子前面放一个大镜子,大镜子通过反射通过小孔就能知道柜子里有什么,反射的原理知道了,对应到代码上就好理解多了。

    同样把代码贴出来,具体的下篇博客见啦!

    实体层

'****************************'项目名称:LoginEntity'命名空间:Entity'文件名:  LoginEntity'作者:    郝雨烁'创建日期:2017/3/2'版本号:  v1.0.0.0'修改日志:'版权说明:'*********************************Public Class UserEntity    Private _userName As String    Private _PWD As String    Private _userLevel As String        Public Property userName() As String        Get            Return _userName        End Get        Set(value As String)            _userName = value        End Set
    End Property    Public Property PWD() As String        Get            Return _PWD        End Get        Set(value As String)            _PWD = value        End Set    End Property    Public Property userLevel() As String        Get            Return _userLevel        End Get        Set(value As String)            _userLevel = value        End Set    End PropertyEnd Class
UI层'注入命名空间Imports EntityImports FacadePublic Class frmLoginUI    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click        Dim enUser As New Entity.UserEntity        '实例化过程        enUser.userName = txtuserName.Text.Trim         '赋值过程        enUser.PWD = txtpassword.Text.Trim        'Dim strResult As String        '判断是否输入了用户名及密码        If txtuserName.Text.Trim() = "" Then            MessageBox.Show("用户名不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtuserName.Select()            txtuserName.Focus()            Exit Sub        ElseIf IsNumeric(txtuserName.Text) = False Then            MessageBox.Show("用户名请输入数字", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtuserName.Text = ""            txtuserName.Select()            txtuserName.Focus()            Exit Sub        ElseIf txtpassword.Text.Trim() = "" Then            MessageBox.Show("请输入密码", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)            txtpassword.Select()            txtpassword.Focus()            Exit Sub        End If        'Try        '定义一个外观层的对象        Dim FacadeLogin As New Facade.LoginFacade        Dim strResult1 As Boolean        strResult1 = FacadeLogin.CheckUser(enUser)        If strResult1 = False Then            MsgBox("用户不存在")            txtuserName.Text = ""            txtuserName.Select()            txtuserName.Focus()        End If        Dim table As DataTable        table = FacadeLogin.CheckPwd(enUser)        If Trim(txtpassword.Text) = Trim(table.Rows(0).Item(1)) Then            MessageBox.Show("登陆成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information)            Me.Show()        Else            MsgBox("密码不正确")            txtpassword.Text = ""            txtpassword.Select()            txtpassword.Focus()        End If    End Sub    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click        End    End SubEnd Class

       get一个小技能哟,分享给大家。
咱们之前用过VB,在一个窗体上把一个按钮的default属性选择后就可以让他变成默认的按钮(默认响应回车键), 还有一个Cancle 属性默认响应 ESC 键.但是在.net环境下中如何来设置呢?有的朋友会说button对象的属性中根本就没有default这个属性呀?原来这两个属性都放在了form对象的属性中, 其中acceptbutton属性对应原来按钮的 Default 属性,把这个属性设置为你想设置的按钮的名称就可以了;同样,你会发现还有一个cancelbutton的属性,这个属性就是设置“取消”按钮的属性了。
Facade层'****************************'项目名称:LoginFacade'命名空间:Facade'文件名:  LoginFacade'作者:    郝雨烁'创建日期:2017/3/2'版本号:  v1.0.0.0'修改日志:'版权说明:'*********************************Imports BLLImports EntityPublic Class LoginFacade    Public Function CheckUser(ByVal enUser As Entity.UserEntity) As Boolean        '用于检查用户是否存在        Dim IsUserExistsBLL As New BLL.IsExists        Dim flag As Boolean        flag = IsUserExistsBLL.IsUserExists(enUser)        If flag = True Then            Return True        Else            Return False        End If    End Function    '检查用户是否正确    Public Function CheckPwd(ByVal enUser As Entity.UserEntity) As DataTable        Dim IsPwdBLL As New BLL.IsExists        Dim table As DataTable        table = IsPwdBLL.CheckUserPWD(enUser)        Return table    End FunctionEnd Class

B层'****************************'项目名称:LoginBLL'命名空间:BLL'文件名:  LoginBLL'作者:    郝雨烁'创建日期:2017/3/2'版本号:  v1.0.0.0'修改日志:'版权说明:'*********************************Imports FactoryImports EntityImports IDALPublic Class IsExists    '检查用户是否存在    Public Function IsUserExists(ByVal enUser As Entity.UserEntity) As Boolean        '定义并实例化一个工厂        Dim factory As New Factory.DataAccess        '定义一个接口变量        Dim IUser As IUser        '通过调用创建用户的工厂方法        IUser = factory.CreateUser        Dim table As DataTable        Dim flag As Boolean        table = IUser.CheckExistsUser(enUser)        If table.Rows.Count = 0 Then            flag = False        Else            flag = True        End If        Return flag    End Function    '查密码是否正确    Public Function CheckUserPWD(ByVal enUser As UserEntity) As DataTable        Dim factory As New Factory.DataAccess  '定义工厂并实例化工厂变量factory        Dim IUser As IUser  '定义接口变量IUser        Dim table As DataTable   '中间变量,用于存储D层查询到的数据        IUser = factory.CreateUser   '调用工厂的CreateUser方法创建iuser接口实例        table = IUser.CheckExistsUser(enUser)  '调用接口的方法UserIsExist,并将返回值返回给flag        Return table    End FunctionEnd Class

Factory层'****************************'项目名称:LoginFactory'命名空间:Factory'文件名:  LoginFactory'作者:    郝雨烁'创建日期:2017/3/2'版本号:  v1.0.0.0'修改日志:'版权说明:'*********************************Imports System.Configuration    '添加对配置文件的引用Imports System.Reflection      '添加对反射的应用Imports IDALPublic Class DataAccess    Dim yu As String = System.Configuration.ConfigurationManager.AppSettings("yu")    '读取配置文件的程序集名称    '创建用户表的工厂    Public Function CreateUser() As IUser        Dim className As String = yu + "UserDAL"        
     '反射的关键代码(返回接口)        Dim iuser As IUser        iuser = CType(Assembly.Load("DAL").CreateInstance(className), IUser)        '将实例化的D层通过向上转型转换成接口类,然后通过调用接口类中的函数来调用D层中实现该接口的函数        Return iuser    End FunctionEnd Class

接口层'****************************'项目名称:LoginIDAL'命名空间:IDAL'文件名:  LoginIDAL'作者:    郝雨烁'创建日期:2017/3/2'版本号:  v1.0.0.0'修改日志:'版权说明:'*********************************Imports EntityPublic Interface IUser    '检查用户是否存在和密码是否正确    Function CheckExistsUser(ByVal enUser As Entity.UserEntity) As DataTableEnd Interface

D层'****************************'项目名称:LoginDAL'命名空间:DAL'文件名:  LoginDAL'作者:    郝雨烁'创建日期:2017/3/2'版本号:  v1.0.0.0'修改日志:'版权说明:'*********************************Imports System.Data.SqlClient  'system.Data.SqlClient 命名空间是SQLSever的.NET Framework数据提供程序  'SQL Sever 的.NET Framework数据提供程序描述了一个类集合,这个类集合用于访问托管空间中的SQL Sever数据库  Imports EntityImports IDALImports SqlHelperPublic Class SqlserverUserDAL : Implements IUser    Public Function SelectExistsUser(enUser As UserEntity) As DataTable Implements IUser.CheckExistsUser        Dim sql As String  '定义字符串变量sql,用于存放要执行的sql语句        Dim table As DataTable '定义表变量table,用于存储执行的结果并返回        Dim paras As SqlParameter() = {New SqlParameter("@userName", enUser.userName), New SqlParameter("@PWD", enUser.PWD)}         '参数化查询,防止sql注入            sql = "select * from User_Info where UserID=@userName and password=@PWD"  '存储sql语句        table = sqlHelper.GetDataTable(sql, CommandType.Text, paras) '执行sql语句,将结果赋给table        Return table    End FunctionEnd Class

我把sqlHelper层放在了D层,为了熟练使用数据库操作Imports System.Data.SqlClientPublic Class sqlHelper    Dim str As String = Configuration.ConfigurationManager.AppSettings("db")    Dim conn As New SqlConnection(str)    Dim cmd As New SqlCommand    Public Function GetDataTable(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable        Dim adp As SqlDataAdapter '定义数据适配器        Dim table As New DataTable        Dim ds As New DataSet        '给cmd赋值        cmd.CommandText = cmdText 'cmdText为所要执行的sql语句        cmd.CommandType = cmdType '命令执行的类型        cmd.Parameters.AddRange(paras) '参数添加        sqlAdapter = New SqlDataAdapter(cmd) '实例化Adapter        conn.Open()        adp.Fill(ds) '用adapter将dataset填充        Return ds.Tables(0) '        cmd.Parameters.Clear() '清除参数       
         conn.Close()           End FunctionEnd Class

多总结,多读书。



1 0
原创粉丝点击