Knocked over the three-layer login for a long time, understand how the three layer is how to pass, but is always the feeling, others say knock a few lines will understand, but, you know ... At this time I think of Miss Rice's words: no matter how, listen to do first, finished talking about their own ideas. If you always do not, you will never progress. So, I started the "seven-tier" version of the login.
Let's use a package diagram to see how this "seven-layer" is delivered:
It is shown from the figure that this "seven layer" is based on the three layer of the U-layer, B-layer, D-layer decoupling. Originally in the three layer is the U layer to receive incoming data, and then call the B layer method, the data passed to B layer, and then B layer called D layer method. But this is not true decoupling, because when B or D is changed, the U-layer changes as well. So we changed the three-storey structure to the "seven-layer" structure.
A surface layer is added between the U-layer and B-layer to allow the appearance to decouple the U-layer and the B-layer so that the U-layer does not know the B-layer change or even the B-layer can realize its function. Similarly, we add an interface class between the B and D layers, including various methods, the implementation of this method is written in the D layer, so B-layer and D-layer can be well decoupled. Abstract factory is to facilitate the replacement of the database later.
First build the following Windows application and class library:
U-Layer:
<span style= "FONT-SIZE:18PX;" >imports JFBLL. Loginbllimports jfentity.loginuserentityimports facadeimports System.Net.DnsPublic Class frmlogin Private Sub btnok_cl Ick (sender as Object, e as EventArgs) Handles btnok.click ' Judging text box If Txtuserid.text = "then Mess Agebox.show ("Please enter user name!") "," ", MessageBoxButtons.OK, messageboxicon.exclamation) Txtuserid.text =" "Txtuserid.focus () Exit Sub ElseIf txtpwd.text = "Then MessageBox.Show (" Enter the password!) "," ", MessageBoxButtons.OK, messageboxicon.exclamation) Txtpwd.text =" "Txtpwd.focus () Exit Sub End If Try Dim facadelogin As New facade.loginfacade ' defines a skin layer object Dim UserInfo As New jfentity.loginuserentity () ' Define an entity ' to pass the input in the text box to the entity Userinfo.userid = TxtUserID.Text.Trim () Userinfo.password = TxtPwd.Text.Trim () Dim strResult1 as Boolean strResult1 = FacadElogin.checkuser (UserInfo) ' passes the contents of the U-layer text box into the skin layer and then determines If strResult1 = False then MsgBox by the appearance of passing in the B layer The user does not exist! ") Txtuserid.text =" "Txtpwd.text =" "Txtuserid.select () txt Userid.focus () End If Dim table as DataTable table = Facadelogin.checkpwd (UserInfo) If trim (txtpwd.text) = trim (table. Rows (0). Item (3)) then MsgBox ("Login Successful! ") ' frmmain.show () Txtuserid.text =" "Txtpwd.text =" "End If Catch ex as Exception MsgBox ("The user does not exist or the password is incorrect!") ") Txtuserid.text =" "Txtpwd.text =" "Txtuserid.select () Txtuserid.focus () End Try End Sub Private Sub Btncancel_click (sender as Object, e as EventArgs) Handles Btncancel.click M E.close () End Sub Private Sub frmlogin_load (sender as Object, e as EventArgs) Handles mybase.load Dim ADdress () as System.Net.IPAddress Address = System.Net.Dns.GetHostEntry (System.Net.Dns.GetHostName ()). AddressList Txtcomputer. Text = System.Net.Dns.GetHostName (). ToString () End subend class</span>appearance layer:
<span style= "FONT-SIZE:18PX;" > '/***************************************** ' class name: Loginfacade ' namespace: Facade ' creation time: 2015/6/7 16:18:00 ' Author: Shanyu ' Group: ' Modify Time: ' Modified person: ' Version number: v1.0.0 ' ******************************************imports jfbllimports jfentitypublic Class Loginfacade ' <summary> ' Check user name ' </summary> ' ' <param name= ' UserInfo ' ></param > ' <returns></returns> ' <remarks></remarks> public Function CheckUser (ByVal User Info as Jfentity.loginuserentity) as Boolean Dim isuserexists as New jfbll. LOGINBLL () Dim flag as Boolean flag = isuserexists.isuserexists (UserInfo) If flag = True Then Return True Else return False End If return flag end Function ' <summary> ; "Check Secret" </summary> "<param name=" UserInfo "></param>" <returns></returns> "<remarks></remarks>Public Function checkpwd (ByVal UserInfo as jfentity.loginuserentity) as DataTable Dim ispwd as New jfbll. LOGINBLL () Dim table as DataTable table = Ispwd.ispwdright (UserInfo) Return table End FunctionEnd Class</span>
Factory:
<span style= "FONT-SIZE:18PX;" > '/***************************************** ' class name: Loginfactory ' namespace: Factory ' creation time: 2015/6/7 16:18:59 ' Author: Shanyu ' group: ' Modified: ' Modified by: ' Version number: v1.0.0 ' ******************************************imports system.configuration ' Add a reference to the configuration file Imports System.Reflection ' Add to the reflection of the application of the Imports Idalpublic class Loginfactory ' read configuration file, each Class D layer in the configuration file corresponding to a key ' under this sentence is to set the key into a variable, and then the next The class of the D layer can be applied only with this variable in the method of the polygon. Dim strDB as String = System.Configuration.ConfigurationSettings.AppSettings ("dbstring") ' <summary> ' instantiation The class of the User_info table in a jfdal ' </summary> ' ' <returns></returns> ' <remarks></remarks> ; Public Shared function Createuserinfo () as Iuserinfodal ' CType is an inline function that converts the expression from the previous part to the following type Return CType (Assembly . Load ("Jfdal"). CreateInstance ("Jfdal" & "." & "Userdal"), Iuserinfodal) ' returns Iuserinfodal ' return CType (Assembly.Load ("Jfdal "). CreateInstance ("Jfdal. Logindal "), Iuserinfodal) End FunctionEndClass</span>
Interface:
<span style= "FONT-SIZE:18PX;" >imports jfentitypublic Interface iuserinfodal ' userinfo is the entity of the user information, which is instantiated by the entity class. ' is the so-called transfer entity. ' This interface defines a method to check whether a user exists Function Selectuser (ByVal UserInfo as jfentity.loginuserentity) as DataTable "<summary>" " Add user "</summary>" <param name= "UserInfo" ></param> " <returns></returns> "<remarks></remarks> Function AddUser (ByVal UserInfo as loginuserentity) as Datatableend interface</span>
b Layer:
<span style= "FONT-SIZE:18PX;" > '/***************************************** ' class name: Loginbll ' namespace: Jfbll ' creation time: 2015/6/7 16:12:49 ' Author: Shanyu ' Group: ' Modified: ' Modified by: ' Version number: v1.0.0 ' ******************************************imports factoryimports idalimports jfentitypublic Class LOGINBLL ' checks if the user has public Function isuserexists (ByVal UserInfo as jfentity.loginuserentity) as Boolean ' Dim f Actory as New factory.loginfactory () Dim Iuser as Idal. Iuserinfodal ' call ' Create user ' factory method iuser = Factory.LoginFactory.CreateUserInfo Dim table as New DataTable Dim flag as Boolean table = Iuser.selectuser (UserInfo) ' because the form returned in SqlHelper is in tabular form (adataset.tables (0)) and begins with The first column is represented as 0, so item (0) represents the user name If table. Rows (0). Item (0) = 0 Then flag = False Else flag = True End If Return flag End functi On to see if the password is correct public Function ispwdright (ByVal UserInfo as jfentity.loginuserentity) as DataTable ' Dim factory as N EW FactOry. Loginfactory () Dim Iuser as Idal. Iuserinfodal Dim table as DataTable ' intermediate variable, used to store D-layer query to Data Iuser = Factory.LoginFactory.CreateUserInfo ' Call Factory C The Reateuserinfo method creates Iuser table = Iuser.selectuser (UserInfo) ' method to invoke the interface Selectuser Return table End functionend Class</span>
Layer D:
<span style= "FONT-SIZE:18PX;" > '/***************************************** ' class name: Logindal ' namespace: Jfdal ' creation time: 2015/6/7 16:01:07 ' Author: Shanyu ' Group: ' Modified: ' Modified by: ' Version number: v1.0.0 ' ******************************************imports System.Data.SqlClient ' System. The Data.sqlclient namespace is a sqlsever. NET Framework Data provider ' SQL Sever. NET Framework Data provider that describes a collection of classes that are used to access SQL in a managed space Sever Database Imports Jfentityimports idalimports sqlhelperpublic Class userdal:implements idal. Iuserinfodal ' implement method in interface ' declares and instantiates SqlHelper class Private SqlHelper as Sqlhelper.sqlhelper = New sqlhelper.sqlhelper () Pub Lic Function Selectuser (UserInfo as loginuserentity) as DataTable Implements iuserinfodal.selectuser Dim Sql as Str ing Dim table As DataTable ' intermediate variable for storing information found from the database Dim Sqlparams as SqlParameter () = {New SqlParameter ("@UserI D ", Userinfo.userid), New SqlParameter (" @Password ", Userinfo.password)} ' declares and instantiates an array of arguments SQL =" SELECT * FROM User_info where [email protected] and [email protected] " ' The next sentence is to call the Getdatatable () method in the SqlHelper class to execute the query and get the return value of table = Sqlhelper.getdatatable (SQL, CommandType.Text, Sqlpa Rams) Return table End functionend class</span>
Entity:
<span style= "FONT-SIZE:18PX;" > '/***************************************** ' class name: Loginuserentity ' namespace: jfentity ' creation time: 2015/6/7-16:06:11 ' make By: Shanyu ' group: ' Modified Time: ' Modified by: ' Version number: v1.0.0 ' ******************************************public Class loginuserentity ' variable name than database The field name is more than one _ private _username as String private _password as String private _userid as String private _level as The string Private _holder as String ' follows the read-write property ' name as in the database as Public UserName () as String Get Return _username End Get Set (ByVal value as String) _username = value End Set End Property Public Property UserID () as String get Return _userid End get Set (ByVal value As String) _userid = value End Set End Property Public Property Password () As String Get Return _password End Get Set (ByVal value as String) _password = value End Set EnD property, level as String get Return _level End Get Set (ByVal value as String) _level = value End Set End Property public property Holder as String Get Return _holder End Get Set (ByVal value as String) _holder = value End Set End Propert Yend class</span>
SQLhelper:
<span style= "FONT-SIZE:18PX;" > '/***************************************** ' class name: SqlHelper ' namespace: SqlHelper ' creation time: 2015/6/7 16:48:39 ' Author: Shanyu ' Group: ' Repair Change time: ' Modified by: ' Version number: v1.0.0 ' ******************************************imports System.Data.SqlClientImports System.configurationimports system.datapublic Class sqlHelper Private Shared ConnectionString as String = Configuration Manager.appsettings ("connstring") ' <summary> ' Execute query operation with parameters ' ' </summary> ' ' <param name= ' Cmdtxt "> Parameter cmdtxt for the SQL statement to be executed </param>" ' <param name= "cmdtype" > Query mode </param> ' <param na Me= "Paras" > Query command Parameters paras</param> "<returns> query after the way returned as a table, such as the following adataset. Tables (0) </returns> "<remarks></remarks> public Shared Function getdatatable (ByVal cmdtxt as St Ring, ByVal Cmdtype as CommandType, ByVal paras as SqlParameter ()) as DataTable Dim conn as SqlConnection = New Sql Connection (ConnectionString) ' establishing a database connectionDim cmd As SqlCommand ' Define command variable cmd Dim adaptor As SqlDataAdapter ' Define data adapter Dim Adataset As DataSet ' define and instantiate data buffer Object, the object that is passed in from the database, cmd = New SqlCommand (cmdtxt, conn) ' Executes the instantiated command variable above conn and executes the statement cmdtype CMD. CommandType = Cmdtype ' executes the type cmd of the command. Parameters.addrange (paras) ' command execution parameter adaptor = New SqlDataAdapter (cmd) ' Binds the result to the data adapter variable adaptor above Adataset = Ne W DataSet Try ' If the database connection status is off, open the If Conn. State = Connectionstate.closed Then Conn. Open () End If adaptor. Fill (adataset) ' fills the adaptor object with the data of the query Catch ex as Exception ' error handler, and an error prompts MsgBox (ex. Message, "database operation") Finally ' If the connection status is open, close it and release the memory if Conn. State = ConnectionState.Open Then Conn. Close () End If End Try ' Returns the result as a table return Adataset. Tables (0) End Function function getdatatable () as DataTable Throw New notimplementedexception EndFunctionEnd class</span>
What we call "seven layer" but is the three layer of the structure of the further decoupling, but still is three layer of thought, we do not think that the layer is more decoupled, in fact, the idea of the three layer is a decoupling thought.
For the SqlHelper study is not very deep, so just write a, wait until the back of the time will be further improved ...
Computer room charge System--"seven layer" login