How to replace databases using abstract factories

Source: Internet
Author: User

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.

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.