防止.NET Core嵌套项目引用 [英] Preventing .NET Core Nested Project References

查看:166
本文介绍了防止.NET Core嵌套项目引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我有一个.NET Core 2.0应用程序
的情况,我也有一个Web程序集,一个Business程序集和一个DataAccess程序集。

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.

我不希望Web程序集直接引用我的DataAccess程序集(其中包含我的Entity Framework内容)。这样可以保护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引用了Business,而Business引用了DataAccess。

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

现在由于某种奇怪的原因,在我的Web项目的Controller中,我仍然可以直接访问 DbContext ,因为在我的DataAccess项目,并且显然是通过某种形式的嵌套引用将其提供给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 但我找不到太多

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元素放在.cspro中业务层的j文件。就像下面的代码一样。

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.Data\GiveAway.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嵌套项目引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆