机房收费系统第三遍做的时候加入了工厂和接口,下面以登录为例记录了从UI层到Bll层(期间有工厂和接口)的代码。
UI层:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' frmLogin.vb
'' Implementation of the Class frmLogin
'' Generated by Enterprise Architect
'' Created on: 07-五月-2011 15:40:01
'' Original author: 黄爱岗
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Modification history:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Option Explicit On
Option Strict On''' <summary>
''' 登陆窗体
''' </summary>
Public Class frmLoginDim BllFactory As New BllFactory.BllFactory '定义Bll工厂产生Bll
Dim WorkUser As New Entity.EntityOnWorkInfo '定义正在工作用户实体类
Dim UserInfo As New Entity.EntityUserInfo '定义用户信息实体类''' <summary>
''' 登陆
''' </summary>
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click'判断文本框是否为空
If Trim(TxtUserID.Text) = Trim("") Then
MsgBox("用户名不能为空!", vbOKOnly, "警告")
Exit Sub
ElseIf Trim(TxtPwd.Text) = Trim("") Then
MsgBox("密码不能为空!", vbOKOnly, "警告")
Exit Sub
End If'给用户信息实体类赋值
UserInfo.UserID = Trim(TxtUserID.Text)
UserInfo.UserPwd = Trim(TxtPwd.Text)'调用用户登陆方法,显示主窗体
Try
BllFactory.CreateILogin.LoginSystem(UserInfo)
Me.Hide() '隐藏登陆窗体
Select Case UserInfo.UserLevel '根据不同用户的不同级别显示不同的主窗体
Case "一般用户"
frmMainStuOnOffLine.Show() '显示一般用户的主窗体
Case "操作员"
frmMainRegisterCard.Show() '显示操作员的主窗体
Case "管理员"
frmMainReckon.Show() '显示管理员的主窗体
End Select
Catch ex As Exception
MsgBox(ex.Message) '显示抓住的消息
Me.TxtPwd.Text = "" '清空用户密码文本框
Me.TxtUserID.Text = "" '清空用户名文本框
End TryEnd Sub
End Class
工厂BllFactory层:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' BllFactory.vb
'' Implementation of the Class BllFactory
'' Generated by Enterprise Architect
'' Created on: 04-五月-2011 15:39:48
'' Original author: asus
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Modification history:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Option Explicit On
Option Strict OnImports BllInterface
Imports System.Reflection''' <summary>
''' 根据需要通过反射的方式建造不同的BLL类
''' </summary>
Public Class BllFactoryPrivate Bll As String = System.Configuration.ConfigurationSettings.AppSettings("Bll1")
''' <summary>
''' 创建登陆接口
''' </summary>
Public Function CreateILogin() As ILogin
Return CType(Assembly.Load(Bll).CreateInstance(Bll & ".BllLogin"), ILogin)
End Function
End Class
接口ILogin层:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' ILogin.vb
'' Implementation of the Interface ILogin
'' Generated by Enterprise Architect
'' Created on: 04-五月-2011 15:40:05
'' Original author: asus
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Modification history:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Option Explicit On
Option Strict OnImports Entity
''' <summary>
''' 登陆的接口
''' </summary>
Public Interface ILogin
''' <summary>
''' 登陆系统,查询用户信息,判断密码、级别和用户使用状态,同时将登陆信息写入用户登陆临时表中
''' </summary>
''' <param name="entityUserInfo">用户信息</param>
Sub LoginSystem(ByVal entityUserInfo As EntityUserInfo)End Interface
BLL层:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' BllLogin.vb
'' Implementation of the Class BllLogin
'' Generated by Enterprise Architect
'' Created on: 07-五月-2011 15:39:49
'' Original author: 黄爱岗
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Modification history:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Option Explicit On
Option Strict OnImports Entity
Imports BllInterface''' <summary>
''' 登陆
''' </summary>
Public Class BllLogin
Implements BllInterface.ILogin
Dim DalFactory As New DalFactory.DalFactory '定义Dal工厂产生Dal
Public strComputerNum As String = System.Configuration.ConfigurationSettings.AppSettings("ComputerNum") '读取配置文件中机房号字段''' <summary>
''' 登陆系统,查询用户信息,判断密码、级别和用户使用状态,同时将登陆信息写入用户登陆临时表中
''' </summary>
''' <param name="entityUserInfo">用户信息</param>
Public Sub LoginSystem(ByVal entityUserInfo As EntityUserInfo) Implements ILogin.LoginSystemDim UserInfo As New DataSet '定义一个用户实体类用来接收从用户信息表传来的用户信息
Dim OnWorkUser As New DataSet '定义一个用户实体类用来接收从正在工作用户表传来的用户信息
Dim entityWorkUser As New EntityOnWorkInfo '定义正在工作用户实体类'根据用户ID获取用户信息
UserInfo = DalFactory.CreateIUserInfo.QueryByID(entityUserInfo)
'判断UserInfo是否为空,空则抛出消息
If UserInfo Is Nothing Or UserInfo.Tables(0).Rows.Count = 0 Then
Throw New Exception("用户不存在!")
End If'给实体类的用户ID属性赋值
entityWorkUser.UserID = Trim(entityUserInfo.UserID)
'根据用户ID获取正在工作用户信息
OnWorkUser = DalFactory.CreateIOnWorkInfo.QueryByID(entityWorkUser)
'判断此用户是否已登录
If OnWorkUser.Tables(0).Rows.Count > 0 Then
Throw New Exception("此用户正在工作!")
End If'判断用户状态是否可用,密码是否正确
If UserInfo.Tables(0).Rows(0)("UseState").ToString() = Trim("False") Then
Throw New Exception("此用户状态不可用!")
ElseIf (Trim(UserInfo.Tables(0).Rows(0)("UserPwd").ToString()) = Trim(entityUserInfo.UserPwd)) = False Then
Throw New Exception("卡号和密码不一致!")
End If'给正在工作实体类的各字段赋值
entityWorkUser.UserID = Trim(entityUserInfo.UserID)
entityWorkUser.ComputerNo = Trim(strComputerNum)
entityWorkUser.EnterDate = Date.Today
entityWorkUser.EnterTime = TimeOfDay
'添加用户信息到用户工作临时表中
Try
DalFactory.CreateIOnWorkInfo.AddOnWorkInfo(entityWorkUser)
entityUserInfo.UserLevel = Trim(UserInfo.Tables(0).Rows(0)("UserLevel").ToString()) '给实体类赋值(用户级别)
Catch ex As Exception
Throw New Exception("登陆失败,请稍后重试!")
End TryEnd Sub
End Class