如何从父项目中隐藏类库

发布于 2025-01-16 13:10:33 字数 522 浏览 2 评论 0原文

我们有一个带有 .NET Core C# API 项目的解决方案。 API 项目依赖于类库。类库具有 API 项目不需要了解的依赖项。为了更容易可视化,我正在讨论以下内容:

在此处输入图像描述

问题:如何从 API 项目中隐藏一些类库,同时仍使它们可供使用类库直接调用来自 API 项目?目标是确保 API 项目不会直接调用它使用的类库的子依赖项。

失败的方法:

  • 使用“内部” - 不起作用 - “父”类库和依赖类库都是不同的程序集,即父类库不能使用内部类库。
  • 使用“受保护” - 不起作用,任何类继承最多都被硬塞在这里
  • 对类库及其依赖项使用完全不同的解决方案,将其导入为 NuGet 包 - 对于积极工作的代码库来说工作量太大

We have a Solution with a .NET Core C# API project. The API project relies on a Class Library. The Class Library has dependencies that the API project needs to know nothing about. To make it easier to visualize, here is what I am talking about:

enter image description here

Question: How do I hide some class libraries from the API project, WHILE still making them available to the Class Library called directly from the API project? The goal is to make sure the API project doesn't directly call sub-dependencies of the Class Library it uses.

Failed approaches:

  • Use "internal" - doesn't work - the "parent" and dependent class libraries are all different assemblies, i.e. the parent Class Library can't use dependent Class Libraries if they are internal.
  • Use "protected" - doesn't work, any class inheritance is shoehorned in here at best
  • Use an entirely different solution for the Class Library and its dependencies, import it as NuGet package - too much work for an actively worked-on codebase

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赴月观长安 2025-01-23 13:10:33

我们可以通过修改 *.csproj 文件中默认的 ProjectReference 部分来隐藏继承的类库的访问。

假设解决方案结构如下所示:

API
 |--> ClassLibrary
            |------->SubClassLibrary

要隐藏 API 项目中的 SubClassLibray 程序集,我们需要修改 ClassLibrary.csproj文件如下:

<ItemGroup>
    <ProjectReference Include="..\SubClassLibrary.csproj" >
        <ExcludeAssets>compile</ExcludeAssets>
    </ProjectReference>
</ItemGroup>

We can hide the inherited class libraries' access by modifying the default ProjectReference section in the *.csproj file.

Let's say the solution structure looks like this:

API
 |--> ClassLibrary
            |------->SubClassLibrary

To hide the SubClassLibray's assemblies in the API project, we need to modify the ClassLibrary.csproj file as below:

<ItemGroup>
    <ProjectReference Include="..\SubClassLibrary.csproj" >
        <ExcludeAssets>compile</ExcludeAssets>
    </ProjectReference>
</ItemGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文