The last chapter discusses the basic concept, function and relationship of three layer, and now shows the simple application of three-layer structure design idea in the process of user landing in the system.
Vb. NET operation is as follows
Start by building the following Windows applications and class libraries
First, create the entity class
Public Class userInfo 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 _pwd As String public property Pwd As String Get Return _pwd End Get Set (value as String) _pwd = value End Set end Propertyend Class
UI layer, provided that the reference entity class (model) and the BLL
Public Class Form1 Private Sub btncancel_click (sender as Object, e as EventArgs) Handles Btncancel.click me.close () End Sub Private Sub btnok_click (sender as Object, e as EventArgs) Handles btnok.click Try Dim Euser2 as New Model.userinfo Dim Euser3 as New model.userinfo euser2. UserName = TxtUserName.Text.Trim ' passes the U-layer data to the entity Euser2. PWD = TxtPassword.Text.Trim Dim Mgr as New BLL. Loginmannager ' passes the data to layer b euser3 = Mgr. Userlogin (euser2) Catch ex as Exception MessageBox.Show (ex. Message.tostring ()) End Try End subend Class
DAL layer: Precondition Referencing entity class
Imports system.dataimports System.Data.SqlClientImports modelpublic Class user ' CREATE DATABASE link public conn as New Sqlconnec tion ("Server=lxy;database=login;user id=sa;password=123456") Public Function Selectuser (ByVal user as Model.userinfo) As Model.userinfo ' passes the entity uesrinfo instead of the parameter id.username, which makes it easy to call the parameters in the entity Dim reader As SqlDataReader Dim euse R as New model.userinfo Dim sql as String = "Select Username,password from User_info WHERE [email protected] A nd [email protected] "Dim cmd as New SqlCommand (SQL, conn) cmd. CommandText = sql cmd. CommandType = CommandType.Text cmd. Parameters.Add (New SqlParameter ("@username", User.username)) cmd. Parameters.Add (New SqlParameter ("@password", USER.PWD)) Conn. Open () reader = cmd. ExecuteReader while reader. Read () Euser.username = reader. GetString (0) euser.pwd = reader. GetString (1) End while Return Euser Conn. Close () End functionend Class
Next is the BLL layer: a precondition to reference the Dal and the entity class
Public Class Loginmannager Public Function userlogin (ByVal User as Model.userinfo) as Model.userinfo Dim Udao as N EW Dal.user Dim eUser1 as New model.userinfo eUser1 = udao.selectuser (user) transmits data to D-tier If IsNothing ( Euser1.username) then Throw New Exception ("Login failed, please verify login name and password!") ") Else MsgBox (" landed successfully! ") Return eUser1 end If End FunctionEnd Class
References:we see the main features of the three-tier architecture: The UI calls the method of the BLL class, that is, the UI knows the method of the BLL, but does not know the details of the method implementation, and the BLL layer does not know the existence of the UI. The same BLL layer calls the DAL, and the DAL does not know the existence of the BLL. Any change in any of the three accesses than either will not affect the normal work of the other two, only to readjust the change. Layer three passes an instance of an entity class as a parameter.
in short, the presentation layer only provides interfaces for software systems to interact with the user, and the business logic layer is the bridge between the interface layer and the data access layer, which is responsible for data processing and transmission;