显示 .NET Core 中间接引用的包中的类 [英] Showing classes from indirectly referenced packages in .NET Core

查看:25
本文介绍了显示 .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>

来源:https://github.com/dotnet/project-system/issues/2313

这篇关于显示 .NET Core 中间接引用的包中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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