Correction of abstract factory and reflection in layer-3 Architecture

Source: Internet
Author: User

Whenever someone asks me about Layer 3, I always recommend my blog "Layer 3 architecture abstraction factory reflection. This blog is a simple logon example I wrote when I was studying Layer 3. In this example, the three layers are briefly introduced by logging on to this use case, and the excessive three layers + simple factory is then transitioned to the three layers + abstract factory. I dare not say it is the best in terms of method naming or code style, but I feel more than enough to reflect the three layers. However, when talking to others about Layer 3 recently, the code of this blog has been pointed out by others, although some of the methods pointed out by others make yourself unhappy or even angry! But after all, there are people who care about themselves, others who read their blogs, and others who point out their shortcomings. In this regard, this should be a good thing. If an error occurs, modify it! Make progress only when there are errors. Maybe this is what Mr. Mi always says. This time I finally realized it, and I have a profound understanding !!!


Course Length

Although there is Jia Yi, the food does not know its purpose also; although there is the ultimate, the learning does not know its good. Learn and then learn the problem, and then learn the problem. Lack of knowledge, and then self-countermeasure; knowledge of difficulties, and then self-improvement. Therefore, it is easy to learn from each other.

---- Excerpted from the book of Rites and learnings


It's really "lack of knowledge", but it's really about "self-improvement, in order to verify the correctness of the code, we decided to gradually increase the difficulty (reduce the Coupling Degree) from the simplest three layers and implement it again. I used VB. NET again and went back to the silly vs environment.

The Code is as follows:

 Simple Layer 3

Interface Layer

Public class login private sub btnlogin_click (byval sender as system. object, byval e as system. eventargs) handles btnlogin. click dim luser as new entity. user dim bcheck as new BLL. blllogin luser. user_id = txtuserid. text luser. user_pwd = txtpwd. text if bcheck. check (luser) = true then msgbox ("Logon successful! ") Else msgbox (" Logon Failed! ") End if end sub private sub btncancle_click (byval sender as system. Object, byval e as system. eventargs) handles btncancle. Click end subend class

Bll Layer

Public Class BllLogin    Function Check(ByVal User As Entity.User) As Boolean        Dim DaUser As New DAL.DalUserInfo        Dim BlUser As New Entity.User        BlUser.User_Id = User.User_Id        BlUser = DaUser.Check(BlUser)        If BlUser.User_Pwd = User.User_Pwd Then            Return True        Else            Return False        End If    End FunctionEnd Class

Dal Layer

Imports System.Data.SqlClientPublic Class DalUserInfo    Dim ConnStr As String = "Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"    Dim conn As SqlConnection = New SqlConnection(ConnStr)    Function Check(ByVal User As Entity.User) As Entity.User        Dim sql As String = "select * from UserInfo where User_ID='" & User.User_Id + "'"        Dim cmd As SqlCommand = New SqlCommand(sql, conn)        Dim read As SqlDataReader        Try            conn.Open()            read = cmd.ExecuteReader            read.Read()            User.User_Id = read.Item("User_ID")            User.User_Pwd = read.Item("User_Pwd")            Return User        Catch ex As Exception            User.User_Pwd = ""            Return User        End Try    End FunctionEnd Class

Entity Layer

Public Class User    Private userId As String    Private userPwd As String    Public Property User_Id() As String        Get            Return userId        End Get        Set(ByVal value As String)            userId = value        End Set    End Property    Public Property User_Pwd() As String        Get            Return userPwd        End Get        Set(ByVal value As String)            userPwd = value        End Set    End PropertyEnd Class

Three-tier + simple factory

The Interface Layer Code remains unchanged.

Bll Layer

Public Class BllLogin    Function Check(ByVal User As Entity.User) As Boolean        Dim DalFactory As New DAL.DFactory        Dim BlUser As New Entity.User        BlUser.User_Id = User.User_Id        BlUser = DalFactory.CreateUserInfo.Check(BlUser)        If BlUser.User_Pwd = User.User_Pwd Then            Return True        Else            Return False        End If    End FunctionEnd Class

Factory

Imports [Interface]Public Class DFactory    'Dim DataBase As String = "Access"    Dim DataBase As String = "Sql"    Function CreateUserInfo() As IUserInfo        Dim DB As IUserInfo        Select Case DataBase            Case "Sql"                DB = New DalUserInfo                'Case "Access"                ' DB = New D_UserInfoAccess        End Select        Return DB    End FunctionEnd Class

Interface

Public Interface IUserInfo    Function Check(ByVal IUser As Entity.User) As Entity.UserEnd Interface

Dal Layer

Imports System.Data.SqlClientPublic Class DalUserInfo : Implements [Interface].IUserInfo    Dim ConnStr As String = "Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"    Dim conn As SqlConnection = New SqlConnection(ConnStr)    Public Function Check(ByVal IUser As Entity.User) As Entity.User Implements [Interface].IUserInfo.Check        Dim sql As String = "select * from UserInfo where User_ID='" & IUser.User_Id + "'"        Dim cmd As SqlCommand = New SqlCommand(sql, conn)        Dim read As SqlDataReader        Try            conn.Open()            read = cmd.ExecuteReader            read.Read()            IUser.User_Id = read.Item("User_ID")            IUser.User_Pwd = read.Item("User_Pwd")            Return IUser        Catch ex As Exception            IUser.User_Pwd = ""            Return IUser        End Try    End FunctionEnd Class

Layer 3 + Abstract Factory

The Interface Layer Code remains unchanged.

Bll Layer Code unchanged

Interface code unchanged

Factory Code

Imports [Interface]Imports System.ReflectionPublic Class DFactory    'Dim DataBase As String = "Access"    'Dim DataBase As String = "Sql"    Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("DBString")    Function CreateUserInfo() As IUserInfo        Return CType(Assembly.Load("DAL").CreateInstance("DAL.DalUserInfo" & strDB), IUserInfo)    End FunctionEnd Class

Dal Layer

Imports System.Data.SqlClientPublic Class DalUserInfoSql : Implements [Interface].IUserInfo    'Dim ConnStr As String = "Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"    Dim strConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")    Dim conn As SqlConnection = New SqlConnection(strConnStr)    Public Function Check(ByVal IUser As Entity.User) As Entity.User Implements [Interface].IUserInfo.Check        Dim sql As String = "select * from UserInfo where User_ID='" & IUser.User_Id + "'"        Dim cmd As SqlCommand = New SqlCommand(sql, conn)        Dim read As SqlDataReader        Try            conn.Open()            read = cmd.ExecuteReader            read.Read()            IUser.User_Id = read.Item("User_ID")            IUser.User_Pwd = read.Item("User_Pwd")            Return IUser        Catch ex As Exception            IUser.User_Pwd = ""            Return IUser        End Try    End FunctionEnd Class

Configuration File

<?xml version="1.0" encoding="utf-8" ?><configuration>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />  </startup>  <appSettings>    <add key="ConnStr" value ="Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"></add>    <add key="DBString" value ="Sql"></add>  </appSettings></configuration>

The Cognition of knowledge really has different feelings every time. The previous goal is to achieve it. In any case, as long as we achieve it, there are many places that we don't understand, now, it's much easier to look at the code. The biggest difference between Java and net platforms is the process of coding. The experience of doing DRP during this period clearly feels that net is a silly operation, and Java operations are much more annoying. Maybe I am not familiar with Java ide now.

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.