title: How to use dependency injection with inheritance in C#
body: <h1>Introduction</h1>
  <p>Hi everyone, I'm currently working on a persistence library in C#. In that library, I have implemented the repository pattern where I'm facing a SOLID issue.
  Here is a simplified example my current implementation to focus on the essential:</p>
  <p><strong>The abstract repository containing in the persistence library:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public abstract class Repository&lt;T&gt; 
  {
      protected Repository(
          IServiceA serviceA,
          IServiceB serviceB) 
      {
          /* ... */
      }
  }
  </code></pre>
  <p><strong>The concrete repository created by the library user:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public class FooRepository : Repository&lt;Foo&gt; 
  {
      protected FooRepository(
          IServiceA serviceA,
          IServiceB serviceB) :
          base(serviceA, serviceB)
      {
          /* ... */
      }
  }
  </code></pre>
  <h1>Problem</h1>
  <p>OK, with the current code, the derived class has to know every dependency of the base class which can be ok, but what if I add a dependency to the base class? Every derived class will break because they will need to pass that new dependency to the base class... So currently, <strong>I'm limited to never change the base class constructor</strong> and it's a problem because I want my base class to had the possibility to evolve. <strong>This implementation clearly breaks the Open/Closed Principle</strong>, but I don't know how to solve this issue without breaking the SOLID...</p>
  <h1>Requirements</h1>
  <ul>
  <li>The library should be easy to use for the user</li>
  <li>The concrete repositories should be able to be constructed through the DI</li>
  <li>One or more dependencies should be added to the abstract repository without impacting the derived repositories</li>
  <li>It should be possible to register every repository in the DI container using a naming convention as does the <em>ASP.NET MVC framework</em> with the controllers</li>
  <li>The user should be able to add more dependencies in his derived repository if he wants</li>
  </ul>
  <h1>Solutions already envisaged</h1>
  <h3>1. Service aggregator pattern</h3>
  <p>Following this <a href="http://peterspattern.com/dependency-injection-and-class-inheritance/" rel="noreferrer">article</a>, the service aggregator model can be applied in this situation so the code would look like something like this:</p>
  <p><strong>The abstract repository containing in the persistence library:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public abstract class Repository&lt;T&gt; 
  {
  
      public interface IRepositoryDependencies
      {
          IServiceA { get; }
          IServiceB { get; }
      }
  
      protected Repository(IRepositoryDependencies dependencies) 
      {
          /* ... */
      }
  }
  </code></pre>
  <p><strong>The concrete repository created by the library user:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public class FooRepository : Repository&lt;Foo&gt; 
  {
      protected Repository(IRepositoryDependencies dependencies) :
          base(dependencies)
      {
          /* ... */
      }
  }
  </code></pre>
  <p><strong>Pros</strong></p>
  <ul>
  <li>The derived classes don't break if a dependency is added to the base class</li>
  </ul>
  <p><strong>Cons</strong></p>
  <ul>
  <li>The implementation of the <code>IRepositoryDependencies</code> interface has to be modified if we add a dependency</li>
  <li>The <a href="http://peterspattern.com/dependency-injection-and-class-inheritance/" rel="noreferrer">article</a> doesn't explain how to use <em>Castle DynamicProxy2</em> (which is an unknown technology for me) to dynamically generate the service aggregator</li>
  </ul>
  <h3>2. Builder pattern</h3>
  <p>Perhaps, it's possible to remove the base repository constructor and introduce a builder template to create the repositories, but for this solution to work, the builder must be inheritable to allow the user to enter his repository own dependencies.</p>
  <p><strong>Pros</strong></p>
  <ul>
  <li>The derived classes don't break if a dependency is added to the base class</li>
  <li>The repositories construction is managed by another class</li>
  </ul>
  <p><strong>Cons</strong></p>
  <ul>
  <li>The user has to create a builder for each repository he wants to create</li>
  <li>It's become harder to register every repository through the DI using a naming convention</li>
  </ul>
  <h3>3. Property injection</h3>
  <p>Perhaps removing the base repository constructor and configuring the DI to use property injection might be an option.</p>
  <p><strong>Pros</strong></p>
  <ul>
  <li>The derived classes don't break if a dependency is added to the base class</li>
  </ul>
  <p><strong>Cons</strong></p>
  <ul>
  <li>I think the property setter must be public?</li>
  </ul>
  <h1>Conclusion</h1>
  <p>Is there any of the mentioned solutions that could be acceptable in a SOLID world? If not, do you have a solution for me guys? You help is very appreciated!</p>