最近一直在学习三层架构,前些天同样也写了一篇同样的博客,今天主要是通过一个登录的实例给大家讲解每部分的作用和相应代码的实现。
先将实现三层架构的UML图给大家,帮助大家更好的理解三层。
1. UI作用 (1) 向用户展示特定业务数据 (2) 采集用户的输入信息和操作
代码语言:javascript复制Public Class frmLogin
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
'定义实例
Dim en_User As New Entity.en_UserInfo
Dim bl_User As New BLL.bl_Login
'赋值
en_User.UserID = txtUserName.Text.Trim
en_User.Password = txtPassword.Text.Trim
'采集用户信息,展示登录结果
If bl_User.LoginManager(en_User) Then
MsgBox("登陆成功")
Else
MsgBox("登录失败")
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
2. BLL作用 (1) 从DAL获取数据,以供UI显示 (2) 从UI获取用户指令和数据,执行业务逻辑
(3) 从UI获取用户指令和数据,通过DAL写入数据
代码语言:javascript复制Imports Entity
Imports DAL.dal_Login
Public Class bl_Login
Public Function LoginManager(ByVal User As Entity.en_UserInfo) As Boolean
Dim dal_User As New DAL.dal_Login
Dim en_User As New Entity.en_UserInfo
en_User.UserID = User.UserID
'调用D层的方法
'en_User = dal_User.selectUser(en_User)
en_User = dal_User.selectUser(User)
'判断操作
If en_User.UserID.Trim = User.UserID And en_User.Password.Trim = User.Password Then
Return True
Else
Return False
End If
End Function
End Class
3. 实体(Entity)层 (1) 属于哪一层很难界定,比较倾向于业务逻辑层,也可以是数据访问层 (2) 目的:为了封装数据的,数据为了在三个层次之间流畅的流转 (3) 独立于其他三个层次的,不会引用任何的层次,其他三层都需要引用实体层
代码语言:javascript复制Imports Entity
Public Class en_UserInfo
'声明UserID属性
Private e_UserID As String
Public Property UserID As String
Get
Return e_UserID
End Get
Set(ByVal value As String)
e_UserID = value
End Set
End Property
'声明Password属性
Private e_Password As String
Public Property Password As String
Get
Return e_Password
End Get
Set(ByVal value As String)
e_Password = value
End Set
End Property
End Class
4. DAL层 作用主要是与操作数据库 (1) 从数据源加载数据(select) (2) 向数据源写入数据(insert/update) (3) 从数据源删除数据(delete)
代码语言:javascript复制Imports Entity
Imports System.Data.SqlClient
Imports System.Data
Public Class dal_Login
'连接数据库
Dim strConn As String = "server =192.168.24.59;database=Login;uid=sa;pwd=123456"
Dim sqlConnect As SqlConnection = New SqlConnection(strConn)
'自定义检查参数
Function selectUser(ByVal User As Entity.en_UserInfo) As Entity.en_UserInfo
Dim read As SqlDataReader
Dim en_User As New Entity.en_UserInfo
'其实下面的这段程序就相当于dim sql as string ="select ID,UserName,PWD From Users Where UserName='"User.UserName"' And PWD='"User.PWD"'"
'而写成这个程序块是为了防止Sql注入,即安全性考虑。
'@UserName相当于传了一个参数,("@UserName", User.UserName)相当于给参数名字传递了参数。
Dim sql As String = "Select UserID,Password From UserInfo Where UserID=@UserID And Password=@Password"
Dim sqlCmd As SqlCommand = New SqlCommand(sql, sqlConnect) '创建sqlCommand对象
'Dim UserDataTable As New DataTable '定义一个DataTable对象
sqlCmd.CommandText = sql '获取SQL语句的具体内容
sqlCmd.CommandType = CommandType.Text '获取上述SQL语句的具体类型,在此为Select
sqlCmd.Parameters.Add(New SqlParameter("@UserID", User.UserID)) '若用成eUser.UserName则会出现参数未传递的错误提示
sqlCmd.Parameters.Add(New SqlParameter("@Password", User.Password))
sqlConnect.Open()
read = sqlCmd.ExecuteReader() '执行查询语句,并生成一个DataReader
read.Read()
'读取查询到的数据,并返回给相应的属性
While read.Read() '获取数据库中相应字段的数据
'数组必须从零开始读取,否则会超出其界限
User.UserID = read.GetString(0)
User.Password = read.GetString(1)
End While
'如果用户存在的话,将数据库表中检索的记录对应赋值给参数
'User.UserID = read.Item("UserID")
'User.Password = read.Item("Password")
Return User '返回查询到的实体
sqlConnect.Close()
End Function
End Class