title: Autofac resolve dependency in CQRS CommandDispatcher
body: <p>I'm trying to implement a simple CQRS-application example.</p>
  
  <p>This is a structure of my "Command" part:</p>
  
  <pre><code>public interface ICommand
  {
  }
  
  //base interface for command handlers
  interface ICommandHandler&lt;in TCommand&gt; where TCommand: ICommand
  {
      void Execute(TCommand command);
  }
  
  // example of the command
  public class SimpleCommand: ICommand 
  {
     //some properties
  }
  
  // example of the SimpleCommand command handler
  public class SimpleCommandHandler: ICommandHandler&lt;SimpleCommand&gt;
  {
      public void Execute(SimpleCommand command)
      {
         //some logic
      }
  
  }
  </code></pre>
  
  <p>This is interface <code>ICommandDipatcher</code>. It dispatches a command to its handler.</p>
  
  <pre><code>public interface ICommandDispatcher
  {
      void Dispatch&lt;TCommand&gt;(TCommand command) where TCommand : ICommand;
  }
  </code></pre>
  
  <p>This is a default implementation of <code>ICommandDispatcher</code>and the main problem is to obtain the necessary command handler by the type of the command via <code>Autofac</code>.</p>
  
  <pre><code>public class DefaultCommandDispatcher : ICommandDispatcher
  {
      public void Dispatch&lt;TCommand&gt;(TCommand command) where TCommand : ICommand
      {
          //How to resolve/get object of the neseccary command handler 
          //by the type of command (TCommand)
          handler.Execute(command);
      }
  }
  </code></pre>
  
  <p>What is the best way to resolve implementation of <code>ICommandHanler</code> by type of the command in that case via <strong>Autofac</strong>?</p>
  
  <p>Thanks!</p>