title: DI with command pattern
body: <p>I have a service which acts to create and dispatch commands for execution.  It can either write commands to queue for deferred processing, or execute them immediately.</p>
  
  <p>For these purposes I have something like:</p>
  
  <pre><code>class MyService
  {
      private ICommandQueueWriter _commandQueueWriter;
      private ICommandExecutor _commandExecutor;
  
      public MyService(ICommandQueueWriter cqw, ICommandExecutor ce)
      {
          _commandQueueWriter = cqr;
          _commandExecutor = ce;
      }
  
      public void DoSomething()
      {
          _commandQueueWriter.Write(new SomeCommand());
          _commandExecutor.Execute(new SomeOtherCommand());
      }
  }
  </code></pre>
  
  <p>The service will deal with all kinds of commands.  I have a series of <code>ICommandHandler&lt;&gt;</code> implementations which will be registered with a DI container.</p>
  
  <p>My plan has a flaw currently: the <code>ICommandExecutor</code> implementation needs to have access to all of the <code>ICommandHandler&lt;&gt;</code> implementations.  In fact, I guess the queued command execution will face the same problem: I'll grab the message later and have to look up the handler somehow.</p>
  
  <p>So my options that I can see are:</p>
  
  <ol>
  <li>Don't use <code>ICommandExecutor</code> as a dependency, just use <code>ICommandHandler&lt;&gt;</code>s directly.  But I wanted the option of wrapping all command handler execution through a standard class - to catch exceptions in a consistent way, or manage execution in some other consistent way.  I really like the option of having a consistent interface for immediate/deferred execution (either call Write or Execute)</li>
  <li>Pass the DI container or root into <code>ICommandExecutor</code> and let it resolve commands.  This seems to break the idea that there should be one call to compose an object graph with DI, and might 'hide' dependencies</li>
  <li>Have the implementation of <code>ICommandExecutor</code> have all <code>ICommandHandler&lt;&gt;</code>s as a dependency to be injected - so it can pick the one it wants manually.  However that doesn't seem ideal either, since all handlers on the system would be instantiated at that point</li>
  </ol>
  
  <p>Is there are fourth option or do I need to bite the bullet with one of these compromises?</p>