title: How to universally create Repositories that inherit from a generic type?
body: <p>I'm currently trying to implement the <em>Repository Pattern</em> on top of my DbContext. The thing is, I eventually end up with a situation where I have to inject several Repositories into a <code>UnitOfWork</code> constructor, like this:</p>
  
  <pre><code>public class UnitOfWork
  {
      private DbContext _context;
      ICustomerRepository Customers { get; private set; }
      IEmployeeRepository Employees { get; private set; } 
      public UnitOfWork(DbContext context, ICustomerRepository cust, IEmployeeRepository emp)
      {
          _context = context;
          Customers = cust;
          Employees = emp;
      }    
  }
  </code></pre>
  
  <p>However, since all of them would have to share the same DbContext, I can't see injecting them as an option.</p>
  
  <p>That's why I was thinking about creating a <code>RepositoryFactory</code> class, but since all of the repositories inherit from a generic interface I found it impossible to create a single <code>Create()</code> method, because, after all, they have no real common ancestor that would be a valid return type.</p>
  
  <p>To give you some more insight, <strong>that's how the code looks like</strong>:</p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity:class
  {
      TEntity Get(int id);
      IEnumerable&lt;TEntity&gt; GetAll();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface ICustomerRepository : IRepository&lt;Customer&gt;
  {
      IEnumerable&lt;Customer&gt; GetSeniorCustomers();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class CustomerRepository : ICustomerRepository
  {
      private readonly DbContext _context;
      public CustomerRepository(DbContext context) : base(context)
      {
          _context = context;
      }
  // ... implementation of ICustomerRepo here
  }
  </code></pre>
  
  <hr>
  
  <p>Now, that's the current state of things:</p>
  
  <p>And what I'd like to be able to do is:</p>
  
  <pre><code>public UnitOfWork(DbContext context, RepositoryFactory fac)
  {
       _context = context;
       Customers = fac.Create(context, RepoType.Customer);
       Employees = fac.Create(context, RepoType.Employee);
  }  
  </code></pre>
  
  <p>I know it doesn't really give me any additional flexibility, but it does keep the code a bit less clunky in my opinion.</p>
  
  <p>But, as I mentioned earlier, I cannot think of a valid return type for the Create() method.</p>
  
  <p>So, I came up with the idea to create multiple methods inside the RepositoryFactory class, instead of a single parameterized one, like this:</p>
  
  <pre><code>public class RepositoryFactory
  {
      public ICustomerRepository CreateCustomerRepo(DbContext context){/*...*/}
      public IEmployeeRepository CreateEmployeeRepo(DbContext context){/*...*/} 
  }
  </code></pre>
  
  <p>So the questions are:</p>
  
  <ol>
  <li>Can the thing I'm doing be even called a Factory Method? </li>
  <li>If not, is it at least a valid solution? If not, how can I <em>achieve the same thing</em> in a cleaner manner?</li>
  </ol>
  
  <p>By <em>achieve the same thing</em> I mean implement a way of creating those repositories in a managable, concise way.</p>
  
  <p>Thank you for all the help in advance. </p>