title: Register Generic Type in Unity Based On Concrete Type
body: <p>I'm trying to emulate a behavior that I can configure in Ninject, only using Unity instead.</p>
  
  <p>I am attempting to use the Cached Repository Pattern, given the following classes and interface:</p>
  
  <pre><code>public interface IRepository&lt;T&gt;
  {
      T Get();
  }
  
  public class SqlRepository&lt;T&gt; : IRepository&lt;T&gt;
      where T : new()
  {
      public T Get()
      {
          Console.WriteLine("Getting object of type '{0}'!", typeof(T).Name);
          return new T();
      }
  }
  
  public class CachedRepository&lt;T&gt; : IRepository&lt;T&gt;
      where T : class
  {
      private readonly IRepository&lt;T&gt; repository;
  
      public CachedRepository(IRepository&lt;T&gt; repository)
      {
          this.repository = repository;
      }
  
      private T cachedObject;
      public T Get()
      {
          if (cachedObject == null)
          {
              cachedObject = repository.Get();
          }
          else
          {
              Console.WriteLine("Using cached repository to fetch '{0}'!", typeof(T).Name);
          }
          return cachedObject;
      }
  }
  </code></pre>
  
  <p>Basically, any time my application uses <code>IRepository&lt;T&gt;</code>, it should get an instance of <code>CachedRepository&lt;T&gt;</code>. But inside of <code>CachedRepository&lt;T&gt;</code> it should fetch the actual SQL repository of <code>SqlRepository&lt;T&gt;</code>. In Ninject, I have accomplished this using the following:</p>
  
  <pre><code>ninjectModule.Bind(typeof(IRepository&lt;&gt;)).To(tyepof(SqlRepository&lt;&gt;)).WhenInjectedExactlyInto(tyepof(CachedRepository&lt;&gt;));
  ninjectModule.Bind(typeof(IRepository&lt;&gt;)).To(tyepof(CachedRepository&lt;&gt;));
  </code></pre>
  
  <p>In Unity, how would I accomplish the same thing? I have a version for non-generic repositories that works:</p>
  
  <pre><code>UnityContainer container = new UnityContainer();
  container.RegisterType&lt;IWidgetRepository, CachedWidgetRepository&gt;(new InjectionMember[] { new InjectionConstructor(new SqlWidgetRepository()) });
  </code></pre>
  
  <p>But this syntax won't work at all with the generic repository, since you can't say <code>new SqlRepository&lt;&gt;</code> without having a syntax error. Any ideas?</p>