title: How to inject two instances of same object using Autofac?
body: <p>I'm using Autofac constructor injection.  I need to figure out how to inject a single object instance into more than one constructor argument, without needing to explicitly resolve each argument during the container setup phase.</p>
  
  <p>I have a complex scenario which would be simplified by this behavior; the following example is just a simplified scenario so I can demonstrate the behavior I'm looking for.</p>
  
  <p><strong>Example:</strong></p>
  
  <p><strong>Say I have these two interfaces, IOpenable and ICloseable:</strong></p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public interface IOpenable
  {
      void Open();
  }
  public interface ICloseable
  {
      void Close();
  }
  </code></pre>
  
  <p><strong>And I have this Door class which implements both of them:</strong></p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public interface IDoor : IOpenable, ICloseable { }
  public class Door : IDoor, IOpenable, ICloseable 
  {
      void Open() { ... }
      void Close() { ... }
  }
  </code></pre>
  
  <p><strong>And I have this class which accepts an IOpenable and an ICloseable:</strong></p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public class DoorHandler : IDoorHandler
  {
      public DoorHandler(IOpenable openable, ICloseable closeable)
      {
          ...
      }
      ...
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>Is it possible to tell autofac to inject the <em>same</em> Door object into both arguments whenever both an IOpenable and ICloseable are dependencies in the same constructor?  </p>
  
  <p><strong>Note: I can't do</strong>:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>container.Register&lt;IDoorHandler&gt;( c =&gt; {
      Door d = c.Resolve&lt;IDoor&gt;();
      return new DoorHandler(d,d)
  });
  </code></pre>
  
  <p>That <em>would</em> do what I want, but remember that this DoorHandler class is just an example.  In my real code, the "DoorHandler" is really an MVC Controller and I'm registering it with RegisterControllers().  So I can't register it like the above.  Besides, sometimes dependency graphs can get overly complex and doing this explicitly in every case can become overwhelming.</p>
  
  <p>I guess what I'm looking for is to be able to do something like:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>container.RegisterType&lt;DoorHandler&gt;().As&lt;IDoorHandler&gt;();
  container.RegisterType&lt;Door&gt;().As&lt;IDoor&gt;();
  container.Register&lt;IOpenable&gt;( c =&gt; c.ResolveShared&lt;IDoor&gt;(); );
  container.Register&lt;ICloseable&gt;( c =&gt; c.ResolveShared&lt;IDoor&gt;(); );
  </code></pre>
  
  <p>where calls to <code>c.ResolveShared&lt;T></code> will all resolve to the same T object if called for more than one argument in same constructor.</p>
  
  <p>Or perhaps:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>container.RegisterType&lt;DoorHandler&gt;().As&lt;IDoorHandler&gt;();
  container.RegisterType&lt;Door&gt;().As&lt;IDoor&gt;().InstancePerDependencyShared();
  container.Register&lt;IOpenable&gt;( c =&gt; c.Resolve&lt;IDoor&gt;(); );
  container.Register&lt;ICloseable&gt;( c =&gt; c.Resolve&lt;IDoor&gt;(); );
  </code></pre>
  
  <p>Obviously if I was using an InstancePerLifetimeScope or something for the Door object, each resolved Door would be the same instance. But I don't want that, I want a new instance of Door each time a DoorHandler is created, and I want that Door to be passed as both arguments to the DoorHandler constructor.</p>