这几天一直在做VB.NET版的机房收费系统,初次使用了三层架构,“万事开头难”,自己迷迷糊糊的纠结了一天,终于将登录实现了(因为之前看了一个三层架构的讲解视频:小燕雀。大家可以直接去百度视频上观看)。
下面我来为那些初步接触三层架构的人们展示一下我的登录代码,希望对大家有所帮助:
下面,大家将会看到的是1、2、3、4的实现及调用过程:
准备工作:
建立数据表(可参见后面的实体层中的代码),
创建相应的类库及窗体:
我用的是VB.NET的WindowsApplication项目来添加界面;使用类库来作为其他几个层的包
UI层(界面层):前提在UI层中添加Entity与BLL的引用
Imports BLL
Imports EntityPublic Class frmLogin
Private Sub Ok_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Ok.Click
' 判断文本框是否为空
……
'填充参数记录(此处调用的是实体层中实体类userInfo中的填充记录Fill方法,后面会有介绍)
Dim entityUserInfo As New userInfoentityUserInfo.Fill(Trim(txtUserName.Text),Trim(txtPwd.Text),Trim(cboUserLevel.Text))
'验证用户记录(调用的BLL层中的验证用户记录CheckRecord方法)
Dim bllUserInfo As New BllLoginIf bllUserInfo.CheckRecord(entityUserInfo) Then
Me.Hide()
frmMain.Show()'判断用户级别
……
'添加用户记录
……
Else
MsgBox("登录失败",vbOKOnly + vbInformation,"登录失败")
txtUserName.Focus()
Exit Sub
End If
End SubPrivate Sub Cancel_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class
BLL层(业务逻辑层):前提添加DAL,IDAL和Entity的引用
Imports DAL
Imports IDAL
Imports EntityPublic Class BllLogin
''' <summary>
''' 查看登录信息是否存在
''' </summary>
''' <param name="entityUserInfo">窗体输入的用户信息</param>
''' <returns>登录是否成功</returns>
''' <remarks>2011-3-20 15:22 by dan</remarks>
Public Function CheckRecord(ByVal entityUserInfo As userInfo) As Boolean
Dim checkResult As Boolean'下面声明的iuser是实例化的Iuser类型的DBuser,实现了接口的调用(接口虽然不能被实例化,但是继承它的类可实例化成接口类型
Dim iuser As Iuser
'此处运用的是抽象工厂设计模式中的发射加配置,后面博客中会更新相关介绍,如果没有听过设计模式的人可以将下面这句话改为:iuser=New DBuser()
iuser = DataAccess.CreateUser()If Not IsNothing(iuser.Check(entityUserInfo)) Then
checkResult = True
Else
checkResult = False
End If
Return checkResult
End FunctionEnd Class
IDAL层(接口层):添加Entity的引用
Imports Entity
Public Interface Iuser
''' <summary>
''' 查看一条记录是否存在
''' </summary>
''' <param name="entityUser">用户信息</param>
''' <returns>一条用户记录</returns>
''' <remarks>2011-3-21 21:24 by dan</remarks>
Function Check(ByVal entityUser As userInfo) As userInfoEnd Interface
DAL层(数据访问层):添加Entity,IDAL和sqlClient的引用
Imports System.Data.sqlClient
Imports Entity
Imports IDALPublic Class DBuser
‘前面BLL没有用抽象工厂的这里不用继承DataAccess
Inherits DataAccess
Implements Iuser''' <summary>
''' 查询一条用户记录是否存在
''' </summary>
''' <param name="entityUser">待查询的记录</param>
''' <returns>查询到的记录</returns>
''' <remarks>2011-3-20 10:37 by dan</remarks>
Public Function Check(ByVal entityUser As Entity.userInfo) As userInfo Implements IDAL.Iuser.Check’这里的sql语句是为了以后的的灵活性,将各个字段使用了常量来进行表示,具体可参见实体层)
Dim sql As String = String.Format("select * from userInfo where [userName]={0} and [passWord]={1} and [userLevel]={2}",userInfo.DBPARAM_USERNAME,userInfo.DBPARAM_PASSWORD,userInfo.DBPARAM_USERLEVEL)Dim conn As sqlConnection = CreateConn()
Dim cmd As sqlCommand = New sqlCommand(sql,conn)
Dim sda As sqlDataAdapter = New sqlDataAdapter(cmd)
Dim ds As New DataSet'向cmd中添加参数
AddsqlParameter(cmd,sqlDbType.VarChar,entityUser.userName)
AddsqlParameter(cmd,entityUser.passWord)
AddsqlParameter(cmd,userInfo.DBPARAM_USERLEVEL,entityUser.userLevel)Try
conn.Open()
sda.Fill(ds,"userInfo1")
Dim dr As DataRow = ds.Tables("userInfo1").Rows(0)
entityUser.LoadFromDataRow(dr)
Return entityUser
Catch ex As Exception
Return Nothing
Finally
Close(cmd)
Close(conn)
End Try
End FunctionEnd Class
Entity层(实体层):大家可参见该代码来设计数据表
Public Class userInfo
#Region "定义userInfo表中各个属性变量"
Private _userName As String
Private _Name As String
Private _passWord As String
Private _userLevel As String
Private _accountHolder As String
Private _regDate As String
Private _regTime As String
#End Region#Region "定义数据表中各个字段名常量"
Private Const DBFIELD_USERNAME = "userName"
Private Const DBFIELD_NAME = "Name"
Private Const DBFIELD_PASSWORD = "passWord"
Private Const DBFIELD_USERLEVEL = "userLevel"
Private Const DBFIELD_ACCOUNTHOLDER = "accountHolder"
Private Const DBFIELD_REGDATE = "regDate"
Private Const DBFIELD_REGTIME = "regTime"
#End Region#Region "定义数据表中各个字段参数变量"
Public Const DBPARAM_USERNAME = "@userName"
Public Const DBPARAM_NAME = "@Name"
Public Const DBPARAM_PASSWORD = "@passWord"
Public Const DBPARAM_USERLEVEL = "@userLevel"
Public Const DBPARAM_ACCOUNTHOLDER = "@accountHolder"
Public Const DBPARAM_REGDATE = "@regDate"
Public Const DBPARAM_REGTIME = "@regTime"
#End Region''' <summary>
''' 获取数据表中的一条记录
''' </summary>
''' <param name="dr">字段名</param>
''' <remarks>2011-3-20 15:59 by dan</remarks>
Public Sub LoadFromDataRow(ByVal dr As DataRow)
userName = dr(DBFIELD_USERNAME)
Name = dr(DBFIELD_NAME)
passWord = dr(DBFIELD_PASSWORD)
userLevel = dr(DBFIELD_USERLEVEL)
accountHolder = dr(DBFIELD_ACCOUNTHOLDER)
regDate = dr(DBFIELD_REGDATE)
regTime = dr(DBFIELD_REGTIME)
End Sub#Region "填充一条记录"
''' <summary>
''' 填充一条记录(登录)到实体层
''' </summary>
''' <param name="AuserName">用户名</param>
''' <param name="ApassWord">密码</param>
''' <param name="AuserLevel">用户级别</param>
''' <remarks>2011-3-20 16:36 by dan</remarks>
Public Overloads Sub Fill(ByVal AuserName As String,ByVal ApassWord As String,ByVal AuserLevel As String)
userName = AuserName
passWord = ApassWord
userLevel = AuserLevel
End Sub#Region "定义数据表中各个属性"
''' <summary>
''' 用户名
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>2011-3-19 20:34 by dan</remarks>
Public Property userName() As String
Get
Return _userName
End Get
Set(ByVal value As String)
_userName = value
End Set
End Property
……
……
#End RegionEnd Class
下面的是DAL层下的DataAccess类:
添加Configuration引用时须从.NET子项目中查找,还要添加项目引用Entity和IDAL
'如果没有使用配置加反射的不用引用Configuration和Reflection
Imports System.Configuration
Imports System.ReflectionImports System.Data.sqlClient
Imports Entity
Imports IDALPublic Class DataAccess
#Region "数据库连接"
Private ReadOnly connStr As String = ConfigurationManager.AppSettings("connStr")''' <summary>
''' 创建一个数据库连接
''' </summary>
''' <returns></returns>
''' <remarks>2011-3-19 20:18 by dan</remarks>
Public Overloads Function CreateConn() As sqlConnection
Return New sqlConnection(connStr)
End Function
#End Region#Region "关闭相关对象"
''' <summary>
''' 关闭sqlConnection对象
''' </summary>
''' <param name="conn">连接数据库</param>
''' <remarks>2011-3-19 20:20 by dan</remarks>
Public Sub Close(ByVal conn As sqlConnection)
conn.Close()
conn = Nothing
End Sub''' <summary>
''' 关闭sqlCommand对象
''' </summary>
''' <param name="cmd ">sql命令</param>
''' <remarks>2011-3-19 20:20 by dan</remarks>
Public Sub Close(ByVal cmd As sqlCommand)
cmd.Dispose()
cmd = Nothing
End Sub
#End Region''' <summary>
''' 为sql变量赋值并添加到sqlCommand中
''' </summary>
''' <param name="cmd">sqlCommand命令</param>
''' <param name="dbParam">参数名</param>
''' <param name="dbType">参数类型</param>
''' <param name="value">参数值</param>
''' <remarks>2011-3-19 20:27 by dan</remarks>
Public Sub AddsqlParameter(ByRef cmd As sqlCommand,ByVal dbParam As String,ByVal dbType As sqlDbType,ByVal value As Object)
Dim sqlParam As sqlParameter = New sqlParameter(dbParam,dbType)
sqlParam.Value = value
cmd.Parameters.Add(sqlParam)
End Sub#Region "配置加反射"
Private Shared ReadOnly AssemblyName As String = ConfigurationManager.AppSettings("AssemblyName")
Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("db")''' <summary>
''' 实例化Iuser类型的DBuser类对象
''' </summary>
''' <returns>DBuser类对象</returns>
''' <remarks>2011-3-21 15:05 by dan</remarks>
Public Shared Function CreateUser() As Iuser
Dim ClassName As String
ClassName = AssemblyName + "." + db + "user"
Return CType(Assembly.Load(AssemblyName).CreateInstance(ClassName),Iuser)
End Function#End Region
End Class
下面是配置文件的使用:在窗体层添加新项目——>选中打开窗口的左边的General选项——>添加配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>’中间这部分是需要手动添加的
<appSettings >
<add key="connStr" value="server=.;database=zdd;user id=sa;password=dandan;"/>
<add key="AssemblyName" value ="DAL"/>
<add key="db" value ="DB"/>
</appSettings>
<connectionStrings/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
</configuration>
因为本文中使用了抽象工厂模式及配置加反射,如果大家有什么问题,请留言……
原文链接:https://www.f2er.com/vb/261343.html