Look at the video knocked over the example of login, as if just know the three layer has D layer, B layer, U layer, each layer is independent of each other, and then transfer the data layers by layer. But how does it come true, or is it a little bit ...
On the knock. NET version of the three-tier login before referring to a lot of people's blog, this is slowly to start, knocked on the again, I feel a lot clearer ...
First, create the following Windows applications and class libraries:
Entity class (Model) code:
<span style= "FONT-SIZE:18PX;" >public Class UserInfo private _username as String ' Private _id as Decimal ' public property ID as Decimal
' get ' Return _id ' End Get ' Set (value as Decimal) ' _id = value ' End Set ' End Property UserName as String Get Return _username End get< C20/>set (ByVal value As String) _username = value End Set End Property Private _pwd As String Public property Pwd As String get Return _pwd End Get Set (value as String) _pwd = value End Set End Propertyend class</span>
D-Layer Code: (Reference Model entity Class)
<span style= "FONT-SIZE:18PX;" > ' Reference namespace Imports System.dataimports System.Data.SqlClientImports Login.model ' imports Logindalpublic Class Userdao ' Connect database ' Dim sqlconnectstr as String = ' sever=yu;database=login; User ID =sa; password=123456 "' Dim sqlConnection1 as SqlConnection = new SqlConnection (SQLCONNECTSTR) public conn as new Sqlconne Ction ("Server=yu;database=login; User ID =sa; password=123456 ") Public Function Selectuser (ByVal Userdao as Login.Model.UserInfo) as Login.Model.UserInfo ' pass real Body parameter userinfo instead of Id,name, which makes it easy to call a parameter in an entity Dim reader as SqlDataReader ' definition SqlDataReader object Dim euser As New Lo Gin. Model.userinfo Dim sql as String = "Select Username,password from Users WHERE [email protected] and [Email&nbs P;protected]password "Dim cmd as New SqlCommand (SQL, conn) ' defines SqlCommand object cmd. CommandText = sql cmd. CommandType = CommandType.Text cmd. Parameters.Add (New SqlParameter ("@username", Userdao.username)) Cmd. Parameters.Add (New SqlParameter ("@password", USERDAO.PWD)) Conn. Open () reader = cmd. ExecuteReader while reader. Read () Euser.username = reader. GetString (0) euser.pwd = reader. GetString (1) End while Return Userdao reader. Close () End functionend class</span>
B-Layer code: (referencing D-layers and model entity classes)
<span style= "FONT-SIZE:18PX;" >imports login.dalpublic Class loginmanager public Function userlogin (ByVal Userdao as Login.Model.UserInfo) as Login.Model.UserInfo Dim Udao As New Login.DAL.UserDAO Dim eUser1 As New Login.Model.UserInfo eUser1 = Udao.selectuser (Userdao) ' Pass data to layer D ' to determine If isnothing (euser1.username) then Throw New Exception ("Login failed, Please verify your login name and password! ") Else MsgBox (" Login Successful! ") Return eUser1 end If End FunctionEnd class</span>
U-Layer Code: (Reference Model entity class and Layer B)
<span style= "FONT-SIZE:18PX;" >public Class Form1 Private Sub button1_click (sender as Object, e as EventArgs) Handles btnok.click Try Di M eUser2 as new Login.Model.UserInfo Dim EUser3 as new Login.Model.UserInfo euser2.username = TxtUserName.Text.Trim ' passes the U-layer data to entity euser2.pwd = txtPassword.Text Dim Mgr as New Login.BLL.LoginManager ' pass data to layer B EUser3 = Mgr. Userlogin (eUser2) Catch ex as Exception MessageBox.Show (ex. Message.tostring ()) End Try End Sub Private Sub btncancel_click (sender as Object, e as EventArgs) Handles Btncancel.click me.close () End subend class</span>
The three layer is characterized by: U-layer, B-layer, D-layer all refer to the entity class, but the entity class does not know the existence of three layers. The U layer passes the data to layer B, the B layer is processed, and then to the D layer, three layers are independent from each other, and if there is a change in the layer, it will not affect the other layers. In summary: The U layer is only responsible for displaying and capturing the user's information and operations, B layer is only responsible for processing business logic, layer D is only responsible for providing basic data access.
. NET Edition Layer three login