【. NET room reconstruction "--look at abstract factory plus reflection to implement login

Source: Internet
Author: User

Pure three-storey machine room reconstruction finally knocked out, but also need to add design mode, this is a problem. Study a half-day time, finally to the seven-layer running steps familiar with some, but also on the abstract Factory plus reflection and configuration files to understand a little more, the following look at my new understanding.



This is a seven-layer package diagram, said to be seven layers, but seven layer is not strictly seven layer, but on the basis of the three layer with the factory, the appearance and interface, use the design mode of the place more naturally formed a layer, so there is now the seven layer.


Each layer of code

Interface Layer

After layering the function of the interface layer is very simple, can no longer have a logical judgment, may be some simple is empty or whether it is a number.

<span style= "font-family:kaiti_gb2312;font-size:18px;" >public Class frmlogin ' <summary> ' login ' </summary> ' <param name= ' sender ' ></ Param> "<param name=" E "></param>" <remarks></remarks> Private Sub Btnlogin_click (sender as Object, E as EventArgs) Handles Btnlogin.click Dim Lfac As New Facade.loginfac Dim result As String Dim User As New entity.us        Erinfo user.id = TxtID.Text.Trim User.password = TxtPwd.Text.Trim result = lfac.confirm (User) Select Case result Case "The user does not exist!" "Txtid.focus () Txtid.selectall () case" wrong password! "Txtpwd.focus () Txtpwd.selectall () case" Login successful! "MsgBox (" Login successful!) ") End Select End SubEnd Class</span> 


Appearance layer

I understand the appearance of the layer is actually put the U layer needs to use the method, so U layer only with the appearance layer to deal with, it does not need to know the specific implementation, so it is good to understand the coupling.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public Class loginfac    ' <summary> '    verify user '    </summary> '    <param name= ' user ' ></param>    ' <returns></returns> '    <remarks></remarks> public    Function Confirm (ByVal User as Entity.userinfo) as String        Dim confirmuser as New BLL. LOGINBLL        If not confirmuser.isexist and then            Return "The user does not exist!" "        Else            If not confirmuser.confirmpwd (User) then                Return" password is wrong! "            Else                Return" Login successful! "            End If End        if    end FunctionEnd class</span>


Logic judgment Layer

Logical judgment layer as the name implies, is to make a logical judgment, login to determine whether the user exists and password input is correct on the B layer to judge.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public Class loginbll ' <summary> ' verifies that the user exists ' </summary> ' <param name= ' user ' >& Lt;/param> ' <returns> boolean </returns> ' <remarks></remarks> public Function isexist ( ByVal User as Entity.userinfo) as a Boolean Dim Iuser as Idal. Iuserinfo Dim Factory as New factory.dataaccess Iuser = Factory. Createuserinfo () If Iuser.selectuser (User) (0). ID = "Then return False Else return Tr UE End If End Function ' <summary> ' verifies that the password is correct ' ' </summary> ' ' <param name= ' use R "></param>" <returns> boolean </returns> "' <remarks></remarks> public Function C Onfirmpwd (ByVal User as Entity.userinfo) as a Boolean Dim Iuser as Idal. Iuserinfo Dim Factory as New factory.dataaccess Iuser = Factory. Createuserinfo () If Iuser.Selectuser (User) (0). Password = "Then return False Else return True end If End FunctionEnd class</s Pan>


Interface

Interface is to define a method name, in order to let the D layer to achieve, I think the existence of the interface is also very good to understand the decoupling, in the B layer only need to declare an interface, the implementation does not need to understand.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public Interface iuserinfo    Function selectuser (ByVal User as Entity.userinfo) as List (of Entity.userinfo) End Interface</span>


Factory

The function of the factory is to produce the class in batch, just like the factory in reality, after giving the order, it can be mass production. In fact, interfaces and factories are like real-life bosses and factories, and bosses are intermediaries that contact customers and factories, as well as interfaces.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public Class dataaccess    Dim strdb as String = System.Configuration.ConfigurationSettings.AppSettings ("DB")    ' <summary> '    produces class ' ' </summary> '    <returns></returns>    ' < Remarks></remarks>    Function createuserinfo () as Idal. Iuserinfo        Return CType (Assembly.Load ("DAL"). CreateInstance ("DAL. Logindal "), Idal. Iuserinfo)    End functionend class</span>


Data layer

D layer is mainly to deal with the database, the information you want to query out and return, or according to your intention to update the database information, that is, database additions and deletions to check.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public Class logindal:implements Idal. Iuserinfo '    <summary> '    queries the users table for information ' ' </summary> ' '    <param name= ' User ' > </param> '    <returns> generic collection </returns>    ' <remarks></remarks> public    Function Selectuser (ByVal User as Entity.userinfo) as List (of Entity.userinfo) Implements Idal. Iuserinfo.selectuser        Dim helper As New SQLHelper        Dim sql As String = "SELECT * from Users Where ID = '" & User . ID & "'"        Dim dt As New DataTable        Dim luser As New List (of entity.userinfo)        dt = helper. ExecuteSelect (SQL, commandtype.text)        luser = converthelper.convertolist (dt, luser)        Return luser    End FunctionEnd class</span>

swapping databases with reflection

assembly.load ("assembly name"). CreateInstance ("namespace. Class name")

The assembly name is the assembly name in the class library property, which is the root namespace in the Class library property, which is the class name under that namespace. The essence of using reflection to exchange a database is that if you want to use a different database, the D layer should have different classes, such as using SQL Server when the class name is Logindalsql, the class name should be Logindalora when using Oracle. Because the database type can be changed with a string in the configuration file, this is required in the configuration file:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><configuration>  <appSettings>    <add key= " Strconn "Value=" server=localhost;database=login; User=sa; password=0722 "/>    <add key=" DB "value=" Sql "/>  </appsettings></configuration></ Span>


Then the code in the factory is

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public Class dataaccess    Dim strdb as String = System.Configuration.ConfigurationSettings.AppSettings ("DB")    ' <summary> '    using Reflection    ' </summary> '    <returns></returns>    ' < ' Remarks></remarks>    Function createuserinfo () as Idal. Iuserinfo        Return CType (Assembly.Load ("DAL"). CreateInstance ("DAL. Logindal "& strDB), Idal. Iuserinfo)    End functionend class</span>

"DB" is a string in the read configuration file, and strDB is the string that defines the database to be swapped, so that the class name returned by the factory should be "DAL." Logindalsql ". If you want to change the database, you just need to replace the "SQL" of the configuration file with "Ora", and the factory returns the class name "DAL." Logindalora ", so that different database operation classes can be replaced.


The error encountered

There are two main issues encountered in login:

1. Failed to load file or assembly "DAL" or one of its dependencies. The system cannot find the specified file

This error is addressed in a blog post by Li Shi Brother Siang: Workaround reference

2. Configuration system failed to initialize

There are many solutions to this problem online, in fact the main reason is that the configuration file is wrong, may be the wrong order or something. My error is the case, should be written in uppercase as lowercase, so that the tragedy.


Summarize

For the things you learn, no matter what, you have to start to do, the beginning of the time do not know how to do, may change and change, but it is the process to let oneself understand it deeper. In addition, learn what things do not only see the surface, do not understand the deep, so easy to get stuck on a small problem.





【. NET room reconstruction "--look at abstract factory plus reflection to implement login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.