三层:
用图做说明:
MVC:
用图做说明:
三层与MVC联系:
用图做说明:
三层与MVC区别:
第一:三层是一个分层式的软件体系架构设计;MVC是一种框架模式,强制性将应用程序的输入、处理、输出分开,是一种解决方案。
第二:三层适用于任何一个项目,MVC有选择性的使用,且主要使用在WEB页面上
第三:三层基于业务的逻辑来分;MVC基于页面来分。
第四:三层目的重点是高内聚,低耦合,即解耦;MVC目的重点是实现WEB系统的职能划分,也是解耦。
第五:三层不一定就是三层,也有可能是二层,乃至多层;MVC却总是三层
三层实例:
上面是对三层和MVC简单的一个理解,由于现在侧重点不是MVC,而是三层,所以也就没有做有关MVC的例子,对于MVC怎样也不了解。
层次展示:
窗体界面:
Common类:
与数据相关,与工程无关。
Imports System.Data.SqlClient
Public Class CDataBase
'要连接的数据库连接字符串
Public Shared Function strConnection() As String
strConnection = "data source=192.168.24.83;initial catalog=charge_sys;user id=sa;password=123456;"
End Function
Public Shared Function ExecQuery(ByVal strSql As String) As SqlClient.SqlDataReader
Dim Coon As New SqlClient.SqlConnection
Dim cmd As New SqlClient.SqlCommand
Dim Reader As SqlClient.SqlDataReader
'数据库连接
Coon = New SqlClient.SqlConnection(CDataBase.strConnection())
cmd.Connection = Coon
cmd.CommandText = strSql
cmd.CommandType = CommandType.Text
Try
Coon.Open()
'查询
Reader = cmd.ExecuteReader()
'读取字段
Catch ex As Exception
MsgBox(ex.Message())
End Try
Return Reader
Coon.Close()
End Function
End Class
DAL层:
根据权限找到适合自己的SQL语句,通过Common类访问数据库,读取相应数据资料。
Imports System.Data.SqlClient
Public Class DabaBase
Function SeletUser(ByVal leavel As Modal.Muser) As List(Of Modal.Muser)
Dim strSql As String
Dim reader As SqlClient.SqlDataReader
Dim common = New Common.CDataBase
Dim UserLeavel As Modal.Muser
Dim Dlist As New List(Of Modal.Muser)
Dim Query As String
Query = leavel.level '获取传过来的leavel字段
Select Case Query
Case "管理员"
strSql = "select * from User_Info where Level ='管理员'"
Case " 操作员"
strSql = "select * from User_Info where Level ='操作员'"
Case "一般用户"
strSql = "select * from User_Info where Level ='一般用户'"
End Select
'查询
reader = common.ExecQuery(strSql)
Try
While reader.Read()
UserLeavel = New Modal.Muser
UserLeavel.userID = reader.GetString(0).Trim
UserLeavel.PWD = reader.GetString(1).Trim
UserLeavel.level = reader.GetString(2).Trim
UserLeavel.UserName = reader.GetString(3).Trim
UserLeavel.Head = reader.GetString(4).Trim
Dlist.Add(UserLeavel)
End While
Catch ex As Exception
MsgBox(ex.Message()) '获取异常
End Try
Return Dlist
End Function
End Class
Modal类:
借助Modal传参,将UL层的参数传入DAL层,获得查询语句,同时又将获得的查询结果返回,传回UI层。
Public Class Muser
Private _userID As String
Public Property userID As String
Get
Return _userID
End Get
Set(ByVal value As String)
_userID = value
End Set
End Property
Private _PWD As String
Public Property PWD As String
Get
Return _PWD
End Get
Set(ByVal value As String)
_PWD = value
End Set
End Property
Private _Level As String
Public Property level As String
Get
Return _Level
End Get
Set(ByVal value As String)
_Level = value
End Set
End Property
Private _UserName As String
Public Property UserName As String
Get
Return _UserName
End Get
Set(ByVal value As String)
_UserName = value
End Set
End Property
Private _Head As String
Public Property Head As String
Get
Return _Head
End Get
Set(ByVal value As String)
_Head = value
End Set
End Property
End Class
BLL层:
BLL层,进行各种逻辑判断,将获取字段通过参数传到DAL层,同时又将结果返回。
Public Class BJudge
Function GetUser(ByVal leavel As Modal.Muser) As List(Of Modal.Muser)
Dim selectuser As New List(Of Modal.Muser) '初始化
Dim LDal As New DataBase.DabaBase '初始化
'传参(获取字段和查询结果)
selectuser = LDal.SeletUser(leavel)
Return LDal.SeletUser(leavel)
End Function
End Class
UI层:
接受数据,显示数据等,这里接受字段名,通过查询后,将结果显示。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'下拉框中显示字段
cmbSelect.Items.Add("管理员")
cmbSelect.Items.Add("一般用户")
cmbSelect.Items.Add("操作员")
End Sub
Private Sub cmbSelect_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSelect.SelectedValueChanged
Dim sourse As New BLL.BJudge
Dim users As New Modal.Muser
users.level = cmbSelect.SelectedItem() '选择的字段给users
DataGridView1.DataSource = sourse.GetUser(users)
End Sub
End Class
查询结果:
对于MVC我还没有具体的接触,知道的也就这么一点,我想在不久的将来,我会用到MVC,也会更加深刻的理解他。而对于三层,还没有真正的使用过他,就仅仅是做过两个小例子,简单尝试了下三层的分层,但是它最大的好处——解耦,说实在的,还不是很透彻的理解他。