jeudi 16 novembre 2017

Loading different Libraries with same Functions dynamically

I have a program which has a reference to an external DLL:

magic.externalFunctions.sql.dll

depending on the target system I might need the following instead:

magic.externalFunctions.nosql.dll

These DLLs are external libraries who offer the same functionality. Even the public methods and properties are the same. But they do not have a combined Interface or base class.

Now I need to chose between these two libraries depending on the target system. If they would have an Interface in common I would do something like:

public class DoDatabaseStuff() {
    private IMagic _magic
    public DoDatabaseStuff(bool useNoSql) {
       if (useNoSql) {
         _magic=new SqlMagic();
       } else {
         _magic=new NoSqlMagic();
       }
       myTable=_magic.Table.Create("CustomerTable");
       myTable.Columns.Add(typeof(int),"ID");
       myTable.Columns.Add(typeof(string),"Name");
   }
}

Is there any way to solve this without using an excessive amount of reflection? I still want to use something like myTable.Columns.Add() instead of tableType.GetMethod("Add").Invoke()...

I also do not want to use conditions in the csproj using different build configurations. While this does work in theory, it gets distroyed every time the csproj changes automatically(e.g. a nuget-package updates)





Aucun commentaire:

Enregistrer un commentaire