Unity - 当前类型是一个接口,无法构造。通用存储库DI

时间:2016-12-13 16:59:07

标签: .net generics unity-container unit-of-work

我有通用存储库:

public interface IRepository<T> where T : class
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    void Delete(Expression<Func<T, bool>> where);
    T GetById(long id);
    T GetById(string id);
    T Get(Expression<Func<T, bool>> where);
    IEnumerable<T> GetAll();
    IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
}

并实现了课程:

public abstract class BaseRepository<T> where T : class 
{
    private ApplicationDbContext _context;
    private readonly IDbSet<T> dbset;

    protected BaseRepository(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = _context.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get;
        private set;
    }

    protected ApplicationDbContext DataContext
    {
        get { return _context ?? (_context = DatabaseFactory.Get()); }
    }

    //methods below..
}

我有 UserRepository ,界面为:

public interface IUserRepository : IRepository<UserRepository>
{

}

public class UserRepository : BaseRepository<UserRepository>, IUserRepository
{
    public UserRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
    {

    }
}    

我正在使用BaseController在从Base扩展的任何控制器中获取我的Identity currentUser:

BaseController

public class BaseController : Controller
{
    protected readonly IUserRepository userRepository;
    public UserAccountModel currentUser { get; private set; }

    public BaseController(IUserRepository UserRepository)
    {
        this.userRepository = UserRepository;
    }

    //Initialize
}

HomeController

public class HomeController : BaseController
{
    protected readonly IUserRepository userRepository;

    public HomeController(IUserRepository UserRepository) : base(UserRepository)
    {
        this.userRepository = UserRepository;
    }

    //actions
}

我正在使用Unity IoC,我的UnityConfig.cs看起来像这样:

private static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IUnitOfWork, UnitOfWork>();
    container.RegisterType<IDatabaseFactory, DatabaseFactory>();
    container.RegisterType<ApplicationDbContext>();
    container.RegisterType<ApplicationSignInManager>();
    container.RegisterType<ApplicationUserManager>();

    container.RegisterType<IAuthenticationManager>(
            new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));

    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(
        new InjectionConstructor(typeof(ApplicationDbContext)));

    container.RegisterType(typeof(IRepository<>), typeof(BaseRepository<UserRepository>));
}

在运行中,我收到错误:

  

当前类型ProjectName.Data.Repository.IUserRepository是一个接口,无法构造。你错过了类型映射吗?

我认为我的问题是错误的类型注册,但我已经把它弄错了......

2 个答案:

答案 0 :(得分:1)

在我看来,您需要IUserRepositoryUserRepository之间的映射。 e.g:

container.RegisterType<IUserRepository, UserRepository>();

答案 1 :(得分:0)

IUserRepository需要HomeController所以它应该在Ioc中注册。在解决HomeController期间,Ioc将尝试解决Ioc中不存在的IUserRepository。尝试改变:

container.RegisterType(typeof(IRepository<>), typeof(BaseRepository<UserRepository>));

为:

container.RegisterType<IUserRepository, UserRepository>()