title: Dependency Injection With Generic Repository
body: <p>I am trying to do dependency injection (ASP.NET CORE) in my web api controller but I am having problems, since have a generic repository with unit of work, and also a generic service layer. The services are in a different class library. The same occurs for the repositories.</p>
  
  <p>I am displaying my code:</p>
  
  <pre><code>    public class AccountController : Controller
     {
       private IAccountService _accountService;
  
     public AccountController(IAccountService accountService)
      {
          _accountService = accountService;
      }
     }
  </code></pre>
  
  <p>service layer (base service):</p>
  
  <pre><code>  public class BaseService&lt;TEntity&gt; : IDisposable, IBaseService&lt;TEntity&gt; where TEntity : class
  {
      private readonly IBaseRepository&lt;TEntity&gt; _repository;
      private readonly IUnitOfWork _unitOfWork;
  
      public BaseService(IBaseRepository&lt;TEntity&gt; repository, IUnitOfWork unitOfWork)
      {
          _repository = repository;
          _unitOfWork = unitOfWork;
      }
  </code></pre>
  
  <p>account service:</p>
  
  <pre><code>public class AccountService : BaseService&lt;User&gt;, IAccountService
  {
      private readonly IAccountRepository _repository;
      private readonly IUnitOfWork _unitOfWork;
  
      public AccountService(IAccountRepository repository, IUnitOfWork unitOfWork) : base (repository, unitOfWork)
      {
          _repository = repository;
          _unitOfWork = unitOfWork;
      }
  </code></pre>
  
  <p>base repository:</p>
  
  <pre><code>  public class BaseRepository&lt;TEntity&gt; : IDisposable, IBaseRepository&lt;TEntity&gt; where TEntity : class
  {
      protected NeoSpaceDbContext _database;
  
      public BaseRepository(NeoSpaceDbContext database)
      {
          _database = database;
      }
  </code></pre>
  
  <p>account repository:</p>
  
  <pre><code>public class AccountRepository : BaseRepository&lt;User&gt;, IAccountRepository
  {
      public AccountRepository(NeoSpaceDbContext context) : base(context)
      {} 
  }
  </code></pre>
  
  <p>I think my problem is my startup configuration in my web api.</p>
  
  <p>I have already tried:</p>
  
  <pre><code> services.AddScoped(typeof(IBaseService&lt;User&gt;), typeof(AccountService));
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>        services.AddScoped(typeof(IBaseService&lt;&gt;), typeof(BaseService&lt;&gt;));
          services.AddSingleton&lt;IAccountService, AccountService&gt;();
  </code></pre>
  
  <p>but nothing works ...
  Can someone tell me what is the right configuration for my case?</p>