title: Dependant object as parameter to dependency in Autofac
body: <p>In broader terms what I am trying to achieve with Autofac is to pass the dependant (a.k.a. parent) object to its dependencies.</p>
  
  <p>For example:</p>
  
  <pre><code>interface IDependency {}
  
  class Dependant
  {
      IDependency Dependency { get; set; }
  }
  
  class ConcreteDependency : IDependency
  {
      ConcreteDependency(Dependant dependant) { /* ... */ }
  }
  </code></pre>
  
  <p>I am hoping this could work, because <code>Dependant</code> breaks the dependency loop using property injection (meaning you <em>can</em> create an instance of <code>Dependant</code>, <em>before</em> having to resolve <code>IDependency</code>). Whilst, if both classes used ctor-injection this wouldn't be possible.</p>
  
  <p>Specifically, I am trying to <em>inject</em> the current ASP.NET MVC controller instance to one of its dependencies.</p>
  
  <p>Take a look at:</p>
  
  <pre><code>public abstract class ApplicationController : Controller
  {
      public ILogger Logger { get; set;}
  }
  
  public class SomeController : ApplicationController
  {
      [HttpPost]
      public ActionResult Create(FormCollection formData)
      {
          // something fails...
          this.Logger.Log("Something has failed.");
      }
  }
  
  public interface ILogger
  {
      public void Log(string message);
  }
  
  public class TempDataLogger : ILogger
  {
      private ControllerBase controller;
  
      public NullLogger(ControllerBase controller)
      {
          this.controller = controller;
      }
  
      public void Log(string message)
      {
          this.controller.TempData["Log"] = message;
      }
  }
  </code></pre>
  
  <p>In plain English the above code uses <code>TempData</code> as a way of "logging" messages (maybe to print it out in a nice way in view-layout or something...).</p>
  
  <p>Simple enough all controllers are registered in Autofac:</p>
  
  <pre><code>builder.RegisterControllers(typeof(MvcApplication).Assembly)
         .PropertiesAutowired(); // not strictly necessary
  </code></pre>
  
  <p>But then, how can I tweak the <code>ILogger</code> registration below to make it work?</p>
  
  <pre><code>builder.RegisterType&lt;TempDataLogger&gt;()
         .As&lt;ILogger&gt;()
         .InstancePerRequest();
  </code></pre>
  
  <p>Is this even possible in Autofac?</p>
  
  <p>Thank you.</p>