在上上篇,《机房收费系统重构(3)》,中主要是介绍了自己关于DAL层,工厂层,以及接口层,还有实体层的理解,但是好多读者再问我,你的代码呢,我在这解释一下,就是我只是写出关于那几部分的理解,并没有写贴出代码让大家研究的,但是不能顺应民心的文章不是好文章,所以我在这篇文章中,将机房收费登录中七层中所有的代码,贴出来供大家拍砖斧正,还有最后我在谈谈针对BLL层和外观层的理解。
首先我所说的七层是针对UI层,外观层,BLL层,DAL层,工厂层,实体层,接口层七层组成。
实现登录窗体,要明白,先要查询用户是否存在,然后将登录信息写入数据库,这两个主要步骤。
首先来看看我数据库中用到的两个表:
这样能方便读者更容易理解后面的代码:
一.首先看看实体层的代码吧
(1)用户表实体层:
Public Class MO_Login Private _userID As String Private _level As String Private _password As String Private _userName As String Private _computer As String Public Shared UserHead As String ' 设置全局变量 Public Shared UserLevel As String Public Property UserID() As String Get Return _userID End Get Set(value As String) _userID = value End Set End Property Public Property PassWord() As String Get Return _password End Get Set(value As String) _password = value End Set End Property Public Property level() As String Get Return _level End Get Set(value As String) _level = value End Set End Property Public Property UserName() As String Get Return _userName End Get Set(value As String) _userName = value End Set End Property Public Property Computer() As String Get Return _computer End Get Set(value As String) _computer = value End Set End Property End Class
(2)工作记录实体层:
Public Class MO_Worklog Private _userID As String Private _level As String Private _loginDataTime As String Private _computer As String Private _status As String Public Shared Property Login_DataTime As String Public Property UserID() As String Get Return _userID End Get Set(value As String) _userID = value End Set End Property Public Property level() As String Get Return _level End Get Set(value As String) _level = value End Set End Property Public Property LoginDataTime() As String Get Return _loginDataTime End Get Set(value As String) _loginDataTime = value End Set End Property Public Property Computer() As String Get Return _computer End Get Set(value As String) _computer = value End Set End Property Public Property Status() As String Get Return _status End Get Set(value As String) _status = value End Set End Property End Class
二.接着来看看UI层的吧:
Public Class frmLogin Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click Dim login As New Model.MO_Login Dim falogin As New Facade.FA_Login '实例化过程 Dim strResult As String login.UserID = txtUserName.Text '赋值过程 login.PassWord = txtPassWord.Text Model.MO_Login.UserHead = txtUserName.Text Dim worklog As New Model.MO_Worklog '实例化工作记录 worklog.LoginDataTime = Date.Now.ToString("yyyy-mm-dd hh:mm:ss") '上机时间 worklog.Status = "正在值班" '工作状态 worklog.UserID = Model.MO_Login.UserHead worklog.Computer = System.Net.Dns.GetHostName().ToString() '本地计算机 strResult = falogin.FLogin(login, worklog) '传参返回过程 Select Case strResult '判断过程 Case "输入有误" MsgBox("输入有误,请重新输入") Case "登录成功" MsgBox("登录成功") Model.MO_Worklog.Login_DataTime = worklog.LoginDataTime Me.Hide() 'frmMain.Show() End Select End Sub Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click End End Sub End Class
三.外观层代码:
Imports BLL Public Class FA_Login Public Function FLogin(ByVal user As Model.MO_Login, worklog As Model.MO_Worklog) As String Dim UserBLL As New BLL.BL_Login If UserBLL.IsnotNull(user, worklog) = False Then Return "输入有误" Else Return "登录成功" End If End Function End Class
四.BLL层代码:
<pre name="code" class="vb">Imports IDAL Public Class BL_Login Public Function IsnotNull(ByVal user As Model.MO_Login, ByVal worklog As Model.MO_Worklog) As Boolean Dim IUser As IDAL.ILogin '注意接口类型不能实例化。 Dim factory As New Factory.LoginFactory '实例化工厂 Dim IWorklog As IDAL.IWorklog IUser = factory.CreateUserInfo() '工厂创建接口,DAL实现接口,BLL调用工厂 IWorklog = factory.CreateWorklog() If IUser.User_Login(user).UserID = "" Then '如果为空 Return False 'Boolean值为false Else worklog.level = IUser.User_Login(user).level '传参数,将查询到的用户类型传给worklog实体层,在后面的增删改中应用。 If IWorklog.SaveWorkLog(worklog) Then '如果查询到用户存在,就开始将登录信息写入数据库中。 Return True End If Return True End If End Function End Class
五.工厂层代码:
Imports System.Reflection '添加反射的引用 Imports System.Configuration '添加配置文件的引用 Imports IDAL Public Class LoginFactory '利用反射+配置文件+抽象工厂 Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("strSqlConnection") '表示读取配置文件,如果配置文件中是SQLserver数据库就访问,如果是别的就放访问别的,不需要更改代码 'Dim strQueryWorkLog As String = System.Configuration.ConfigurationSettings.AppSettings("strSqlConnection") '创建用户表工厂 Public Function CreateUserInfo() As ILogin Return CType(Assembly.Load("DAL").CreateInstance("DAL" & "." & "DA_Login"), ILogin) End Function Public Function CreateWorklog() As IWorklog Return CType(Assembly.Load("DAL").CreateInstance("DAL" & "." & "DA_Worklog"), IWorklog) End Function
End Class
六.接口层代码:
(1)用户查询接口
Public Interface ILogin Function User_Login(ByVal user As Model.MO_Login) As Model.MO_Login End Interface
(2)将操作记录写入数据库代码:
Public Interface IWorklog Function SaveWorkLog(ByVal worklog As Model.MO_Worklog) As Boolean End Interface
七.最后是DAL层代码:
(1)用户查询DAL
Imports System.Data.SqlClient Imports System.Data Imports IDAL Public Class DA_Login : Implements IDAL.ILogin Public Function User_Login(user As Model.MO_Login) As Model.MO_Login Implements IDAL.ILogin.User_Login Dim sqlparams As SqlParameter() = {New SqlParameter("@UserID", user.UserID), New SqlParameter("@Password", user.PassWord)} '传递参数过程 Dim cmdText As String = "select * from T_UserInfo where UserName [email protected] and PassWord [email protected]" '查询数据库 Dim aUser As New Model.MO_Login '实例化过程 Dim helper As New SqlHelper Dim cmdType As CommandType = New CommandType() cmdType = CommandType.Text '定义命令类型为存储过程 Dim table As DataTable '实例化一个过程 '在这里想解释一个,由于函数返回参数是实体层,但是这里定义的table是一个Datable类型,所以系统无法进行转换。所以我定义了一个aUser进行中转。 table = helper.ExecuteQuery(cmdText, cmdType, sqlparams) '定义返回值 If table.Rows.Count <> 0 Then aUser.UserID = table.Rows(0).Item("UserID") aUser.PassWord = table.Rows(0).Item("PassWord") aUser.level = table.Rows(0).Item("level") aUser.UserName = table.Rows(0).Item("UserName") End If Return aUser End Function End Class
(2)操作记录写入代码DAL
Imports System.Data.SqlClient Imports System.Data Imports IDAL Public Class DA_Worklog : Implements IDAL.IWorklog Public Function SaveWorkLog(worklog As Model.MO_Worklog) As Boolean Implements IWorklog.SaveWorkLog Dim strText As String = "INSERT INTO T_WorkLog(UserID,level,LoginDateTime,Computer,Status)VALUES (@UserID,@level,@LoginDateTime,@Computer,@Status)" Dim sqlparams As SqlParameter() = {New SqlParameter("@UserID", worklog.UserID), New SqlParameter("@level", worklog.level), New SqlParameter("@LoginDateTime", worklog.LoginDataTime), New SqlParameter("@Computer", worklog.Computer), New SqlParameter("@Status", worklog.Status)} Dim cmdType As CommandType = CommandType.Text Dim helper As New SqlHelper Return helper.ExecuteNonQuery(strText, cmdType, sqlparams) End Function End Class
其实这些代码也不全,少了关于工厂反射的配置部分的内容,还有就是SqlHelper。SqlHelper,在我《机房收费重构(4)》中有很详细的介绍。在这就不写了。
最后说一下关于BLL和外观层的理解吧。从代码看,仿佛感觉外观层没有什么作用,是的,关于登录窗体小功能的实现,没有太大的必要用外观层,但是以后要敲一个庞大的窗体时,我们需要将U层和B层解耦,那么外观层的作用就彰显出来了。关于BLL和外观其实没有什么想说的,最想说的是数据类型的转换,因为层与层之间传递参数和返回参数的过程需要用到同样类型的数据,但是将数据类型都设置为Datatable和string或者实体类型是不可能,希望大家在今后敲代码过程中注意就可以了。
本片文章代码较多,希望大家拍砖斧正。。
机房收费系统重构(五)