I am struggling with how to organize external references in a C# program in order to minimize code repetition.
I have application "A1" for which I want to use Microsoft's Unity IoC/Dependency Injection system. So I use NuGet to pull down the Unity package and install it in A1. This pulls down 2 projects and 4 DLLs into A1's project.
To facilitate using Unity, I encapsulate the container object in a Singleton pattern (S1). So I have a constant/consistent home for the container and no matter where in A1 I have easy access it.
But I want to use Unity in a different and independent application A2 as well. This other application is not in the same solution as A1. Thus it would make sense to move my S1 code to a third solution which builds an external DLL (D1) that can be referenced by both A1 and A2. In this way I am not continually writing the S1 code in each application. I have achieved code re-use!
However in order to do so I now need to use NuGet to insert references to Unity into each of the A1, A2 and D1 projects/solutions. And because of that, the versions of Unity in the application and DLL are totally independent. I am starting to feel like the epitome of the old Regex joke:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
I can't see a clean way to organize all of this. The only options I see are:
Manually duplicate the S1 code in every application (and leave it out of the DLL), and use NuGet to enable Unity into just those applications.
Move my S1 code to the DLL, but have NuGet reference totally independent versions of Unity in the applications A1 and A2 and the DLL D1 solutions.
Move my S1 code to the D1 DLL and have NuGet reference Unity only in that solution. Then in every application add not only my custom D1 DLL from the DLL solution, but also manually add the 4 Unity DLL's that were added to my D1 solution.
All 3 of these options have some sort of code smell. I am currently leaning to option 1 as being the least worst option of those 3. Is there a 4th option that provides the flexibility I want but eliminates these code smells?
I know that the singleton code is only a very small file, but it is the principle of this dilemma that is killing me. Also my D1 project has a whole bunch of other common code and not just the S1 code.