一、概念简介
七层是在三层的基础上形成的,三层为UI层、BLL层、DAL层,在此基础上,增添了Facade层、Factory层、IDAL层、Entity层四层。
UI层主要是与用户接触的界面;
BLL层主要负责一些功能的业务逻辑判断;
DAL层是负责与数据库接触;
实体层Entity是为了和数据库表解耦,使要访问的字段都在其中;
Facade层是在UI层与BLL层之间增添的一层,我们也称为外观层,这样使复杂的子系统提供一个简单的接口,是的耦合度降低;
Factory,抽象工厂,为了减少数据的类型之前的转换,从而使得BLL和DAL之间的耦合度增加;
SqlHelper,将数据库的数据表存储在这,在其他层调用DAL时查询语句,通过SqlHelper的DataSet将数据表的查询信息返回给DAL;
二、七层间关系
七层之间的关系可以用如下的包图来表示:
三、机房收费系统七层登录代码
UI层:
Imports Entity.UserEnity
Imports Facade
Imports System.Net.Dns
Public Class frmLogin
'Private Property MessaBoxButtons As Object
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
If txtUserID.Text = "" Then
Windows.Forms.MessageBox.Show("请输入用户名", "", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
txtUserID.Text = ""
txtPassWord.Text = ""
Exit Sub
ElseIf txtPassWord.Text = "" Then
Windows.Forms.MessageBox.Show("请输入密码", "", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
txtPassWord.Text = ""
txtPassWord.Focus()
Exit Sub
End If
'Try
Dim FacadeLogin As New Facade.LoginFacade
Dim UserInfo As New Entity.UserEnity
UserInfo.UserID = txtUserID.Text.Trim()
UserInfo.PWD = txtPassWord.Text.Trim()
Dim strResult1 As Boolean
strResult1 = FacadeLogin.CheckUser(UserInfo)
If strResult1 = False Then
MsgBox("用户不存在")
txtUserID.Text = ""
txtPassWord.Text = ""
txtUserID.Select()
txtUserID.Focus()
Else
'Dim table As DataTable
'table = FacadeLogin.CheckPwd(UserInfo)
'If Trim(txtPassWord.Text) = Trim(table.Rows(0).Item(3)) Then
MsgBox("恭喜,登录成功", , "提醒")
' txtUserID.Text = ""
' txtPassWord.Text = ""
'End If
''Catch ex As Exception
'MsgBox("用户不存在或密码不正确")
'txtUserID.Text = ""
'txtPassWord.Text = ""
'txtUserID.Select()
'txtUserID.Focus()
'End Try
Me.Hide()
frmmain.Show()
End If
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Facade层:
Imports BLL
Imports Entity
Public Class LoginFacade
Public Function CheckUser(ByVal User_Info As Entity.UserEnity) As Boolean
Dim IsUserExists As New BLL.LoginBLL()
Dim flag As Boolean
flag = IsUserExists.IsUserExists(User_Info)
If flag = True Then
Return True
Else
Return False
End If
End Function
Public Function CheckPwd(ByVal User_Info As Entity.UserEnity) As DataTable
Dim IsPwd As New BLL.LoginBLL()
Dim table As DataTable
table = IsPwd.IsPwdright(User_Info)
Return table
End Function
End Class
IDAL接口:
Imports Entity
Public Interface IuserinfoDAL
Function selectUserPWD(ByVal User_Info As Entity.UserEnity) As DataTable
End Interface
Factory层:
Imports System.Configuration
Imports System.Reflection
Imports IDAL
Public Class LoginFactory
'Dim strDB As String = System.Configuration.ConfigurationManager.AppSettings("DBString")
Public Function CreateUserInfo() As IDAL.IuserinfoDAL
Return CType(Assembly.Load("DAL").CreateInstance("DAL" & "." & "UserDALsql"), IDAL.IuserinfoDAL)
End Function
End Class
DAL层:
Imports System.Data.SqlClient
Imports Entity
Imports IDAL
Imports System.Data
Imports DAL
Public Class UserDALsql : Implements IDAL.IuserinfoDAL
'声明并实例化SQLHelper类
'Public SqlHelper As sqlHelper = New sqlHelper()
'SQLhelper.sqlHelper sql =new SQLhelper.sqlHelper()
'Dim SqlHelper As DAL.sqlHelper
Public Function selectUserPWD(User_Info As UserEnity) As DataTable Implements IuserinfoDAL.selectUserPWD
Dim Sql As String
Dim sqlhelper As New SqlHelper.SqlHelper
Dim table As New DataTable
Dim sqlParams As SqlParameter() = {New SqlParameter("@UserID", User_Info.UserID), New SqlParameter("@Password", User_Info.PWD)}
Sql = "select * from User_Info where UserID=@UserID and PWD=@Password"
table = sqlhelper.ExecSelect(Sql, CommandType.Text, sqlParams)
Return table
End Function
End Class
SqlHelper层(可放在DAL层):
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Public Class SqlHelper
'定义变量
'获取数据库连接字符串
Private ReadOnly strConnection As String = ConfigurationSettings.AppSettings("strConnection")
'设置连接
Dim conn As SqlConnection = New SqlConnection(strConnection)
'定义cmd命令
Dim cmd As New SqlCommand
Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable
Dim sqlAdapter As SqlDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
cmd.CommandText = cmdText
cmd.CommandType = cmdType
cmd.Connection = conn
cmd.Parameters.AddRange(paras) '参数添加
sqlAdapter = New SqlDataAdapter(cmd) '实例化adapter
Try
sqlAdapter.Fill(ds) '用adapter将dataset填充
dt = ds.Tables(0)
cmd.Parameters.Clear()
Catch ex As Exception
MsgBox(ex.Message, , "数据库操作") '如果操作,返回0
End Try
Return dt
End Function
Entity层:
Public Class UserEnity
Private _UserName As String
Private _PWD As String
Private _userID As String
Private _Level As String
Private _Head As String
Public Property UserName() As String
Get
Return _UserName
End Get
Set(value As String)
_UserName = value
End Set
End Property
Public Property UserID() As String
Get
Return _userID
End Get
Set(value As String)
_userID = value
End Set
End Property
Public Property PWD() As String
Get
Return _PWD
End Get
Set(value As String)
_PWD = 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 Head As String
Get
Return _Head
End Get
Set(value As String)
_Head = value
End Set
End Property
End Class
三、总结
在三层的基础上,我们接触到了七层登录,一直听很多同胞在强调七层这一条线很重要,自己对于七层登录的“一条线”也是了解了好几天时间,对于其中的很多内容都很模糊,到目前为止,还算是顺畅地理清楚,一个很重要的经验就是实践,带着自己的疑问去做,多次的尝试后,我们互更加清晰其中的思路,我们也会能够独立地完成七层。在这个过程中多跟其他同学交流是非常重要的。