Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core project dependency "bleed"

Reference encapsulation in .NET Core seems to have changed in a way which allows references in one project to "bleed" into another project.

In prior versions of .NET, assemblies referenced by project "X" were not exposed to other projects in the solution which referenced project "X".

So for example, if you had a Domain project which referenced Entity Framework, adding a reference to that Domain project to another project in your solution would not grant that other project access to any Entity Framework classes.

This was a good thing (at least in my opinion).

Was playing around with a .NET Core 2.1 app tonight, and created a domain project which leveraged EF Core.

I then created a unit test project (in eager anticipation of leveraging the new EF Core InMemory provider), and referenced my domain project.

What completely caught me off guard was that the unit test project was able to access EF Core classes even though I did not bring the EF Core NuGet package into the unit test project; my only assumption is that it was able to access EF via my domain project, ie:

Example of the reference bleed

This seems highly undesirable; I don't care too much about reference bleeds in my unit tests, but I do care very much about this kind reference bleed when working with other projects in my solution (such as an ASP.NET Core Web project).

Is there a way to hide / shield these package references in my Domain project from other projects which reference it?

like image 626
RMD Avatar asked Jun 09 '18 23:06

RMD


1 Answers

This is a curious thing and i have never noticed it before,

However take a look at this

Controlling dependency assets

PrivateAssets These assets will be consumed but won't flow to the parent project

And the tag

compile Contents of the lib folder and controls whether your project can compile against the assemblies within the folder

Right click Class library -> Dependencies -> Nuget -> Package, and set the PrivateAssets to the word compile... I was seemingly able to use (and yet hide the dependency) in a calling .Net Core project.

Disclaimer, i really only played with this setting out of curiosity, and not really sure if there are any side affects to doing this

like image 183
TheGeneral Avatar answered Oct 12 '22 08:10

TheGeneral