title: Unity - The current type is an interface and cannot be constructed. Generic repository DI
body: <p>I have generic repository:</p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      void Add(T entity);
      void Update(T entity);
      void Delete(T entity);
      void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; where);
      T GetById(long id);
      T GetById(string id);
      T Get(Expression&lt;Func&lt;T, bool&gt;&gt; where);
      IEnumerable&lt;T&gt; GetAll();
      IEnumerable&lt;T&gt; GetMany(Expression&lt;Func&lt;T, bool&gt;&gt; where);
  }
  </code></pre>
  
  <p>And realized class: </p>
  
  <pre><code>public abstract class BaseRepository&lt;T&gt; where T : class 
  {
      private ApplicationDbContext _context;
      private readonly IDbSet&lt;T&gt; dbset;
  
      protected BaseRepository(IDatabaseFactory databaseFactory)
      {
          DatabaseFactory = databaseFactory;
          dbset = _context.Set&lt;T&gt;();
      }
  
      protected IDatabaseFactory DatabaseFactory
      {
          get;
          private set;
      }
  
      protected ApplicationDbContext DataContext
      {
          get { return _context ?? (_context = DatabaseFactory.Get()); }
      }
  
      //methods below..
  }
  </code></pre>
  
  <p>And I have <strong>UserRepository</strong> with interface:</p>
  
  <pre><code>public interface IUserRepository : IRepository&lt;UserRepository&gt;
  {
  
  }
  
  public class UserRepository : BaseRepository&lt;UserRepository&gt;, IUserRepository
  {
      public UserRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
      {
  
      }
  }    
  </code></pre>
  
  <p>And I'm using BaseController to get my Identity currentUser in any controller extended from Base:</p>
  
  <p><strong>BaseController</strong> : </p>
  
  <pre><code>public class BaseController : Controller
  {
      protected readonly IUserRepository userRepository;
      public UserAccountModel currentUser { get; private set; }
  
      public BaseController(IUserRepository UserRepository)
      {
          this.userRepository = UserRepository;
      }
  
      //Initialize
  }
  </code></pre>
  
  <p>And <strong>HomeController</strong> :</p>
  
  <pre><code>public class HomeController : BaseController
  {
      protected readonly IUserRepository userRepository;
  
      public HomeController(IUserRepository UserRepository) : base(UserRepository)
      {
          this.userRepository = UserRepository;
      }
  
      //actions
  }
  </code></pre>
  
  <p>I'm using Unity IoC, and my UnityConfig.cs looks like this:</p>
  
  <pre><code>private static void RegisterTypes(IUnityContainer container)
  {
      container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();
      container.RegisterType&lt;IDatabaseFactory, DatabaseFactory&gt;();
      container.RegisterType&lt;ApplicationDbContext&gt;();
      container.RegisterType&lt;ApplicationSignInManager&gt;();
      container.RegisterType&lt;ApplicationUserManager&gt;();
  
      container.RegisterType&lt;IAuthenticationManager&gt;(
              new InjectionFactory(c =&gt; HttpContext.Current.GetOwinContext().Authentication));
  
      container.RegisterType&lt;IUserStore&lt;ApplicationUser&gt;, UserStore&lt;ApplicationUser&gt;&gt;(
          new InjectionConstructor(typeof(ApplicationDbContext)));
  
      container.RegisterType(typeof(IRepository&lt;&gt;), typeof(BaseRepository&lt;UserRepository&gt;));
  }
  </code></pre>
  
  <p>On run, I'm getting error:</p>
  
  <blockquote>
    <p>The current type, ProjectName.Data.Repository.IUserRepository, is an interface and cannot be constructed. Are you missing a type mapping?</p>
  </blockquote>
  
  <p>I think my problem is wrong type registration, but I'm already broke my head on it...</p>