您好,登錄后才能下訂單哦!
小編給大家分享一下互聯(lián)網(wǎng)中三層架構(gòu)如何實(shí)現(xiàn)登錄,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
分層思想:
三層開發(fā)就是將整個(gè)業(yè)務(wù)應(yīng)用劃分為表示層、業(yè)務(wù)邏輯層、數(shù)據(jù)訪問(wèn)層、數(shù)據(jù)庫(kù)等,明確地將客戶端的表示層、業(yè)務(wù)邏輯訪問(wèn)、和數(shù)據(jù)訪問(wèn)及數(shù)據(jù)庫(kù)訪問(wèn)劃分出來(lái),有利于系統(tǒng)的開發(fā),維護(hù)、部署和擴(kuò)展。
其實(shí)總結(jié)一句話,是為了實(shí)現(xiàn)“高內(nèi)聚、低耦合”。采用“分而治之”的思想,把問(wèn)題劃分開來(lái)各個(gè)解決,易于控制,易于延展,易于分配資源。
以登錄demo為例:
工廠+反射+配置文件
三層 UML圖:
通過(guò)UML圖,我們可以很清楚的看到各層間的依賴關(guān)系。
NET設(shè)計(jì)方案:
各層代碼實(shí)現(xiàn):
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("登錄失?。?quot;) End If End Sub End Class
BLL層
'-- Dim DalF As New Factory,在調(diào)用工廠的時(shí)候?qū)⒔涌诘念愋妥鰹閰?shù)傳進(jìn)去,在工廠中在通過(guò)接口類型去查找具體的實(shí)現(xiàn)對(duì)象 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類
'-- 反射+配置文件實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪問(wèn),更換數(shù)據(jù)庫(kù)只需要更改配置文件 '--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;">'--定義接口,通過(guò)引用使DAL層繼承,實(shí)現(xiàn)接口 Imports Entity Public Interface InterfaceUser Function GetBase(Entity_User As Entity.EntityUser) As Entity.EntityUser End Interface</span>
DAL層
<span style="color:#000000;">'--操作數(shù)據(jù)庫(kù) 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) '---連接收據(jù)庫(kù) 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語(yǔ)句,從User_Info中讀取所有信息 Dim sqlcmd As New SqlCommand(sqldata, sqlconn) '---連接數(shù)據(jù)庫(kù) 打開User_Info表 Dim sqlread As SqlDataReader '---SqlDataReader讀取數(shù)據(jù)庫(kù)的方法,只讀 Try sqlconn.Open() sqlread = sqlcmd.ExecuteReader '---ExecuteReader 方法,查詢數(shù)據(jù)庫(kù)并得到結(jié)果。ExecuteReader 返回SqlDataReader sqlread.Read() '---sqlcmd.ExecuteReader 的 Read() 讀取表中的記錄,每次調(diào)用都是返回一行的結(jié)果集。 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實(shí)體層
'----實(shí)體類負(fù)責(zé)實(shí)體的表示和數(shù)據(jù)的傳遞,不包含任何邏輯性內(nèi)容。 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) '---設(shè)置值 strUser_ID = value '---傳值,放在value參數(shù)里 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>
以上是“互聯(lián)網(wǎng)中三層架構(gòu)如何實(shí)現(xiàn)登錄”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。