c# - Creating a Web Application which supports multiple Backend -


i need create web based database application in asp.net , c# work across database providers sql server, mysql etc. if client has sql server small change in program should make web application work sql server, if client has mysql should work mysql.

is there tools available ? or should want follow programming procedures or methods achieve it?

so guide me achieve above scenario.

to maintain independence of external systems (such database), you'll want architect solution speak interface. can use several techniques, including dependency injection, decide implementation use @ runtime.

to keep simple here, though, let's use factory.

first thing decide database should do. simple example - we'll store or retrieve integer. first let's define interface. i'm leaving out async stuff simplicity - you'd want when talking db.

public interface idatabase{     void store(int value);     int get(); } 

so we've got our calling code can use refer type of database.

next we'll need different types of classes each db

public class sqlserverdb : idatabase{     public void store(int value){//store it}     public void get(){//get it} }  public class mysqldb : idatabase{     public void store(int value){//store it}     public void get(){//get it} } 

again, don't want calling code know implementation, can't instantiate object new. instead, you'll need construct class knows different types of databases.

i'm using string here, should use enum in real code.

public static class databasefactory{      public static idatabase getdatabase(string dbname){          switch(dbname){              case "sqlserver":                 return new sqlserverdb();              case "mysql":                 return new mysqldb();              default:                  throw new argumentexception("don't know db");          }      } } 

now calling code doesn't have change dbs. if you've got config object (like built in .net ones) can change database being used easily.

idatabase mydb = databasefactory.getdatabase(config.dbname); mydb.store(191);  int result = mydb.get(); //result equal 191! 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -