I made a library to deal with persistence using the Entity Framework Core. It is used by a Web API targeting a SQL Server database. Furthermore, I test it (unit tests) targeting a SQLite database.
I accomplished it in a straightforward way with a configuration file. So, in my class that extends the DBContext, I implemented OnConfiguring with something like this:
if(isSqlServer) //don't mind where it comes from, it works fine
optionsBuilder.UseSqlServer(connectionString);
else
optionsBuilder.UseSqlite(connectionString);
It seems to work fine, but it has an undesired effect. Supposed I have this projects:
- PersistenceLayer
- PersistenceLayerTests
- WebAPI
Since the method UseSqlite is a static extension method in a class of the package Microsoft.EntityFrameworkCore.Sqlite and, similarly, the method UseSqlServer is a static extension method in a class of the package Microsoft.EntityFrameworkCore.SqlServer, two things bother me.
First, I have to include both dependencies in the three projects to avoid the error "System.IO.FileNotFoundException: Could not load file or assembly Microsoft.EntityFrameworkCore.SqlServer (...)" at runtime.
Second, if I need to support a new DBMS (Oracle, MySQL), I have to include new dependencies on the three projects.
I would expect to build a scenario where the project PersistentLayer would not have those dependencies, while PersistentLayerTests would depend solely on Microsoft.EntityFrameworkCore.Sqlite and WebAPI would depend solely on Microsoft.EntityFrameworkCore.SqlServer.
Is there another way to configure the connection context to decouple these packages dependencies?
Notes:
- I did that with NHibernate, that uses different drivers implementations for each DBMS.
- I tried using reflection, but wasn't elegant and I noticed it would not work if an extension method have different parameters.
- I don't need a full solution. If someone can point me another way, it will be very helpful.