title: Generic repository lifetime configuration with Windsor
body: <p>I'm out of ideas how to configure right Windsor container for use with repositories in Windows app.
  I have generic repository implementation Repository, where T is entity type, it has a dependency IDatacontextProvider, which provides datacontext for it: </p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      protected DataContext DataContext;
      public Repository(IDataContextProvider dataContextProvider) {
           DataContext = dataContextProvider.DataContext;
      }
      ....
  }
  </code></pre>
  
  <p>And for simple things everything works ok with following configuration:</p>
  
  <pre><code> container.Register(                                
              Component.For&lt;IDataContextProvider&gt;()
                    .ImplementedBy&lt;DataContextProvider&gt;()
                    .Lifestyle.Transient,
              Component.For(typeof(IRepository&lt;&gt;))
                    .ImplementedBy(typeof(Repository&lt;&gt;))
                    .Lifestyle.Transient, .... 
  </code></pre>
  
  <p>Problems occur, when i try to join different entities from several repositories, as long as each repository instance has different data context instance.<br>
  For example i have simple service:</p>
  
  <pre><code>public class SimpleService : ISimpleService {
       public SimpleService(IRepository&lt;Order&gt;, IRepository&lt;OrderLine&gt;) {
           ....
       }
  }
  </code></pre>
  
  <p>I could make IDataContextProvider as Singleton, but i think that would bring even bigger problems.<br>
  I could pass IDataContextProvider to SimpleService, and try to resolve repository instances there, but that would require additional code to make service easy testable and would require additional dependencies.
  May be somebody has a better idea how to solve this?</p>
  
  <p>update:
  following advice, I've created repository factory (it's little bit different from proposed in answer, it does not have direct dependency to datacontext, but idea is very same):</p>
  
  <pre><code> public interface IRepositoryFactory
  {
      IRepository&lt;T&gt; GetRepository&lt;T&gt;() where T:class;
  }
  
  public class RepositoryFactory : IRepositoryFactory
  {
      private readonly IDataContextProvider dataContextProvider;
  
      public RepositoryFactory(IDataContextProvider dataContextProvider)
      {
          this.dataContextProvider = dataContextProvider;
      }
  
      public IRepository&lt;T&gt; GetRepository&lt;T&gt;() where T : class
      {
          return new Repository&lt;T&gt;(dataContextProvider);
      }
  }
  </code></pre>