Reconstruction of abstract factory and reconstruction of factory
We are familiar with the abstract factory. We have seen it when learning the design model. I also saw it when I started refactoring. However, I have to get a glimpse of it on the paper. I am sure I want to do it! Now let's talk about abstract factories.
I. Configuration File
Let's just talk about the definition. Let's take a look at where it is? What is in it. That is, App. config stored in the UI Layer
Next, let's take a look at the content in it.
The two lines marked in the figure are no stranger. The first line is to connect to the database, and the second line is to define SqlServer as a DB. The reason for layer-3 decoupling is to use abstract factories to facilitate database connection. We should use sqlserver when restructuring the data center, but what if we want to use access now? Does it need to be changed in every form? No, just define access as a DB in the configuration file.
Ii. Abstract Factory
Based on the figure above, we can know the location of the abstract factory, connect layer B and interfaces, and implement interfaces at Layer D, we will introduce the abstract factory and precautions Based on the recharge record query of normal users.
<Span style = "font-size: 18px;"> Imports System. reflectionImports System. configurationImports IDALPublic Class RechargeFactory Private Shared ReadOnly AssemlyName As String = "DAL" 'defines the Assembly name variable D-layer namespace name Private Shared db As String = ConfigurationManager. appSettings ("DB ") private Property AssemblyName As String = "DAL" 'indicates reading the configuration file. If the configuration file contains sqlserver, access the sqlserver database ''' <summary> ''' the abstract factory in the recharge window'' '</summary> ''' <returns> </returns> ''' <remarks> </remarks> Public Function checkCfactory () as IRecharge 'create the factory Dim ClassName As String = AssemblyName + ". "+ db +" RechargeDAL "''' AssemblyName is the Assembly name, and db +" UserDAL "is the sqlServerUserDAL In the DAL layer. If SQLserver database is not used, then we construct a class. For example, if we access the access database, the class name is accessUserDal, change the Value in the configuration file 'access. This is an extension instead of modifying Dim irecharge As IRecharge 'instance' to understand Assembly. load (AssemblyName ). createInstance (ClassName) irecharge = CType (Assembly. load (AssemblyName ). createInstance (ClassName), IRecharge) 'converts the instantiated layer D to an interface class through an upward transformation, then, call the function in the interface Class to call the function Return irecharge End FunctionEnd Class that implements the interface in layer D. </span>
Please read this line
<span style="font-size:18px;"> Dim ClassName As String = AssemblyName + "." + db + "RechargeDAL"</span>
Here, the DB is the SqlServer defined in the configuration file. RechargeDAL is the name of the connection D layer, that is, the complete name of the D layer is SqlServerRechargeDAL.
At the beginning, we still need the following practices to deepen our understanding of the code ....