显示 类 来自 .NET Core 中间接引用的包
Showing classes from indirectly referenced packages in .NET Core
我正在尝试使用 ASP 实现基本的 UoW/Repository 模式。NET/Entity Framework Core 我遇到了非常麻烦的行为。
我的解决方案总共包含4个项目。
.DAL 项目,其中定义了我的实体 classes 以及定义了我的 DbContext:
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:
public interface IUnitOfWork
{
IProductRepository Products { get; }
}
public interface IProductRepository
{
string GetName(int id);
}
.Facade.EF项目,我的Facade是用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 class.
.DAL 引用 Entity Framework 6.1.3.
.Facade 没有引用任何东西。
.Facade.EF 引用 .DAL 项目、.Facade 项目和 Entity Framework 6.1.3.
.DemoApp 引用 Facade 和 Facade.EF 但不是 Entity Framework
6.1.3. NOR .DAL 项目。即使在构建时将 EF 程序集添加到我的 bin 文件夹中,此项目也不会直接引用 EF。
使用 .NET Framework 4.6.x,如果我尝试在 DemoApp 中针对 ApplicationDbContext 进行编码,它会告诉我 class 不是已定义且未向我提供任何要添加的用途,这是预期的行为。
如果我尝试对 .NET Core 1.0 RC2(通过使用 Entity Framework Core)执行相同的操作,则可以从 .DemoApp 访问 ApplicationDbContext,而无需添加对.DAL 项目完全破坏了我隐藏实现细节的企图。
.DAL 项目没有被 .DemoApp 项目直接引用 - 为什么我可以从那里看到 classes?
这是预期的行为吗?
有没有办法让 .NET Core 项目与 .NET Framework 4.6.x 项目具有相同的行为?
这是有意为之的行为。它被称为元包,例如用于 NETStandard.Library
包以包含基本 class 库的所有库。我认为没有办法隐藏它们。
几个月来我一直在努力解决这个问题,终于找到了一种方法来禁用 Core 中项目的传递引用。
在 .Facade.EF 的 .csproj 文件中,您可以将 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>
我正在尝试使用 ASP 实现基本的 UoW/Repository 模式。NET/Entity Framework Core 我遇到了非常麻烦的行为。
我的解决方案总共包含4个项目。
.DAL 项目,其中定义了我的实体 classes 以及定义了我的 DbContext:
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:
public interface IUnitOfWork
{
IProductRepository Products { get; }
}
public interface IProductRepository
{
string GetName(int id);
}
.Facade.EF项目,我的Facade是用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 class.
.DAL 引用 Entity Framework 6.1.3.
.Facade 没有引用任何东西。
.Facade.EF 引用 .DAL 项目、.Facade 项目和 Entity Framework 6.1.3.
.DemoApp 引用 Facade 和 Facade.EF 但不是 Entity Framework 6.1.3. NOR .DAL 项目。即使在构建时将 EF 程序集添加到我的 bin 文件夹中,此项目也不会直接引用 EF。
使用 .NET Framework 4.6.x,如果我尝试在 DemoApp 中针对 ApplicationDbContext 进行编码,它会告诉我 class 不是已定义且未向我提供任何要添加的用途,这是预期的行为。
如果我尝试对 .NET Core 1.0 RC2(通过使用 Entity Framework Core)执行相同的操作,则可以从 .DemoApp 访问 ApplicationDbContext,而无需添加对.DAL 项目完全破坏了我隐藏实现细节的企图。
.DAL 项目没有被 .DemoApp 项目直接引用 - 为什么我可以从那里看到 classes?
这是预期的行为吗? 有没有办法让 .NET Core 项目与 .NET Framework 4.6.x 项目具有相同的行为?
这是有意为之的行为。它被称为元包,例如用于 NETStandard.Library
包以包含基本 class 库的所有库。我认为没有办法隐藏它们。
几个月来我一直在努力解决这个问题,终于找到了一种方法来禁用 Core 中项目的传递引用。
在 .Facade.EF 的 .csproj 文件中,您可以将 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>