title: How to register Generic type with Unity container
body: <p>I am struggling to inject a generic type repository into my service class using Unity. At lease, I think this generic repository is what is causing the trouble.  Can somebody please point out what am I doing wrong?. Please bear with me since I am fairly new to Unity.  Firstly, here is the error message I receive the following error.</p>
  
  <p>Resolution of the dependency failed, type = "TestUnity.Services.Contracts.ICustBillingTypeService", name = "(none)".
  Exception occurred while: while resolving.</p>
  
  <h2>Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.</h2>
  
  <p>At the time of the exception, the container was:</p>
  
  <p>Resolving TestUnity.Services.Implementations.CustBillingTypeService,(none) (mapped from TestUnity.Services.Contracts.ICustBillingTypeService, (none))
    Resolving parameter "repo" of constructor TestUnity.Services.Implementations.CustBillingTypeService(TestUnity.Repositories.Contracts.IRepository<code>1[[TestUnity.DAL.CustBillingType, TestUnity.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] repo)
      Resolving TestUnity.Repositories.Implementations.Repository</code>1[TestUnity.DAL.CustBillingType],(none) (mapped from TestUnity.Repositories.Contracts.IRepository<code>1[TestUnity.DAL.CustBillingType], (none))
      Resolving parameter "ctx" of constructor TestUnity.Repositories.Implementations.Repository</code>1[[TestUnity.DAL.CustBillingType, TestUnity.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]](TestUnity.Repositories.Contracts.IDataContext ctx)
        Resolving TestUnity.Repositories.Implementations.DataContext,(none) (mapped from TestUnity.Repositories.Contracts.IDataContext, (none))
        Resolving parameter "nameOrConnectionString" of constructor TestUnity.Repositories.Implementations.DataContext(System.String nameOrConnectionString)
          Resolving System.String,(none)</p>
  
  <p>Here is my Unity bootstrap class that is in a separate class library</p>
  
  <pre><code>    public static class UnityBootstrap
  {
      private static IUnityContainer container;
      public static IUnityContainer Container
      {
          get
          {
              if (container == null)
                  InitializeContainer();
              return container;
          }
      }
  
      public static void InitializeContainer()
      {
          container = new UnityContainer();
          container.RegisterType&lt;ICustBillingTypeService, CustBillingTypeService&gt;();
          container.RegisterType&lt;IDataContext, DataContext&gt;();
          container.RegisterType(typeof(IRepository&lt;&gt;), typeof(Repository&lt;&gt;));
      }
  }
  
      public interface IRepository&lt;T&gt; where T : class, IObjectState
  {
      T FindOne(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
  }
  
      public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class, IObjectState
  {
      private readonly IDataContext context;
      private readonly IDbSet&lt;T&gt; dbSet;
  
      public Repository() { }
  
      public Repository(IDataContext ctx)
      {
          this.context = ctx;
          var dbContext = context as DbContext;
          dbSet = dbContext.Set&lt;T&gt;();
      }
  
      public virtual T FindOne(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return dbSet.SingleOrDefault(predicate);
      }
  }
  
  public interface ICustBillingTypeService
  {
      CustBillingType GetCustBillingTypeByPKId(int pkId);
  }
  
  public class CustBillingTypeService : ICustBillingTypeService
  {
      IRepository&lt;CustBillingType&gt; custBillingTypeRepo;
  
      public CustBillingTypeService() { }
  
      public CustBillingTypeService(IRepository&lt;CustBillingType&gt; repo)
      {
          custBillingTypeRepo = repo;
      }
      public CustBillingType GetCustBillingTypeByPKId(int pkId)
      {
          var cbt = custBillingTypeRepo.FindOne(x =&gt; x.PKId == pkId);
          return cbt;
      }
  }
  
  public interface IDataContext : IDisposable
  {
      int SaveChanges();
      void SyncObjectState&lt;TEntity&gt;(TEntity entity) where TEntity : class, IObjectState;
      void SyncObjectsStatePostCommit();
  }
  
  
  public class DataContext : DbContext, IDataContext
  {
      private readonly Guid _instanceId;
      bool _disposed;
  
      public DataContext(string nameOrConnectionString) : base(nameOrConnectionString)
      {
          _instanceId = Guid.NewGuid();
          Configuration.LazyLoadingEnabled = false;
          Configuration.ProxyCreationEnabled = false;
      }
      public Guid InstanceId { get { return _instanceId; } }
      public override int SaveChanges()
      {
          SyncObjectsStatePreCommit();
          var changes = base.SaveChanges();
          SyncObjectsStatePostCommit();
          return changes;
      }
  
      protected override void Dispose(bool disposing)
      {
          if (!_disposed)
          {
              if (disposing)
              {
              }
              _disposed = true;
          }
  
          base.Dispose(disposing);
      }
  }
  
  public partial class TestModel : DataContext
  {
      public TestModel() : base("name=TestModel") {}
  
      public virtual DbSet&lt;CustBillingType&gt; CustBillingTypes { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          CustBillingTypeModelBuilder.Map(modelBuilder);
      }
  }
  
  [TestClass]
  public class UnityTestCases
  {
      private IUnityContainer uContainer;
      private ICustBillingTypeService icbService;
      public UnityTestCases()
      {
          uContainer = UnityBootstrap.Container;
          icbService = uContainer.Resolve&lt;ICustBillingTypeService&gt;();
      }
      [TestMethod]
      public void GetCustBillingTypeByPKId_return_ValidObj()
      {
          var obj = icbService.GetCustBillingTypeByPKId(pkId: 15);
          Assert.IsNotNull(obj);
      }
  }
  </code></pre>
  
  <p>Please note that in my test case, I am not mocking anything.  This is sort of integration test.</p>
  
  <p>If I had to guess, I would think there is something wrong with the Generic Type Registration.  Or it could be totally something else.</p>
  
  <p>Babu.</p>