title: Autofac - How to create a generated factory with parameters
body: <p>I am trying to create with Autofac a 'generated' factory that will resolve dependencies in real-time based on an enum parameter.</p>
  
  <p><strong><em>Given the following interfaces/classes</em></strong>:</p>
  
  <pre><code>public delegate IConnection ConnectionFactory(ConnectionType connectionType);
  
  public enum ConnectionType
  {
      Telnet,
      Ssh
  }
  
  public interface IConnection
  {
      bool Open();
  }
  
  public class SshConnection : ConnectionBase, IConnection
  {
      public bool Open()
      {
          return false;
      }
  }
  
  public class TelnetConnection : ConnectionBase, IConnection
  {
      public bool Open()
      {
          return true;
      }
  }
  
  public interface IEngine
  {
      string Process(ConnectionType connectionType);
  }
  
  public class Engine : IEngine
  {
      private ConnectionFactory _connectionFactory;
  
      public Engine(ConnectionFactory connectionFactory)
      {
          _connectionFactory = connectionFactory;
      }
  
      public string Process(ConnectionType connectionType)
      {
          var connection = _connectionFactory(connectionType);
  
          return connection.Open().ToString();
      }
  }
  </code></pre>
  
  <p>I'd like to use Autofac to generate some sort of factory that has a method that receives one parameter: ConnectionType and returns the correct connection object.</p>
  
  <p><strong><em>I started with the following registrations:</em></strong></p>
  
  <pre><code>builder.RegisterType&lt;AutoFacConcepts.Engine.Engine&gt;()
      .As&lt;IEngine&gt;()
      .InstancePerDependency();
  
  builder.RegisterType&lt;SshConnection&gt;()
      .As&lt;IConnection&gt;();
  builder.RegisterType&lt;TelnetConnection&gt;()
      .As&lt;IConnection&gt;();
  </code></pre>
  
  <p>I then continued to play with the TelnetConnection/SshConnection registrations with different options:</p>
  
  <ol>
  <li>Named</li>
  <li>Keyed</li>
  <li>Metadata</li>
  </ol>
  
  <p>I couldn't find the correct combination of the registrations that will allow me to define a generated factory delegate that will return the correct connection object (SshConnection for ConnectionType.Ssh and TelnetConnection for ConnectionType.Telnet).</p>