title: How to do open generic decorator chaining with unity + UnityAutoRegistration
body: <p>Went off on an interesting tangent today after reading <a href="https://cuttingedge.it/blogs/steven/pivot/entry.php?id=91" rel="nofollow">this article on command handler decoration</a>. I wanted to see if I could implement the pattern using Unity instead of <a href="https://simpleinjector.org/" rel="nofollow">SimpleInjector</a>, and so far it is proving extremely difficult. </p>
  
  <p>The first thing I had to do was install <a href="https://autoregistration.codeplex.com/" rel="nofollow">UnityAutoRegistration</a> to resolve the open generic <code>ICommandHandler&lt;TCommand&gt;</code> interface. Current solution for that aspect is as follows:</p>
  
  <pre><code>Container = new UnityContainer().LoadConfiguration();
  
  Container.ConfigureAutoRegistration()
      .ExcludeSystemAssemblies()
      .Include(type =&gt; type.ImplementsOpenGeneric(typeof(ICommandHandler&lt;&gt;)),
          (t, c) =&gt; c.RegisterType(typeof(ICommandHandler&lt;&gt;), t)
      )
      .ApplyAutoRegistration()
  ;
  </code></pre>
  
  <p>This works for the first part, resolving any <strong>single</strong> <code>ICommandHandler&lt;TCommand&gt;</code>. What's proven frustrating so far is implementing a decoration handler. As soon as I add a second <code>ICommandHandler&lt;TCommand&gt;</code> as a decorator, Unity throws a StackOverflowException. I don't know enough about Unity internals, but I'm guessing this is because it can't figure out which instance to resolve to -- the command handler, or the command handler decorator -- since both implement the <code>ICommandHandler&lt;TCommand&gt;</code> interface.</p>
  
  <p>Googling around led me <a href="http://www.pnpguidance.net/post/GenericDecoratorChainsExampleUnityDependencyInjectionContainer.aspx" rel="nofollow">first to this article</a>, which explains how to do it in what I would consider a brute force method. I also found <a href="http://davidhayden.com/blog/dave/archive/2008/11/19/ChainResponsibilityDesignPatternUnityDependencyInjectionContainer.aspx" rel="nofollow">these</a> <a href="https://stackoverflow.com/questions/6109646/how-do-i-use-the-decorator-pattern-with-unity-without-explicitly-specifying-ever">related</a> <a href="https://unity.codeplex.com/discussions/24015?ProjectName=unity" rel="nofollow">pages</a> but none is a complete solution to my problem (and I am too ignorant to figure it out for myself).</p>
  
  <p>I then found <a href="http://www.beefycode.com/post/Decorator-Unity-Container-Extension.aspx" rel="nofollow">this article, which seems to address my same concerns</a>. However beefy's solution does not account for dealing with open generics. Currently most of our dependencies are loaded from a unity section in the .config file, so I don't want to write a ton of compiled code for each handler or decorator. It seems like having some kind of UnityContainerExtension and DecoratorBuildStrategy is the right way to go, but I can't figure it out.  I have been playing with beefy's code for a little while now, and am completely stuck. My attempts to modify his code to account for generics has led to BadImageFormatExceptions (An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)).</p>
  
  <p>I like the idea of doing this to implement the decorator chaining, because it's short, and there is only 1 line per concern:</p>
  
  <pre><code>var container = new Container();
  
  // Go look in all assemblies and register all implementations
  // of ICommandHandler&lt;T&gt; by their closed interface:
  container.RegisterManyForOpenGeneric(typeof(ICommandHandler&lt;&gt;),
      AppDomain.CurrentDomain.GetAssemblies());
  
  // Decorate each returned ICommandHandler&lt;T&gt; object with an
  // TransactionCommandHandlerDecorator&lt;T&gt;.
  container.RegisterDecorator(typeof(ICommandHandler&lt;&gt;),
      typeof(TransactionCommandHandlerDecorator&lt;&gt;));
  
  // Decorate each returned ICommandHandler&lt;T&gt; object with an
  // DeadlockRetryCommandHandlerDecorator&lt;T&gt;.
  container.RegisterDecorator(typeof(ICommandHandler&lt;&gt;),
      typeof(DeadlockRetryCommandHandlerDecorator&lt;&gt;));
  </code></pre>
  
  <p>...but I don't want to change my container from Unity to Simple Injector if I don't have to. </p>
  
  <p>SO my question is how could I go about implementing open generic decorator chaining using unity (plus <code>UnityAutoRegistration</code>)?</p>