三层登录完成了,在摸索中开始了七层的学习。机房重构已经进行了一段时间了,现在把机房重构最基础的七层总结一下。这里的内容还是应该多多应用,应用的多了也就能更好的理解了
七层就是在三层的基础之上,添加了外观层——设计模式的外观模式,抽象工厂加反射,接口层。
外观层:主要是把UI和BLL层分离。但是应用了一些之后,发现他除了传递参数,或者给与返回值并没有发现他有什么其他的作用
抽象工厂加反射:主要是为了更换数据库的方便性,同时把DAL层中的类转换成为IDAL层中的接口,从而使BLL层实现通过调用IDAL从而调用DAL层。
接口层:它用来定义一个统一的接口,接触BLL层和DAL层的耦合
这样,当需要改动系统的时候,非常的方便,使用七层做到了大大的解耦和,当有新的需求出现的时候或者在后期的系统维护时,仅仅需要改动某一层就可以,或者具体到某一层中的某一个类就可以,而不至于牵一发而动全身,提高了系统的可扩展性,可维护性和灵活性。
UML中一共有四大关系,即关联、依赖、泛化和实现,这里用到了三种,即关联、依赖和实现。
七层从UI层开始,逐层依赖于下一层,除了SqlHelper和Factory之外的所有的层,都要添加对Entity层的引用,这里是关联关系,因为所有的层在使用实体层的时候,首先都先要对实体进行实例化,即New一个实体,所以暂时理解为关联关系(最普通的关联关系,不是聚合也不是组合关系),而DAL层实现的是IDAL层中的方法,同时依赖于SqlHelper实现它自己的功能。
首先看一下实体层的代码
Private _userName As String
Private _passWord As String
Private _userLevel As String
Private _userID As String
'UserName属性
Public Property UserName() As Integer
Get
Return _userName
End Get
Set(value As Integer)
_userName = value
End Set
End Property
'Password属性
Public Property Password As String
Get
Return _passWord
End Get
Set(value As String)
_passWord = value
End Set
End Property
'UserLevel属性
Public Property Level As String
Get
Return _userLevel
End Get
Set(value As String)
_userLevel = value
End Set
End Property
UI层
'定义一个新的登陆实体
Dim facade As New Facade.LoginFacade
Dim myList As New List(Of Entity.LoginEntity)
Dim enUser As New Entity.LoginEntity
Dim flag As New List(Of Entity.WorklogEntity)
If txtUserName.Text = "" Then
MsgBox("请输入用户名!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If IsNumeric(txtUserName.Text) = False Then
MsgBox("用户名请输入数字!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtUserName.Text = ""
End If
If txtPassword.Text = "" Then
MsgBox("请输入密码!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Try
enUser.UserName = txtUserName.Text.Trim()
enUser.Password = txtPassword.Text.Trim()
myList = facade.SelectUser(enUser)
If myList.Count > 0 Then
MsgBox("登陆成功")
'更新Worklog
Dim enWorklog As New Entity.WorklogEntity
enWorklog.UserName = txtUserName.Text.Trim()
enWorklog.LoginDate = CStr(Format(Now(), "yyyy-MM-dd"))
enWorklog.LoginTime = CStr(Format(Now(), "HH:mm:ss"))
enWorklog.Status = "正在值班"
enWorklog.Computer = Environment.GetEnvironmentVariable("USERNAME")
flag = facade.AddWorklog(enWorklog)
enLogin.UserName = myList.Item(0).UserName
enLogin.Level = myList.Item(0).Level
frmMain.Show()
Me.Hide()
End If
Catch ex As Exception
MsgBox(ex.Message.ToString())
txtUserName.Focus()
txtUserName.SelectAll()
txtPassword.Text = ""
End Try
新增的外观层(Facade)
Public Function SelectUser(ByVal enUser As Entity.LoginEntity) As List(Of Entity.LoginEntity)
Dim userBLL As New BLL.LoginBLL
Dim myList As List(Of Entity.LoginEntity)
myList = userBLL.SelectUser(enUser)
If myList.Count = 0 Then
Throw New Exception("用户名或密码错误")
Else
Return myList
End If
End Function
B层
Public Function SelectUser(ByVal enUser As Entity.LoginEntity) As List(Of Entity.LoginEntity)
Dim factory As New Factory.LoginFactory
Dim IUser As IDAL.IUser
Dim myList As List(Of Entity.LoginEntity)
IUser = factory.CreateIUser()
myList = IUser.SelectUser(enUser)
Return myList
End Function
工厂(Factory)
Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("strDB")
'用户工厂
Public Function CreateIUser() As IDAL.IUser
'CType是一个内联函数,将前部分的表达式转换为后面的类型
Return CType(Assembly.Load("DAL").CreateInstance("DAL" & "." & "LoginDAL"), IUser) '返回IuserinfoDAL
End Function
D层
Public Function SelectUser(ByVal enUser As Entity.LoginEntity) As List(Of Entity.LoginEntity) Implements IUser.SelectUser
Dim strText As String = "select * from Login where UserName=@UserName and Password=@Password "
Dim cmdType As CommandType = CommandType.Text
Dim Parameter As SqlParameter()
Parameter = {New SqlParameter("@UserName", enUser.UserName),
New SqlParameter("@Password", enUser.Password)}
Dim SqlHelper As New Sqlhelper.SqlHelper()
Dim dt As New DataTable
Dim myList As List(Of Entity.LoginEntity)
dt = SqlHelper.ExecuteReaderTable(strText, cmdType, Parameter)
myList = ConvertHelper.convertToList(Of Entity.LoginEntity)(dt)
Return myList
接口层
Function SelectUser(ByVal enUser As Entity.LoginEntity) As List(Of Entity.LoginEntity)