Search code examples
visual-studiodependency-injectionnugetdependency-managementnuspec

Nuspec: can I have an assembly as a dependency but instruct visual studio to NOT reference it?


I'm developing a set of assemblies which contain classes that, using a dependency injection framework, are supposed to be instantiated only by an "InstanceProvider" class --basically that's my gateway to SimpleInjector's GetInstance(). Then I'm packaging these assemblies as a nuget package.

My goal is to enforce that a developer does

var myDuck = InstanceProvider.GetInstance<IDuck>();

and doesn't do

var myDuck = new Duck();

For this, I have to avoid referencing some of the assemblies, the ones that contain the concrete implementations. I still need them to be there though.

So for now, I have them as dependencies in my nuget package. I'm looking for a way to keep them there, but when a developer uses the package for her project, some of the assemblies should not be directly referenced in their visual studio project.

Is this even possible?


Solution

  • Nuspec: can I have an assembly as a dependency but instruct visual studio to NOT reference it?

    Since you do not want to those assemblies directly referenced in their visual studio project when you uses the package for her project, only keep them there. You can set those assemblies in the content files or tools files, like:

    <file src="\*.dll" target="content\" />
    
    <file src="\*.dll" target="Tools\" />
    

    Check Creating the .nuspec file for some more details.

    With this way, those assemblies included in the nuget package, but those assemblies would not directly referenced to the project when you use the nuget package.

    Hope this helps.