title: How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor
body: <p>This helpful article from David Haydn (EDIT: scam link removed,  it could have been <a href="http://codebetter.com/davidhayden/2008/11/06/unity-application-block-and-decorator-pattern/" rel="noreferrer">this article</a>) shows how you can use the <code>InjectionConstructor</code> class to help you set up a chain using the decorator pattern with Unity. However, if the items in your decorator chain have other parameters in their constructor, the <code>InjectionConstructor</code> must explicitly declare each one of them (or Unity will complain that it can't find the right constructor). This means that you can't simply add new constructor parameters to items in the decorator chain without also updating your Unity configuration code.</p>
  
  <p>Here's some example code to explain what I mean. The <code>ProductRepository</code> class is wrapped first by <code>CachingProductRepository</code> and then by <code>LoggingProductRepostiory</code>. Both CachingProductRepository and LoggingProductRepository, in addition to taking a IProductRepository in their constructor, also need other interfaces from the container.</p>
  
  <pre><code>    public class Product 
      {
          public int Id;
          public string Name;
      }
  
      public interface IDatabaseConnection { }
  
      public interface ICacheProvider 
      { 
          object GetFromCache(string key);
          void AddToCache(string key, object value);
      }
  
      public interface ILogger
      {
          void Log(string message, params object[] args);
      }
  
  
      public interface IProductRepository
      {
          Product GetById(int id);    
      }
  
      class ProductRepository : IProductRepository
      {
          public ProductRepository(IDatabaseConnection db)
          {
          }
  
          public Product GetById(int id)
          {
              return new Product() { Id = id, Name = "Foo " + id.ToString() };
          }
      }
  
      class CachingProductRepository : IProductRepository
      {
          IProductRepository repository;
          ICacheProvider cacheProvider;
          public CachingProductRepository(IProductRepository repository, ICacheProvider cp)
          {
              this.repository = repository;
              this.cacheProvider = cp;
          }
  
          public Product GetById(int id)
          {       
              string key = "Product " + id.ToString();
              Product p = (Product)cacheProvider.GetFromCache(key);
              if (p == null)
              {
                  p = repository.GetById(id);
                  cacheProvider.AddToCache(key, p);
              }
              return p;
          }
      }
  
      class LoggingProductRepository : IProductRepository
      {
          private IProductRepository repository;
          private ILogger logger;
  
          public LoggingProductRepository(IProductRepository repository, ILogger logger)
          {
              this.repository = repository;
              this.logger = logger;
          }
  
          public Product GetById(int id)
          {
              logger.Log("Requesting product {0}", id);
              return repository.GetById(id);
          }
      }
  </code></pre>
  
  <p>Here's a (passing) unit test. See the comments for the bits of surplus configuration I want to remove the need for:</p>
  
  <pre><code>    [Test]
      public void ResolveWithDecorators()
      {
          UnityContainer c = new UnityContainer();            
          c.RegisterInstance&lt;IDatabaseConnection&gt;(new Mock&lt;IDatabaseConnection&gt;().Object);
          c.RegisterInstance&lt;ILogger&gt;(new Mock&lt;ILogger&gt;().Object);
          c.RegisterInstance&lt;ICacheProvider&gt;(new Mock&lt;ICacheProvider&gt;().Object);
  
          c.RegisterType&lt;IProductRepository, ProductRepository&gt;("ProductRepository");
  
          // don't want to have to update this line every time the CachingProductRepository constructor gets another parameter
          var dependOnProductRepository = new InjectionConstructor(new ResolvedParameter&lt;IProductRepository&gt;("ProductRepository"), new ResolvedParameter&lt;ICacheProvider&gt;());
          c.RegisterType&lt;IProductRepository, CachingProductRepository&gt;("CachingProductRepository", dependOnProductRepository);
  
          // don't want to have to update this line every time the LoggingProductRepository constructor changes
          var dependOnCachingProductRepository = new InjectionConstructor(new ResolvedParameter&lt;IProductRepository&gt;("CachingProductRepository"), new ResolvedParameter&lt;ILogger&gt;());
          c.RegisterType&lt;IProductRepository, LoggingProductRepository&gt;(dependOnCachingProductRepository);
          Assert.IsInstanceOf&lt;LoggingProductRepository&gt;(c.Resolve&lt;IProductRepository&gt;());
      }
  </code></pre>