分层思想:
三层开发就是将整个业务应用划分为表示层、业务逻辑层、数据访问层、数据库等,明确地将客户端的表示层、业务逻辑访问、和数据访问及数据库访问划分出来,有利于系统的开发,维护、部署和扩展。
其实总结一句话,是为了实现“高内聚、低耦合”。采用“分而治之”的思想,把问题划分开来各个解决,易于控制,易于延展,易于分配资源。
以登录demo为例:
工厂+反射+配置文件
三层 UML图:
通过UML图,我们可以很清楚的看到各层间的依赖关系。
NET设计方案:
各层代码实现:
UI层
- '--界面
- Imports Entity
- Imports BLL
- Public Class Login
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim EntityUser As New Entity.EntityUser
- Dim BLLUser As New BLL.BLLUser
- EntityUser.User_ID = txtID.Text
- EntityUser.User_Key = txtKey.Text
- If BLLUser.GetBase(EntityUser) Then
- MsgBox("登录成功!")
- Else
- MsgBox("登录失败!")
- End If
- End Sub
- End Class
BLL层
- '-- Dim DalF As New Factory,在调用工厂的时候将接口的类型做为参数传进去,在工厂中在通过接口类型去查找具体的实现对象
- Imports Entity
- Imports [Interface]
- Imports FactoryClass
- Public Class BLLUser
- Public Function GetBase(ByVal EntityUser As Entity.EntityUser) As Boolean
- Dim DalF As New Factory
- Dim Entity_User As New Entity.EntityUser
- Entity_User.User_ID = EntityUser.User_ID
- Entity_User = DalF.Interface_User.GetBase(Entity_User)
- If Entity_User.User_Key = EntityUser.User_Key Then
- Return True
- Else
- Return False
- End If
- End Function
- End Class
Factory类
- '-- 反射+配置文件实现数据库访问,更换数据库只需要更改配置文件
- '--AppSetting读取配置文件中的类别字符串
- Imports [Interface]
- Imports System.Reflection
- Public Class Factory
- Private Shared ReadOnly AssemblyName As String = "DAL"
- Dim DataBase As String = System.Configuration.ConfigurationSettings.AppSettings("sql")
- Function Interface_User() As [Interface].InterfaceUser
- Dim ClassName As String = AssemblyName + "." + DataBase + "DALUser"
- Return CType(Assembly.Load(AssemblyName).CreateInstance(ClassName), [Interface].InterfaceUser)
- End Function
- End Class
Interface接口层
- <span style="color:#000000;">'--定义接口,通过引用使DAL层继承,实现接口
- Imports Entity
- Public Interface InterfaceUser
- Function GetBase(Entity_User As Entity.EntityUser) As Entity.EntityUser
- End Interface</span>
DAL层
- <span style="color:#000000;">'--操作数据库
- Imports Entity
- Imports [Interface]
- Imports System.Data.SqlClient
- Public Class DALUser : Implements [Interface].InterfaceUser
- Dim ConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")
- Dim sqlconn As SqlConnection = New SqlConnection(ConnStr) '---连接收据库
- Function GetBase(Entity_User As Entity.EntityUser) As Entity.EntityUser Implements [Interface].InterfaceUser.GetBase
- Dim sqldata As String = "select * from User_Info where User_ID='" & Entity_User.User_ID & "'" '---SQL语句,从User_Info中读取所有信息
- Dim sqlcmd As New SqlCommand(sqldata, sqlconn) '---连接数据库 打开User_Info表
- Dim sqlread As SqlDataReader '---SqlDataReader读取数据库的方法,只读
- Try
- sqlconn.Open()
- sqlread = sqlcmd.ExecuteReader '---ExecuteReader 方法,查询数据库并得到结果。ExecuteReader 返回SqlDataReader
- sqlread.Read() '---sqlcmd.ExecuteReader 的 Read() 读取表中的记录,每次调用都是返回一行的结果集。
- Entity_User.User_ID = sqlread.Item("User_ID")
- Entity_User.User_Key = sqlread.Item("User_Key")
- Return Entity_User
- Catch ex As Exception
- Entity_User.User_Key = ""
- Return Entity_User
- End Try
- End Function
- End Class
- </span>
Entity实体层
- '----实体类负责实体的表示和数据的传递,不包含任何逻辑性内容。
- Public Class EntityUser
- Private strUser_ID As String
- Private strUser_Key As String
- Public Property User_ID As String
- Get '---获取值
- Return (strUser_ID) '---获取User_ID属性返回strUser_ID
- End Get
- Set(value As String) '---设置值
- strUser_ID = value '---传值,放在value参数里
- End Set
- End Property
- Public Property User_Key As String
- Get
- Return (strUser_Key)
- End Get
- Set(value As String)
- strUser_Key = value
- End Set
- End Property
- End Class
配置文件
- '--
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
- </startup>
- <appSettings>
- <add key ="ConnStr" value="Data Source=(Local);DataBase=Login;User ID=sa;Password=123456"></add>
- <add key ="sql" value =""></add>
- </appSettings>
- </configuration>
虽然只有几十行的代码,但是现在理解的还不够透彻,实现过程中也遇到了很多问题,学到了很多,还缺乏更多的实践去学习,去发现问题。