作者:mobiledu2502891787 | 来源:互联网 | 2023-01-12 01:27
上篇文章讲到了3层的应用,而我们的机房是用7层重构的,那么什么是7层?我们为什么要要用7层,下面我来细细分解~
what?
7层就是在3层的基础上,在UI和BLL层之间增添了外观层,来解耦,在BLL和DAL层之间增添了抽象工厂层和接口层解耦。这基于我们学过的设计模式来对3层进行扩充,应用了设计模式进行练习。其实本质上也就是3层的优化。
下面来看看7层的包图:
WHY ?
添加设计模式、解耦合,易扩展
外观层解耦UI和BLL,本来一个业务功能,UI直接联系到了BLL层,每个UI连各自的BLL,未免会显得很乱,现在在他们中间添加一个外观层,让UI调用外观层来实现给BLL消息的传递,洪玉的图画的很生动。
工厂层和接口层也是同样,让BLL通过工厂和接口来实现对DAL的访问,让DAL实现IDAL(接口),也同样易扩展。
WHEN?
同3层用的时机一样,在大型系统中,需要很多功能来设计维护,那么用3层(7层)就是很好的选择,易扩展,易维护,但是这样的成本就高,在一些简单的小程序里面,可以不用。
HOW?
具体是怎么实现的呢?下面是我的VB.NET 的代码(以登陆功能为例)
UI层
Imports System.Windows.Forms
Public Class frmLogin
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'判断信息是否输入完整--------①
If txtUserName.Text = "" Then
MsgBox("请输入用户名", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
If txtPassword.Text = "" Then
MsgBox("请输入密码", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Dim fa As New Facade.LoginFacade
Dim euser As New Entity.LoginInfo
'把用户输入的信息赋值给实体-------②
Entity.LoginInfo.USER = txtUserName.Text.Trim
Entity.LoginInfo.Computer = Environment.MachineName
euser.Password = txtPassword.Text
'user.User = txtUserName.Text
Dim strResult As Boolean
'调用外观层
strResult = fa.Login(euser)
'接受返回值,并作出相应动作
If strResult = False Then
MsgBox("用户名或密码错误,请重新输入", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtUserName.Text = ""
txtPassword.Text = ""
txtUserName.Select()
txtUserName.Focus()
Exit Sub
Else
MsgBox("登录成功,欢迎您!", , "温馨提示")
'Dim frMain As New UI.frmMain
'frMain.Show()
frmMain.Show()
End If
Dim fa1 As New Facade.LoginFacade
Dim flag1 As Boolean
'将外观层传回的boolean给flag
flag1 = fa1.AddOnlineTeacher
End Sub
外观层(Facade)
Public Class LoginFacade
Public Function Login(ByVal euser As Entity.LoginInfo) As Boolean
Dim LoginB As New BLL.LoginManager
Dim flag As Boolean
'调用BLL层
flag = LoginB.Login(euser)
Return flag
End Function
end class
BLL层
Public Class LoginManager
'执行登陆逻辑,判断登陆的用户名和密码是否存在
Public Function Login(ByVal euser As Entity.LoginInfo) As Boolean
'先实例化工厂,到工厂读取配置文件,判断是哪个数据库
Dim fac As New Factory.LoginFactory
Dim IUser As IDAL.ILoginUserInfo
'调用工厂的方法,先连接上数据库,并准备好接口等着sql语句的到来
IUser = fac.CreateUserDAO
Dim table As DataTable
Dim flag As Boolean
'调用接口来使用DAO(D层),让DAO执行select等sql语句,并将DAO找到的table返回来
table = IUser.Login(euser)
Try
Entity.LoginInfo.TeacherClass = table.Rows(0)(3)
Catch
Exit Try
End Try
If table.Rows.Count = 0 Then
flag = False
Else
flag = True
End If
Return flag
End Function
工厂层(Factory)
Imports System.Reflection
Imports System.Configuration
Public Class LoginFactory
'读取配置文件
Dim DB As String = System.Configuration.ConfigurationSettings.AppSettings("DBss")
Private Shared ReadOnly assemblyName = "DAL"
Public Function CreateUserDAO() As IDAL.ILoginUserInfo
'工厂的方法就是调用接口
Dim className As String = assemblyName + "." + "LoginDAO"
Return CType(Assembly.Load(assemblyName).CreateInstance(className), IDAL.ILoginUserInfo)
End Function
End Class
接口层(IDAL)
Public Interface ILoginUserInfo
Function Login(ByVal euser As Entity.LoginInfo) As DataTable
End Interface
DAL层
Imports System.Data.SqlClient
Public Class LoginDAO : Implements IDAL.ILoginUserInfo
'引用sqlhelper Private sqlHelper As New SQLHelper.SqlHelper
’sql语句实现数据库的访问 Public Function UserClass(ByVal euser As Entity.LoginInfo) As DataTable Implements IDAL.ILoginUserInfo.UserClass Dim sql As String
'定义参数 Dim sqlParams As SqlParameter() = { New SqlParameter("@UserName", Entity.LoginInfo.USER)} sql = "select * from T_User where Chr_UserID=@UserName" Dim table As New DataTable table = sqlHelper.GetDataTable(sql, CommandType.Text, sqlParams) Return table End Functionend class
实体层(Entity)
Public Class LoginInfo
Public Shared USER As String
Private _password As String
Public Shared TeacherClass As String
Public Shared Computer As String
Public Property Password As String Get Return _password End Get Set(value As String) _password = value End Set End PropertyEnd Class
这里用shared 定义了几个全局变量,实体就是放置数据的地方,登陆要用到Username 和Password,teacherclass 和 conputer 这几个数据在UI和DAL之间的各层之间传递。
【小结】
7层并非很难,刚开始的时候,觉得7层很麻烦,不知道怎么传递怎么走通一条线,不过在小伙伴的指导下,断点调试,一步步来分析,自己理清楚了7层走的路线。当实现登陆的时候还是很有成就感的,接下来,就是其他业务的实现,一步步积累进行着。