《客房收费系统我的版》基本完成,矿U层的代码是很是很是混乱。基本上D层有几个函数,B层就相应有几个函数,U层使用相应B层中的每一个函数。比方说在登陆中,U层首次要使用一个函数检查username和用户password是否正确,而后再使用“加入用户上机记录”的函数。如下是登陆的时序图:sql
登陆业务比較简单,但是对于复杂的上机过程呢?U层要检查卡是否注冊。剩余金额是否充足,卡的状态是否在使用中。该卡是否现在不在线,经过这一系列检验后,还要查询学生表显示学生信息等等。数据库
这样就形成U层有好多函数,和B层的耦合度太大。现在咱们来回想一下三层中各层的功能:设计模式
表现层(UI):採集用户的输入信息和操做,向用户展示特定业务数据。通俗讲就是用户界面,即用户在使用一个系统的时候他的所见所得。架构
业务逻辑层(BLL):针对详细问题的操做。也可以说是对数据层的操做。对数据业务逻辑处理。主要有三种方式:从UI中获取用户指令和数据,运行业务逻辑;从DAL中获取数据,以供UI显示;从UI中获取用户指令和数据,经过DAL写入数据源。ide
数据訪问层(DAL):该层所作事务直接操做数据库,针对数据的增添、删除、改动、查找等。函数
在师傅的指点下,进行了改动:学习
U层:spa
'登陆 Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click '查空 If PublicFunction.IsEmptyText(Me) = True Then Exit Sub End If '实例化实体User,引用B层 Dim euser As New Entity.User Dim euserRecord As New Entity.UserRecord Dim blogin As New BLL.LoginBLL Try '将用户输入的信息传给实体 euser.ProuserID = txtUserID.Text.Trim euser.ProuserPwd = txtUserPwd.Text '用户验证后反馈信息 If blogin.Check(euser, euserRecord) Then '登陆成功 ''UserID和UserLevel为全局变量,其它功能要用到 UserID = euser.ProuserID.Trim '去空格 UserLevel = euser.ProuserLevel.Trim '去空格 '主窗口显示 Me.Hide() frmMain.Show() Else MsgBox("登陆失败!username或password有误。B层:", vbExclamation, "系统提示") txtUserID.Focus() Exit Sub End If Catch ex As Exception MsgBox("错误!", vbExclamation, "系统提示") End Try End Sub设计
Public Function Check(ByVal euser As Entity.User, ByVal euserRecord As Entity.UserRecord) As Boolean Dim dt As DataTable dt = iuser.QueryUser(euser) Try If dt.Rows.Count = 0 Then Return False Else 'username和password输入正确 euser.ProuserID = dt.Rows(0).Item(0) '用户ID euser.ProuserLevel = dt.Rows(0).Item(2) '用户级别 '输入用户上机记录信息 euserRecord.ProuserID = euser.ProuserID euserRecord.ProuserLevel = euser.ProuserLevel euserRecord.ProloginTime = Now euserRecord.PrologoutTime = Now euserRecord.ProisOnline = 1 '1表示在线。0表示不在线 euserRecord.Procomputer = My.Computer.Name '得到当前电脑的username '加入用户上机记录 Dim result As Integer result = iuser.AddUserRecord(euserRecord) If result <> 0 Then '加入用户记录成功 Return True End If End If Catch ex As Exception Throw New Exception End Try End Function
'查找用户的方法 Public Function QueryUser(euser As Entity.User) As DataTable Implements IUser.QueryUser Try Dim strSQL As String = "select * from T_User where userID=@userID and userPwd=@userPwd " Dim params() As SqlParameter = {New SqlParameter("@userID", euser.ProuserID), New SqlParameter("@userPwd", euser.ProuserPwd)} Dim helper As New SqlHelper.sqlHelper Dim table = helper.GetDataTable(strSQL, CommandType.Text, params) Return table Catch ex As Exception Throw New Exception End Try End Function '用户登陆成功加入记录到UserRecord Public Function AddUserRecord(euserRecord As Entity.UserRecord) As Integer Implements IUser.AddUserRecord '每一个字段都填写时,可省略前面括号里的内容 'Dim strSQL As String = "insert into T_UserRecord values(@userID,@level,@loginTime,@logoutTime,@computer,@isOnline)" Try Dim strSQL As String = "insert into T_UserRecord (userID,userLevel,loginTime,logoutTime,computer,isOnline)values(@userID,@level,@loginTime,@logoutTime,@computer,@isOnline)" Dim params() As SqlParameter = {New SqlParameter("@userID", euserRecord.ProuserID), New SqlParameter("@level", euserRecord.ProuserLevel), New SqlParameter("@loginTime", euserRecord.ProloginTime), New SqlParameter("@logoutTime", euserRecord.PrologoutTime), New SqlParameter("@computer", euserRecord.Procomputer), New SqlParameter("@isOnline", euserRecord.ProisOnline)} Dim helper As New SqlHelper.sqlHelper Dim intResult = helper.ExecuteNoQuery(strSQL, CommandType.Text, params) Return intResult Catch ex As Exception Throw New Exception End Try End Function改动后的登陆时序图:
两幅登陆时序图造成鲜明的对照,这样用户点击“登陆”button后,U层负责採集用户输入的username和password,而后仅仅需调用一个Check()函数进行验证。B层处理业务逻辑。先推断用户信息,若输入正确则进行下一步加入用户上机记录。D层和数据库打交道。进行增删改查。U层根本不知道详细的验证用户细节。这样就解耦了,各司其职。code
小结:从去年的第一次机房收费系统。到现在的我的版重构。以及接下来的合做重构,每一步都是跨越。最初咱们是纯面向过程的,现在学习了三层架构,使用了设计模式,但距离面向对象仍然很是遥远。咱们正在一步一步向前走,这个过程很是重要。比如是唐僧西天取经,假设让孙悟空翻个跟头就能取到经。那就不会有经典的《西游记》了。