title: Windsor Typed Factory with Open Generic Interface
body: <p>In my application, I want to take dependencies on multiple repositories in a class, where not all of them are required each time.  Rather than constructing an instance of each one where unnecessary, I use the <a href="http://docs.castleproject.org/%28S%28kwaa14uzdj55gv55dzgf0vui%29%29/Windsor.Typed-Factory-Facility-interface-based-factories.ashx" rel="nofollow">Typed Factory facility</a> in Windsor.</p>
  
  <p>However, registering a factory for each repository is a bit tiresome, and I would like to replace this with an open generic registration.  What I want to do is something like the following:</p>
  
  <pre><code>container.Register(
      Component.For&lt;IFactory&lt;IRepository&lt;&gt;&gt;&gt;().AsFactory()
  );
  </code></pre>
  
  <p>However, this is a syntax error because of the missing type parameter for IRepository.  Is there a syntax I can use which would make this work?</p>
  
  <p>NB: I'm aware that I can register an untyped Factory interface and use this to create multiple components.  I'm not interested in doing this as this is essentially taking a dependency on a service locator - if I've not registered a dependency then I won't know about it until the code tries to use it - with my approach I know about this in the constructor even though I'm not creating an instance yet.</p>
  
  <p>Full (simplified) sample below:</p>
  
  <pre><code>public class TestA { }
  public class TestB { }
  public interface IRepository&lt;T&gt; { T Create();    }
  public class Repository&lt;T&gt; : IRepository&lt;T&gt;
  {
      public T Create() { return Activator.CreateInstance&lt;T&gt;(); }
  }
  
  public interface IFactory&lt;T&gt;
  {
      T Create();
      void Release(T instance);
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          IWindsorContainer container = new WindsorContainer();
          container.AddFacility&lt;TypedFactoryFacility&gt;();
  
          container.Register(
              // Individual registrations of repositories here are fine
              Component.For&lt;IRepository&lt;TestA&gt;&gt;().ImplementedBy&lt;Repository&lt;TestA&gt;&gt;(),
              Component.For&lt;IRepository&lt;TestB&gt;&gt;().ImplementedBy&lt;Repository&lt;TestB&gt;&gt;()
          );
  
          container.Register(
              // Individual registrations of factories - works, but trying to avoid!
              Component.For&lt;IFactory&lt;IRepository&lt;TestA&gt;&gt;&gt;().AsFactory(),
              Component.For&lt;IFactory&lt;IRepository&lt;TestB&gt;&gt;&gt;().AsFactory()
          );
  
          container.Register(
              // Generic Registration of Factories - syntax errors
              // Component.For&lt;IFactory&lt;IRepository&lt;&gt;&gt;&gt;().AsFactory()
              // Component.For(typeof(IFactory&lt;IRepository&lt;&gt;&gt;)).AsFactory()
          );
  
          var factoryA = container.Resolve&lt;IFactory&lt;IRepository&lt;TestA&gt;&gt;&gt;();
          var factoryB = container.Resolve&lt;IFactory&lt;IRepository&lt;TestB&gt;&gt;&gt;();
  
          var repoA = factoryA.Create();
          var repoB = factoryB.Create();
  
          Console.WriteLine("Everything worked");
      }
  }
  </code></pre>