In VS, we have all added the abstract factory model. We all know that this mode can be used to replace databases. We all know the specific application reflection. First of all, let me introduce my situation.
The current situation is that my D-layer assembly and namespace are all DAL, And the classes in the D-layer are also named with the SQL prefix, such as SqlCancelCardDAL, in the factory, when I write this code, you can take a look:
Imports dfactoryimports idalimports system. reflectionimports system. configurationpublic class dataaccess private readonly assemblyname as string = "Dal" dim strdb as string = system. configuration. configurationsettings. appsettings ("DB") 'returns an istudent interface public function checkstuid () as istudent dim classname as string = assemblyname + ". "+ strdb +" stuinfodal "Return ctype (assembly. load ("Dal "). createinstance (classname), istudent) end function 'returns the registration interface iregist public function aboutregist () as iregist dim classname as string = assemblyname + ". "+ strdb +" registdal "Return ctype (assembly. load ("Dal "). createinstance (classname), iregist) end function 'returns the recharge interface icharge public function insertinfo () as icharge dim classname as string = assemblyname + ". "+ strdb +" chargedal "Return ctype (assembly. load ("Dal "). createinstance (classname), icharge) end function ...... end Class
Reflection in APP. config is:
<Add key = "DB" value = "SQL"/> changed to <add key = "DB" value = "oracle"/>
Next, we thought that our original namespace is totally different from the changed oracledao namespace. Let's take a look at the original Factory Code:
Private ReadOnly assemblyName As String = "DAL"
In this way, the namespace can be written to the program, but we can also use the reflection principle to move the namespace to XML, without opening, because our operation starts from the UI-layer debug in the project file, we only need to go to UI \ bin \ debug \ ui.exe. add a sentence to config:
<add key="MM" value="OracleDAO"/>
At the same time, in the factory layer, Replace the sentence "Dead namespace":
Private ReadOnly assemblyName = System.Configuration.ConfigurationSettings.AppSettings("MM")
The last step is to change "Dal" in the return sentence of each method to assemblyname, Which is perfect.
In this way, we only need to change the prefix of the class in the namespace and reflection in the configuration file of how our program changes the database, so that the abstract factory + reflection can be fully used to implement the function of changing the database.