问题描述
鉴于我有一个 .NET Core 2.0 应用程序的情况我还有一个 Web 程序集、一个业务程序集和一个 DataAccess 程序集.
Given the situation that I have a .NET Core 2.0 ApplicationI also have a Web assembly, a Business assembly, and a DataAccess assembly.
我不希望 Web 程序集直接引用我的 DataAccess 程序集(其中包含我的实体框架内容).这是为了防止 Web 程序集走捷径并直接与 DbContext 对话.
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.
所以我有我的 Web 引用业务和业务引用 DataAccess.
So I have my Web referencing Business and Business referencing DataAccess.
现在出于一些奇怪的原因,在我的 Web 项目中的控制器中,我仍然可以直接访问 DbContext
,因为它在我的 DataAccess 项目中被标记为 public,显然它被赋予了由某种形式的嵌套引用构成的 Web 项目.
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.
我想一个类似的主题是这个:https://github.com/aspnet/Mvc/issues/6635 但我在 Stack Overflow 上找不到太多关于这个主题的信息.
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?
推荐答案
为了防止您的 DataAccess 层从 web 层可见,您应该将 PrivateAssets 元素放在业务层的 .csproj 文件中.就像下面的代码一样.
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>
您可以在此处阅读有关 PrivateAssets 元素的更多信息:https://docs.microsoft.com/en-us/dotnet/core/tools/csproj
Here you can read more information about PrivateAssets element: https://docs.microsoft.com/en-us/dotnet/core/tools/csproj
这篇关于防止 .NET Core 嵌套项目引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!