我真的想将SharpRepository与Ninject一起使用,但我不明白如何配置Ninject以在存储库之间共享Entity Framework DbContext.
我正在使用Entity Framework版本5和Ninject版本3.
目前我Ef5Repository在我的源代码中使用,但我想用它替换它ConfigurationBasedRepository.但我无法弄清楚如何将EF传递(或注入)DbContext到存储库.
示例(当前状态):
using SharpRepository.Repository;
public interface IProductRepository : IRepository<Product>
{
}
using SharpRepository.Ef5Repository;
using System.Data.Entity;
// TODO Tightly coupled to Ef5Repository.
public class ProductRepository : Ef5Repository<Product>, IProductRepository
{
// TODO The DbContext has to be injected manually.
public ProductRepository(DbContext context) : base(context)
{
}
// [...]
}
Run Code Online (Sandbox Code Playgroud)
目标:
using SharpRepository.Repository;
public interface IProductRepository : IRepository<Product>
{
}
public class ProductRepository : ConfigurationBasedRepository<Product, int>, IProductRepository
{ …Run Code Online (Sandbox Code Playgroud)