显示 .NET Core 中间接引用的包中的类

编程入门 行业动态 更新时间:2024-10-24 01:51:24
本文介绍了显示 .NET Core 中间接引用的包中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 ASP.NET/Entity Framework Core 实现基本的 UoW/Repository 模式,但我遇到了非常令人不安的行为.

I am trying to implement basic UoW/Repository pattern with ASP.NET/Entity Framework Core and I have encountered very troubling behavior.

我的解决方案总共包含 4 个项目.

My solution consists of 4 projects in total.

.DAL 项目,其中定义了我的实体类并定义了我的 DbContext:

.DAL project, where my entity classes are defined and where my DbContext is defined:

public class Product { public int Id { get; set; } public string Name { get; set; } } public class ApplicationDbContext : DbContext { public DbSet<Product> Products { get; set; } }

.Facade 项目,其中定义了我的 IUnitOfWork 和 IProductRepository:

.Facade project, where my IUnitOfWork and IProductRepository are defined:

public interface IUnitOfWork { IProductRepository Products { get; } } public interface IProductRepository { string GetName(int id); }

.Facade.EF 项目,其中我的 Facade 是用 EF 实现的:

.Facade.EF project, where my Facade is implemented with EF:

public class UnitOfWork : IUnitOfWork { private ApplicationDbContext _context; public IProductRepository Products { get; private set; } internal ApplicationDbContext Context { get { return _context; } } public UnitOfWork() { _context = new ApplicationDbContext(); Products = new ProductRepository(this); } } public class ProductRepository : IProductRepository { private ApplicationDbContext _context; public ProductRepository(UnitOfWork uow) { _context = uow.Context; } public string GetName(int id) { return _context.Products .Where(x => x.Id == id) .Select(x => x.Name) .FirstOrDefault(); } }

.DemoApp 项目,我的应用程序代码应该放在那里.这个项目应该只知道 UnitOfWork 和 UserRepository 而不是 ApplicationDbContext 类.

.DemoApp project where my application code should be. This project should only know about UnitOfWork and UserRepository and not about ApplicationDbContext class.

  • .DAL 引用实体框架 6.1.3.

  • .DAL references Entity Framework 6.1.3.

.Facade 未引用任何内容.

.Facade does not reference anything.

.Facade.EF 引用 .DAL 项目、.Facade 项目和实体框架 6.1.3.

.Facade.EF references .DAL project, .Facade project and Entity Framework 6.1.3.

.DemoApp 引用 Facade 和 Facade.EF 但不是实体框架6.1.3.NOR .DAL 项目.即使在构建时将 EF 程序集添加到我的 bin 文件夹中,该项目也不会直接引用 EF.

.DemoApp references Facade and Facade.EF but NOT Entity Framework 6.1.3. NOR .DAL project. Even though EF assemblies are added to my bin folder on build, EF is not directly referenced by this project.

使用 .NET Framework 4.6.x.,如果我尝试针对 DemoApp 中的 ApplicationDbContext 进行编码,它会告诉我该类未定义,并且没有为我提供任何要添加的使用预期行为.

With .NET Framework 4.6.x., if I try try to code against ApplicationDbContext in DemoApp, it tells me that class is not defined and is not providing me any usings to be added which is expected behavior.

如果我尝试对 .NET Core 1.0 RC2 做同样的事情(通过使用 Entity Framework Core),ApplicationDbContext 可以从 .DemoApp 访问,而无需添加对 .DAL 项目的直接引用,这完全破坏了我的尝试隐藏实现细节.

If I attempt to do the same with .NET Core 1.0 RC2 (by using Entity Framework Core), ApplicationDbContext is accessible from .DemoApp without adding direct reference to .DAL project which completely destroys my attempt to hide implementation details.

.DAL 项目没有被 .DemoApp 项目直接引用 - 为什么我可以在那里看到它的类?

.DAL project is not directly referenced by .DemoApp project - why am I allowed to see classes from it there?

这是预期的行为吗?有没有办法让 .NET Core 项目与 .NET Framework 4.6.x 项目具有相同的行为?

Is this expected behavior? Is there a way to make .NET Core projects to have the same behavior as .NET Framework 4.6.x projects?

推荐答案

我已经为此苦苦挣扎了几个月,终于找到了一种方法来禁用 Core 中项目的传递引用.

I've been struggling with this for months and finally found a way to disable the transitive references of projects in Core.

在 .Facade.EF 的 .csproj 文件中,您可以将 PrivateAssets="All" 添加到 .DAL 的 ProjectReference:

In the .csproj file for .Facade.EF, you can add PrivateAssets="All" to the ProjectReference to .DAL:

<ItemGroup> <ProjectReference Include="...DAL.DAL.csproj" PrivateAssets="All" /> </ItemGroup>

使用此设置,引用 .Facade.EF 的项目也不再引用 .DAL.

With this setting, projects that reference .Facade.EF no longer reference .DAL too.

用更抽象的术语来说,如果您希望 A 引用 B,B 引用 C,但不想 A 引用 C,请添加:

In more abstract terms, if you want A to reference B and B to reference C, but don't want A to reference C, add this:

在 B.csproj 中

<ItemGroup> <ProjectReference Include="..CC.csproj" PrivateAssets="All" /> </ItemGroup>

来源:github/dotnet/project-system/issues/2313

更多推荐

显示 .NET Core 中间接引用的包中的类

本文发布于:2023-11-04 01:04:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:包中   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!