How PETSHOP is compatible with different databases

xiaoxiao2021-03-06  47

Database transplants usually bring high cost. I have a deep understanding this. The size of the price is to see how the program is written. Last year, a project moved from MySQL to Oracle. The whole program has been repaired in the whole program, which is about two months.

If you do less modifications, even if you don't modify the code, it is undoubtedly a very good thing.

PetShop is very good to do this

To be compatible with multiple databases, you must first implement a polymorphism. Both SQLSerVerdal and Oracles implement all interfaces of the IDAL to achieve polymorphism.

Factorydal is used to create DAL objects,

Public static petshop.idal.iaccount crete ()

{

/// Look Up The Dal Implementation We Should Be Using IMPLEMENTATION WEHOULD BE

String path = system.configuration.configurationSettings.AppSettings ["Webdal"];

String classname = PATH ".account";

// using the eviden in the config file loading 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.

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

Then create a DAL instance with Assembly.createInstance ().

To use an Oracle database, just put

Petshop.sqlserdal is changed to PETSHOP.ORACLEDAL.

In BLL, it doesn't need to know that you use that database. Just get an object by the following statement.

// Get An Instance of the Account Dal Using The Dalfactory

Iaccount Dal = PETSHOP.DALFACTORY.ACCOUNT.CREATE ();

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

Scalability: What should I do if I want to be compatible with mySQL database?

Write a mysqldal module to implement all interfaces in the IDAL. Modify the value in web.config.

PETSHOP provides good scalability, enclosing the changes in the business layer. Very good to meet OCP (open-closed principles).

转载请注明原文地址:https://www.9cbs.com/read-109615.html

New Post(0)