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

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

我正在尝试使用ASP.NET/Entity Framework Core实现基本的UoW /存储库模式,并且遇到了非常麻烦的行为。

我的解决方案由4个项目组成

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

公共类产品 { public int Id {get;组; } 公共字符串Name {get;组; } } 公共类ApplicationDbContext:DbContext {公共DbSet< Product>产品{组; } }

.Facade 项目,我的IUnitOfWork和IProductRepository定义为:

公共接口IUnitOfWork { IProductRepository产品{ } } 公共接口IProductRepository { string GetName(int id); }

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

公共类UnitOfWork:IUnitOfWork { private ApplicationDbContext _context; 个公共IProductRepository产品{私人套装; } 内部ApplicationDbContext上下文{get {return _context; }} public UnitOfWork() { _context = new ApplicationDbContext(); 个产品=新的ProductRepository(this); } } 公共类ProductRepository:IProductRepository { private ApplicationDbContext _context; public ProductRepository(UnitOfWork uow) { _context = uow.Context; } 公共字符串GetName(int id) { return _context.Products .Where(x => x.Id == id)。选择(x => x.Name) .FirstOrDefault(); } }

.DemoApp 项目,其中我的应用程序代码应该是。该项目应该只知道UnitOfWork和UserRepository,而不是ApplicationDbContext类。

  • 。DAL引用实体框架6.1.3。 / p>

  • .Facade不引用任何内容。

  • .Facade.EF引用.DAL项目,.Facade项目和实体框架6.1.3。

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

.NET Framework 4.6.x 。,如果我尝试在DemoApp中针对ApplicationDbContext进行编码,它会告诉我未定义类,并且未提供任何预期的行为给我添加任何用途。 / p>

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

.DemoApp项目未直接引用DAL项目-为什么我可以在那里查看课程吗?

这是预期的行为吗? 是否可以使.NET Core项目具有与.NET Framework 4.6.x项目相同的行为?

解决方案

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

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

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

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

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

在B.csproj

< ItemGroup> < ProjectReference Include = .. \C\C.csproj PrivateAssets = All /> < / ItemGroup>

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

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

My solution consists of 4 projects in total.

.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 project, where my IUnitOfWork and IProductRepository are defined:

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

.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 project where my application code should be. This project should only know about UnitOfWork and UserRepository and not about ApplicationDbContext class.

  • .DAL references Entity Framework 6.1.3.

  • .Facade does not reference anything.

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

  • .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.

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.

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 project is not directly referenced by .DemoApp project - why am I allowed to see classes from it there?

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?

解决方案

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

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>

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

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:

In B.csproj

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

Source: github/dotnet/project-system/issues/2313

更多推荐

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

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

发布评论

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

>www.elefans.com

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