title: How do I combine a Controlled Lifetime relationship type (i.e. Owned<T>) with a delegate factory?
body: <p>In my application, I have a service that requires a constructor parameter not resolved by Autofac, that I instantiate using a delegate factory:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Service
  {
      public Service(string parameter /*, ... other dependencies */)
      {
      }
  
      public delegate Service Factory(string parameter);
  }
  </code></pre>
  
  <p>This works great! I really love this feature.</p>
  
  <p>I also like the Controlled Lifetime relationship, so I can let my component depend on a <code>Func&lt;Owned&lt;ISomething&gt;&gt;</code> like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Component
  {
      private Func&lt;Owned&lt;ISomething&gt;&gt; _somethingFactory;
  
      /* constructor omitted for brevity */
  
      public void DoSomethingUseful()
      {
         using (var ownedSomething = _somethingFactory())
         {
             /* Lots of useful code here */
         }
      }
  }
  </code></pre>
  
  <p>My problem is that now I want to combine the two. I can't have an instance of <code>Func&lt;Owned&lt;Service&gt;&gt;</code> injected, because it needs that parameter, so my current solution is to abstract the factory away into another service, say <code>IServiceFactory</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface IServiceFactory
  {
      Service Create(string parameter);
  }
  </code></pre>
  
  <p>...implemented as such:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ServiceFactory : IServiceFactory
  {
      private Service.Factory _internalFactory;
  
      public ServiceFactory(Service.Factory internalFactory)
      {
          _internalFactory = internalFactory;
      }
  
      public Service Create(string parameter)
      {
          return _internalFactory(parameter);
      }
  }
  </code></pre>
  
  <p>My component then becomes this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Component
  {
      Func&lt;Owned&lt;IServiceFactory&gt;&gt; _serviceFactoryFactory;
  
      /* ... */ 
  }
  </code></pre>
  
  <p>The need for such a field name leaves a bad taste in my mouth to the point that I suspect there must be a cleaner way to handle this case.</p>
  
  <p>Is there another way?</p>