title: Unity: Register and resolve class with generic type
body: <p>I'm using Unity and try to follow to SOLID-principles as far as possible. Therefore all implementations only have dependencies to interfaces.</p>
  
  <p>I have a collectionwrapper which looks like this:</p>
  
  <pre><code>public interface ICollectionWrapper&lt;TModel&gt;
  {
      int TotalCount { get; set; }
      IEnumerable&lt;TModel&gt; Items { get; set; }
  }
  </code></pre>
  
  <p>Now I want to create the instance of <code>ICollectionFactory&lt;T&gt;</code> with a factory. This is what I got so far:</p>
  
  <pre><code>public interface ICollectionWrapperFactory
  {
      ICollectionWrapper&lt;T&gt; CreateCollection&lt;T&gt;();
      ICollectionWrapper&lt;T&gt; CreateCollection&lt;T&gt;(IEnumerable&lt;T&gt; items);
      ICollectionWrapper&lt;T&gt; CreateCollection&lt;T&gt;(IEnumerable&lt;T&gt; items, int totalCount);
  }
  
  public class CollectionWrapperFactory : ICollectionWrapperFactory
  {
      private readonly IUnityContainer _container;
  
      public CollectionWrapperFactory(IUnityContainer container)
      {
          _container = container;
      }
  
      public ICollectionWrapper&lt;T&gt; CreateCollection&lt;T&gt;()
      {
          var collectionWrapper = _container.Resolve&lt;ICollectionWrapper&lt;T&gt;&gt;();
          return collectionWrapper;
      }
  
      public ICollectionWrapper&lt;T&gt; CreateCollection&lt;T&gt;(IEnumerable&lt;T&gt; items)
      {
          throw new System.NotImplementedException();
      }
  
      public ICollectionWrapper&lt;T&gt; CreateCollection&lt;T&gt;(IEnumerable&lt;T&gt; items, int totalCount)
      {
          throw new System.NotImplementedException();
      }
  }
  </code></pre>
  
  <p>I know that using the container as a servicelocator is considered an anti-pattern, but I don't know any better way to solve this. If there's a better pattern for doing this I'm all ears... An alternative is using the Activator, but then the factory would need to know about the actual implementation of <code>ICollectionWrapper&lt;T&gt;</code>.</p>
  
  <p>But the real problem is that I cannot register the ICollectionWrapper correctly.</p>
  
  <pre><code>container.RegisterType&lt;ICollectionWrapper&lt;T&gt;, CollectionWrapper&lt;T&gt;&gt;(new TransientLifetimeManager()); // Does not compile.
  </code></pre>
  
  <p><code>T</code> may be any type. <strong>I want to be able to create instances of <code>ICollectionWrapper&lt;T&gt;</code> without having to register every possible combination of <code>T</code>.</strong></p>
  
  <p>Currently I only have one implementation of <code>ICollectionWrapper&lt;T&gt;</code>. But the point is that I really want Unity to be the only part that knows about the actual implementation.</p>
  
  <pre><code>[DataContract]
  public class CollectionWrapper&lt;TModel&gt; : ICollectionWrapper&lt;TModel&gt;
  {
      public CollectionWrapper(IEnumerable&lt;TModel&gt; items)
      {
          Items = items;
      }
  
      public CollectionWrapper(IEnumerable&lt;TModel&gt; items, int totalCount)
      {
          Items = items;
          TotalCount = totalCount;
      }
  
      public CollectionWrapper()
      {
  
      }
  
      [DataMember]
      public int TotalCount { get; set; }
      [DataMember]
      public IEnumerable&lt;TModel&gt; Items { get; set; }
  }
  </code></pre>