title: Autofac - resolving through factory methods
body: <p>I just can't seem to wrap my brain around this situation in Autofac:</p>
  
  <ul>
  <li>My business logic needs an object of type IFinal.</li>
  <li>The implementing class Final has several dependencies that are not known by the business logic, but can be directly resolved via the IoCContainer.</li>
  <li>Final also has other dependencies that are only known by the business logic at run time.</li>
  <li>I want to be able to use Interceptors on IFinal, so class Final should be registered and resolved via IFinal.</li>
  </ul>
  
  <p>So I tried to create a factory for IFinal, but I can't seem to get the resolution right.</p>
  
  <p>Example code:</p>
  
  <pre><code>    public static void Main(string[] args)
      {
          ContainerBuilder builder = new ContainerBuilder();
  
          builder.RegisterType&lt;Dependency&gt;().As&lt;IDependency&gt;();
  
          builder.RegisterType&lt;Final&gt;().As&lt;IFinal&gt;();
  
          // this fails because context c is not available at the time finalObjectFactoryMethod is used
          builder.Register&lt;Factory&gt;((c,p)=&gt;{
              var dependency = c.Resolve&lt;IDependency&gt;();
              Func&lt;int, IFinal&gt; finalObjectFactoryMethod =
                  rta =&gt; c.Resolve&lt;IFinal&gt;(
                      new TypedParameter(typeof(IDependency), dependency),
                      new TypedParameter(typeof(int), rta)
                  );
              return new Factory(finalObjectFactoryMethod);
          }).As&lt;IFactory&gt;();
  
          // this works but doesn't use the resolve mechanism for IFinal
          /*
          builder.Register&lt;Factory&gt;((c,p)=&gt;{
              var dependency = c.Resolve&lt;IDependency&gt;();
              Func&lt;int, IFinal&gt; finalObjectFactoryMethod = rta =&gt; new Final(dependency, rta);
              return new Factory(finalObjectFactoryMethod);
          }).As&lt;IFactory&gt;();
          */
  
          IContainer container = builder.Build();
  
  
          IFactory factory = container.Resolve&lt;IFactory&gt;();
          IFinal final = factory.GetFinalObject(42);
      }
  }
  
  
  public interface IDependency{}
  
  public class Dependency: IDependency{}
  
  
  public interface IFinal
  {
      void Test();
  }
  
  public class Final: IFinal
  {
      public Final(IDependency dependency, int runtimeArgument){}
  
      public void Test(){}
  }
  
  
  public interface IFactory
  {
      IFinal GetFinalObject(int runtimeArgument);
  }
  
  public class Factory: IFactory
  {
      readonly Func&lt;int, IFinal&gt; _finalObjectFactoryMethod;
  
      public Factory(Func&lt;int, IFinal&gt; finalObjectFactoryMethod)
      {
          _finalObjectFactoryMethod = finalObjectFactoryMethod;
      }
  
      public IFinal GetFinalObject(int runtimeArgument)
      {
          return _finalObjectFactoryMethod(runtimeArgument);
      }
  }
  </code></pre>
  
  <p>Google and Autofac documentation could not help me on this.<br>
  Somehow there seems to be a knot in my brain, maybe I just try to use the wrong pattern.<br>
  I'm pretty sure there is a solution for this, but I just can't find it.  </p>