title: Base controller constructor injection in ASP.NET MVC with Unity
body: <p>I have a base controller in my MVC 5 project which implements some shared functionality. This functionality requires some dependencies. I am using Unity 3 to inject these implementations into my controllers, a pattern which has worked fine until I switched my controllers to inherit from this base controller. Now I am running into the following issue:</p>
  
  <pre><code>public class BaseController : Controller
  {
      private readonly IService _service;
  
      public BaseController(IService service)
      {
          _service = service;
      }
  }
  
  public class ChildController : BaseController
  {
      private readonly IDifferentService _differentService;
  
      public ChildController(IDifferentService differentService)
      {
          _differentService = differentService;
      }
  }
  </code></pre>
  
  <p>This is understandably throwing an error of <code>'BaseController' does not contain a constructor that takes 0 arguments</code>. Unity is not resolving the construction of the BaseController, so it can't inject the dependencies into it. I see two obvious ways of solving this issue:</p>
  
  <p><strong>1.) Explicitly call the BaseController ctor and have each ChildController ctor inject the BaseController's dependencies</strong></p>
  
  <pre><code>public class ChildController : BaseController
  {
      private readonly IDifferentService _differentService;
  
      public ChildController(IDifferentService differentService,
                             IService baseService)
          : base(baseService)
      {
          _differentService = differentService;
      }
  }
  </code></pre>
  
  <p>I don't like this approach for several reasons: one, because the ChildControllers are not making use of the additional dependencies (so it causes constructor bloat in the child controllers for no reason), and more importantly, if I ever change the constructor signature of the Base Controller, I have to change the constructor signatures of each child controller.</p>
  
  <p><strong>2.) Implement the BaseController's dependencies via property injection</strong></p>
  
  <pre><code>public class BaseController : Controller
  {
      [Dependency]
      public IService Service { get; set; }
  
      public BaseController() { }
  }
  </code></pre>
  
  <p>I like this approach better - I'm not using any of the dependencies in the BaseController's constructor code - but it makes the dependency injection strategy of the code inconsistent, which isn't ideal either.</p>
  
  <p>There's probably an even better approach which involves some sort of BaseController dependency resolution function that calls on the Unity container to sort out the ctor's method signature, but before I get into writing anything too involved, I was wondering if anyone had solved this problem before? I found a few solutions floating around on the web, but they were workarounds such as Service Locator which I don't want to use.</p>
  
  <p>Thanks!</p>