Search code examples
c#.netreferencenuget.net-8.0

Isolate Referenced Package between 2 projects c#


I have one Internal Package (DBAbs) that uses static classes to abstract database operations.

In my solution I have 2 different projects that both use a reference to (DBAbs) but connect to 2 different databases. Im using Nuget to manage the reference.

It works just fine when I only use one or the other but when I attempt to use both it messes up, it appears that they are sharing the same config and I have to set the connection string to the correct database before I can switch between them.

I drew a diagram that outlines what I thought would happen vs what I observed happening.

diagram

Is there a way to isolate DBAbs reference between the 2 DB projects or should I just convert DBAbs from using static classes to an actual reference style so that i can create two instances with different config.

I hope i provided enough info, thanks for any help.


Solution

  • Using static classes (or perhaps 'hardcoded' config access) is a generally bad idea. Thats why we invented "instances" - same code, but different data (e.g. different connection string).

    It is possible to use different versions of a packages using the Aliases="Alias1" parameter (perhaps even using the same version???). However if there is a hardcoded access to some config-file/settings - you still will read the same config. So you should have same DbContext instances that receives different config-values and will provide access to different DBs.