How the PetShop is compatible with the database

Source: Internet
Author: User
Data | The porting of database databases often brings high costs. I have a deep understanding of this. The size of the cost depends on how the program's architecture is written. Last year, a project was moved from MySQL to Oracle, and the entire program was tinkering in and out, and it took about two months.

If you do less modification, and even do not modify the code, the compatibility of the database is undoubtedly a very good thing,

PetShop did it very well.

To be compatible with multiple databases, you first need to implement polymorphism. Sqlserverdal and Oracledal both implement the method of all interfaces in Idal and realize polymorphism.

Factorydal is used to create a Dal object,

public static PetShop.IDAL.IAccount Create ()

{

Look up the DAL implementation we should to be using

String path = system.configuration.configurationsettings.appsettings["Webdal"];

String className = Path + ". Account ";

Using the evidence given in the config file load the appropriate assembly and class

Return (PetShop.IDAL.IAccount) assembly.load (path). CreateInstance (ClassName);

}

As above: Create account, first get the value of Webdal in Web.config.

<add key= "Webdal" value= "Petshop.sqlserverdal"/>

The value of "Webdal" in Web.config is Petshop.sqlserverdal, which determines the path of the DAL to be created.

The DAL instance is then created with Assembly.createinstance ().



To use an Oracle database, just

<add key= "Webdal" value= "Petshop.sqlserverdal"/> and

<add key= "Ordersdal" value= "Petshop.sqlserverdal"/>

In the petshop.sqlserverdal change to petshop.oracledal on it.



And in BLL, it doesn't need to know that you use that database. The object is only obtained by using the following statement.

Get a instance of the account DAL using the Dalfactory

IAccount dal = PetShop.DALFactory.Account.Create ();

No matter what database you use, the BLL module does not need to be modified.





Extensibility: What to do if you want to be compatible with MySQL database?

Write a mysqldal module to implement the method of all interfaces in the Idal. Then modify the value in the web.config.



PetShop provides good scalability and closes the business layer's modifications. Well satisfied with the OCP (open-closed principle).





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.