Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

visual studio - Preventing .NET Core Nested Project References

Given the situation that I have a .NET Core 2.0 Application I also have a Web assembly, a Business assembly, and a DataAccess assembly.

I don't want the Web assembly to directly reference my DataAccess assembly (Which has my Entity Framework stuff inside of it). This to protect the Web assembly from taking shortcuts and talking to the DbContext directly.

So I have my Web referencing Business and Business referencing DataAccess.

Now for some odd reason, in the Controllers in my Web project, I can still directly access the DbContext, because this is marked as public in my DataAccess project, and apparently it is given to the Web project by some form of nested references.

I suppose a similar topic is this one: https://github.com/aspnet/Mvc/issues/6635 But I couldn't find much on the subject here on Stack Overflow.

Is there any elegant way to prevent these nested dependencies to be accessed by the top level project?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to prevent your DataAccess layer to be visible from a web layer, you should put PrivateAssets element inside a .csproj file of your business layer. Like in a code below.

<ItemGroup>
 <ProjectReference Include="..GiveAway.DataGiveAway.Data.csproj">
   <PrivateAssets>all</PrivateAssets>
 </ProjectReference>
</ItemGroup> 

Here you can read more information about PrivateAssets element: https://docs.microsoft.com/en-us/dotnet/core/tools/csproj


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...