自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三層架構(gòu)實(shí)現(xiàn)登錄(工廠+反射+配置文件)

開(kāi)發(fā) 架構(gòu)
本文介紹了三層架構(gòu)實(shí)現(xiàn)登錄,雖然只有幾十行的代碼,但是現(xiàn)在理解的還不夠透徹,實(shí)現(xiàn)過(guò)程中也遇到了很多問(wèn)題,學(xué)到了很多,還缺乏更多的實(shí)踐去學(xué)習(xí),去發(fā)現(xiàn)問(wèn)題。

分層思想:

三層開(kāi)發(fā)就是將整個(gè)業(yè)務(wù)應(yīng)用劃分為表示層、業(yè)務(wù)邏輯層、數(shù)據(jù)訪問(wèn)層、數(shù)據(jù)庫(kù)等,明確地將客戶(hù)端的表示層、業(yè)務(wù)邏輯訪問(wèn)、和數(shù)據(jù)訪問(wèn)及數(shù)據(jù)庫(kù)訪問(wèn)劃分出來(lái),有利于系統(tǒng)的開(kāi)發(fā),維護(hù)、部署和擴(kuò)展。

其實(shí)總結(jié)一句話,是為了實(shí)現(xiàn)“高內(nèi)聚、低耦合”。采用“分而治之”的思想,把問(wèn)題劃分開(kāi)來(lái)各個(gè)解決,易于控制,易于延展,易于分配資源。

以登錄demo為例:

工廠+反射+配置文件

三層 UML圖:

 

通過(guò)UML圖,我們可以很清楚的看到各層間的依賴(lài)關(guān)系。

NET設(shè)計(jì)方案:

各層代碼實(shí)現(xiàn):

UI層

  1. '--界面  
  2. Imports Entity  
  3. Imports BLL  
  4. Public Class Login  
  5.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
  6.         Dim EntityUser As New Entity.EntityUser  
  7.         Dim BLLUser As New BLL.BLLUser  
  8.         EntityUser.User_ID = txtID.Text  
  9.         EntityUser.User_Key = txtKey.Text  
  10.         If BLLUser.GetBase(EntityUser) Then  
  11.             MsgBox("登錄成功!")  
  12.         Else  
  13.             MsgBox("登錄失敗!")  
  14.         End If  
  15.     End Sub  
  16. End Class  

 

BLL層

  1. '-- Dim DalF As New Factory,在調(diào)用工廠的時(shí)候?qū)⒔涌诘念?lèi)型做為參數(shù)傳進(jìn)去,在工廠中在通過(guò)接口類(lèi)型去查找具體的實(shí)現(xiàn)對(duì)象   
  2. Imports Entity  
  3. Imports [Interface]  
  4. Imports FactoryClass  
  5. Public Class BLLUser  
  6.     Public Function GetBase(ByVal EntityUser As Entity.EntityUser) As Boolean  
  7.         Dim DalF As New Factory  
  8.         Dim Entity_User As New Entity.EntityUser  
  9.         Entity_User.User_ID = EntityUser.User_ID  
  10.         Entity_User = DalF.Interface_User.GetBase(Entity_User)  
  11.         If Entity_User.User_Key = EntityUser.User_Key Then  
  12.             Return True  
  13.         Else  
  14.             Return False  
  15.         End If  
  16.     End Function  
  17. End Class 

Factory類(lèi)

  1. '-- 反射+配置文件實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪問(wèn),更換數(shù)據(jù)庫(kù)只需要更改配置文件   
  2. '--AppSetting讀取配置文件中的類(lèi)別字符串  
  3. Imports [Interface]  
  4. Imports System.Reflection  
  5. Public Class Factory  
  6.     Private Shared ReadOnly AssemblyName As String = "DAL" 
  7.     Dim DataBase As String = System.Configuration.ConfigurationSettings.AppSettings("sql")  
  8.     Function Interface_User() As [Interface].InterfaceUser  
  9.         Dim ClassName As String = AssemblyName + "." + DataBase + "DALUser" 
  10.         Return CType(Assembly.Load(AssemblyName).CreateInstance(ClassName), [Interface].InterfaceUser)  
  11.     End Function  
  12. End Class 

Interface接口層

  1. <span style="color:#000000;">'--定義接口,通過(guò)引用使DAL層繼承,實(shí)現(xiàn)接口  
  2. Imports Entity  
  3. Public Interface InterfaceUser  
  4.     Function GetBase(Entity_User As Entity.EntityUser) As Entity.EntityUser  
  5. End Interface</span> 

DAL層

  1. <span style="color:#000000;">'--操作數(shù)據(jù)庫(kù)  
  2. Imports Entity  
  3. Imports [Interface]  
  4. Imports System.Data.SqlClient  
  5.  
  6. Public Class DALUser : Implements [Interface].InterfaceUser  
  7.     Dim ConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")  
  8.     Dim sqlconn As SqlConnection = New SqlConnection(ConnStr)  '---連接收據(jù)庫(kù)  
  9.  
  10.     Function GetBase(Entity_User As Entity.EntityUser) As Entity.EntityUser Implements [Interface].InterfaceUser.GetBase  
  11.         Dim sqldata As String = "select * from User_Info where User_ID='" & Entity_User.User_ID & "'"   '---SQL語(yǔ)句,從User_Info中讀取所有信息  
  12.         Dim sqlcmd As New SqlCommand(sqldata, sqlconn)    '---連接數(shù)據(jù)庫(kù) 打開(kāi)User_Info表  
  13.         Dim sqlread As SqlDataReader           '---SqlDataReader讀取數(shù)據(jù)庫(kù)的方法,只讀  
  14.         Try  
  15.             sqlconn.Open()  
  16.             sqlread = sqlcmd.ExecuteReader   '---ExecuteReader 方法,查詢(xún)數(shù)據(jù)庫(kù)并得到結(jié)果。ExecuteReader 返回SqlDataReader  
  17.             sqlread.Read()                                    '---sqlcmd.ExecuteReader 的 Read()  讀取表中的記錄,每次調(diào)用都是返回一行的結(jié)果集。  
  18.             Entity_User.User_ID = sqlread.Item("User_ID")  
  19.             Entity_User.User_Key = sqlread.Item("User_Key")  
  20.  
  21.             Return Entity_User  
  22.         Catch ex As Exception  
  23.             Entity_User.User_Key = "" 
  24.             Return Entity_User  
  25.         End Try  
  26.     End Function  
  27. End Class  
  28. </span> 

Entity實(shí)體層

  1. '----實(shí)體類(lèi)負(fù)責(zé)實(shí)體的表示和數(shù)據(jù)的傳遞,不包含任何邏輯性?xún)?nèi)容。  
  2. Public Class EntityUser  
  3.     Private strUser_ID As String  
  4.     Private strUser_Key As String  
  5.     Public Property User_ID As String  
  6.         Get                                  '---獲取值  
  7.             Return (strUser_ID)  '---獲取User_ID屬性返回strUser_ID  
  8.         End Get  
  9.         Set(value As String)     '---設(shè)置值  
  10.             strUser_ID = value    '---傳值,放在value參數(shù)里  
  11.         End Set  
  12.     End Property  
  13.     Public Property User_Key As String  
  14.         Get  
  15.             Return (strUser_Key)  
  16.         End Get  
  17.         Set(value As String)  
  18.             strUser_Key = value  
  19.         End Set  
  20.     End Property  
  21. End Class 

配置文件

  1. '--  
  2. <?xml version="1.0" encoding="utf-8" ?> 
  3. <configuration> 
  4.     <startup> 
  5.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
  6.     </startup> 
  7.   <appSettings> 
  8.     <add key ="ConnStr" value="Data Source=(Local);DataBase=Login;User ID=sa;Password=123456"></add> 
  9.     <add key ="sql" value =""></add> 
  10.   </appSettings> 
  11. </configuration> 

附:時(shí)序圖

雖然只有幾十行的代碼,但是現(xiàn)在理解的還不夠透徹,實(shí)現(xiàn)過(guò)程中也遇到了很多問(wèn)題,學(xué)到了很多,還缺乏更多的實(shí)踐去學(xué)習(xí),去發(fā)現(xiàn)問(wèn)題。

原文鏈接:http://blog.csdn.net/akkzhjj/article/details/8614726

責(zé)任編輯:林師授 來(lái)源: akkzhjj的博客
相關(guān)推薦

2011-04-19 13:53:41

三層架構(gòu)

2013-05-30 15:02:33

dom4j反射

2013-01-09 11:00:20

架構(gòu)開(kāi)發(fā)三層架構(gòu).NET架構(gòu)

2009-08-26 18:20:42

三層架構(gòu)

2011-05-12 14:24:14

三層架構(gòu)

2009-07-28 17:25:14

ASP.NET三層結(jié)構(gòu)

2011-08-08 14:14:03

架構(gòu)

2012-02-03 09:44:33

.NET

2009-04-30 15:56:50

三層架構(gòu)MVCMVP

2009-07-28 15:08:50

MVC三層架構(gòu)實(shí)例

2010-09-29 14:01:05

三層交換機(jī)配置DHCP

2015-07-02 10:57:11

General框架架構(gòu)開(kāi)發(fā)

2018-10-31 14:32:53

數(shù)據(jù)中心網(wǎng)絡(luò)架構(gòu)

2018-03-08 15:30:31

超融合架構(gòu)傳統(tǒng)三層架構(gòu)

2009-08-05 10:07:20

交換機(jī)配置實(shí)驗(yàn)

2009-05-06 09:40:04

LINQWEB開(kāi)發(fā)構(gòu)架

2012-02-07 10:40:13

MVCJava

2014-02-12 10:07:07

三層交換原理

2009-07-28 17:18:33

2009-04-21 11:27:52

MVCJSPJDBC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)