2

I have a simple console application with a get,update,add and delete method. I'm currently using a SQL Server in the background.

Now I want to improve this with adding a config file which lets you choose the type of the database e.g. MySql or Oracle or Msql.

My idea is to create different Server layers and a DB Interface which lets me access the Server layer. My main class would access the DB layer through dependecy injection.

Thats the general idea right now however I dont have a clue how to do this.

Are there other ways or does anyone know how to do this?

Ekos
  • 525
  • 2
  • 6
  • 15
  • So, you want help with reading a file that defines the type of the database. Is that right? – Juan Ferrer Jun 26 '17 at 12:03
  • Yeah thats the first step I have to take. Creating a file which defines the database and reading it – Ekos Jun 26 '17 at 12:14
  • Just a matter of adding and utilizing different connection strings. How you implement is up to you. – Mad Myche Jun 26 '17 at 12:16
  • [This contains many good reads on DI](https://stackoverflow.com/questions/130794/what-is-dependency-injection) - To find the methods of a good Interface look at your code so far: anything it does to talk to the DBMS is a candidate: Connect, Disconnect, Execute, Select, Insert, Update, Delete, BeginTransaction, Commit, Rollback. - Then create one class inheriting from the interface for each DBMS you want to support and change all your code to talk to those classes as interfaces. pick which you instantiate at startup or whenever you read the config file or want to switch DBMS.. – TaW Jun 26 '17 at 16:09

2 Answers2

1

in this case you should use dependency injection; create an interface with your four operations (add,delete,update,read) and create 3 classes one for each database type you have; implement this interface in each class, and at run time you can easily inject an object of your desired database provider

TaW
  • 48,779
  • 8
  • 56
  • 89
Mostafa ELite
  • 537
  • 2
  • 9
  • Yeah this was my idea aswell. Dont really know how to do this though since I'm still a beginner – Ekos Jun 26 '17 at 12:56
0

You need to write something like this right before you decide what code to execute.

string dbType = File.ReadAllText(configFilePath).ToLowerInvariant();
switch (dbType)
    {
        case "mysql":
            // Run mySQL code
            break;
        case "oracle":
            // Run Oracle code
            break;

        // And so on...
    }
Juan Ferrer
  • 1,031
  • 1
  • 16
  • 25