37

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:

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;
    }
}

This is understandably throwing an error of 'BaseController' does not contain a constructor that takes 0 arguments. 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:

1.) Explicitly call the BaseController ctor and have each ChildController ctor inject the BaseController's dependencies

public class ChildController : BaseController
{
    private readonly IDifferentService _differentService;

    public ChildController(IDifferentService differentService,
                           IService baseService)
        : base(baseService)
    {
        _differentService = differentService;
    }
}

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.

2.) Implement the BaseController's dependencies via property injection

public class BaseController : Controller
{
    [Dependency]
    public IService Service { get; set; }

    public BaseController() { }
}

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.

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.

Thanks!

3 Answers 3

35

The first thing you have to understand is that you aren't instantiating the base controller. You're instantiating the child controller, which inherits the base controllers interface and functionality. This is an important distinction. When you say "the ChildControllers are not making use of the additional dependencies", then you're absolutely wrong. Because the ChildController IS the BaseController as well. There aren't two different classes created. Just one class that implements both functionality.

So, since ChildController IS A BaseController, there is nothing wrong or strange about passing parameters in the child controllers constructor that calls the base classes constructor. This is the way it should be done.

If you change your base class, you will likely have to change your child classes anyways. There is no way to use constructor injection to inject base class dependencies that are not included in the child class.

I do not recommend property injection, since this means your objects can be created without proper initialization, and you have to remember to configure them correctly.

BTW, the proper terms are Subclass and Superclass. A "child" is a subclass, the parent is the "superclass".

6
  • 1
    You're right! I added a base controller as a solution to needing shared functionality without fully considering the OO implications. I think what I'll do is refactor a bit to ensure that the base dependencies are nice and tidy, and pass those through the child constructors as you suggest. Thanks Eric!
    – NWard
    Commented Feb 21, 2014 at 7:39
  • Perhaps this is the right answer here, but it's unfortunately that, while I can enclose the initialization of code within the base class, each derived class has to implement the correct constructor. I have to remember to type in that same constructor for each derived class. And any change the data I'm passing to the constructor requires changing every class that derives from it. It would be far better to be able to maintain this solely from the base class. Commented Dec 1, 2014 at 1:35
  • @JonathanWood - That argument is not particularly compelling, since constructors are there for a reason... You want to initialize the object with specific data. If you "forget" to include the right constructor, then the task you want to accomplish won't work because there won't be a constructor that you need and you will quickly notice your mistake. It's like complaining that you have to "remember" to add properties that you need to a class... you won't get very far if you don't remember. Commented Dec 1, 2014 at 3:34
  • 2
    @JonathanWood - what you call an "approach" is the way all object oriented languages that i'm aware of work, and since this is.. by definition "the object oriented approach", I don't quite understand what you mean. To my knowledge, all object oriented languages that implement inheritance are implemented in a manner in which the derived class is the only instance of the object, and thus there are no alternate instances that something like Unity or any other DI container could inject a value in if the derived class itself does not implement that constructor. Commented Dec 1, 2014 at 5:19
  • 1
    @JonathanWood - I think what you seem to be suggesting is that base class constructors should be exposed by default, which is really a very different thing from what the original poster was asking for. If that is what you mean, then I suggest you read any of the many articles out there about why constructors are not inherited in C++, Java, and other langauges (including C#). FWIW, C++-11 has added a feature that allows the compiler to automatically create matching constructors in the base classes, but this is not quite the same thing. Commented Dec 1, 2014 at 5:32
20

With ASP.Net 5 and it's built in DI

public class BaseController : Controller
{
    protected ISomeType SomeMember { get; set; }

    public BaseController(IServiceProvider serviceProvider)
    {
        //Init all properties you need
        SomeMember = (SomeMember)serviceProvider.GetService(typeof(ISomeMember));
    }
}

public class MyController : BaseController  
{
public MyController(/*Any other injections goes here*/, 
                      IServiceProvider serviceProvider) : 
 base(serviceProvider)
{}
}

UPDATE

There is also an extension method in Microsoft.Extensions.DependencyInjection to make it shorter

SomeMember = serviceProvider.GetRequiredService<ISomeMember>();
11
  • This is a great addition. Is it possible to implement a base controller without DI in ASP.NET 4.5? Commented Jan 25, 2016 at 19:11
  • Not sure what you mean. You could use Factory+Singleton instead of DI in BaseController.
    – Vitaly
    Commented Jan 26, 2016 at 16:02
  • yea, disregard. I left off Namespace Controllers. This project I'm working on does not have DI implimented. Commented Jan 26, 2016 at 17:27
  • Good one, goes well with MVC 6 Commented Apr 27, 2016 at 13:39
  • 1
    MVC 5 is used and MS DI. I've solved my issue by adding public HomeController() : this(DependencyInjectionResolver.ServiceProvider) { } etc
    – anatol
    Commented Jan 12, 2018 at 10:52
0

I've taken a somewhat different (but to me, fairly obvious and probably common) approach. It works for me, but there may be pitfalls I'm not aware of.

I have created common service properties in my BaseController class that most of my controllers use. They get instantiated when they are needed/referenced.

If there's a service a particular controller needs that is less used, I inject it into that controller's constructor as normal.

using JIS.Context;
using JIS.Managers;
using JIS.Models;
using JIS.Services;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

namespace JIS.Controllers
{
    public class BaseController : Controller
    {
        public BaseController()
        {
            ViewBag.User = UserManager?.User;
        }

        private IUserManager userManager;
        public IUserManager UserManager
        {
            get
            {
                if (userManager == null)
                {
                    userManager = DependencyResolver.Current.GetService<IUserManager>();
                }
                return userManager;
            }
            set
            {
                userManager = value;
            }
        }

        private ILoggingService loggingService;
        public ILoggingService LoggingService
        {
            get
            {
                if (loggingService == null)
                {
                    loggingService = DependencyResolver.Current.GetService<ILoggingService>();
                }
                return loggingService;
            }
            set { loggingService = value; }
        }

        private IUserDirectory userDirectory;
        public IUserDirectory UserDirectory
        {
            get
            {
                if (userDirectory == null)
                {
                    userDirectory = DependencyResolver.Current.GetService<IUserDirectory>();
                }
                return userDirectory;
            }
            set { userDirectory = value; }
        }

        private ApplicationDbContext appDb;
        public ApplicationDbContext AppDb
        {
            get
            {
                if (appDb == null)
                {
                    appDb = new ApplicationDbContext();
                }
                return appDb;
            }
            set
            {
                appDb = value;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && appDb != null)
            {
                appDb.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

In my controller I simply subclass from this BaseController:

public class UsersController : BaseController
{
    // and any other services I need are here:
    private readonly IAnotherService svc;
    public UsersController(IAnotherService svc)
    {
        this.svc = svc;
    }
...
}

This way, common services are generated on the fly when they're needed, and are available to my controllers without a lot of boilerplate.

2
  • 1
    This is using the Service Locator pattern which is generally considered an anti-pattern.
    – Dave Black
    Commented Aug 16, 2019 at 20:40
  • @DaveBlack it is, but only as a fallback mechanism. I agree however, that this can become un-testable (too many things to mock) Commented Feb 12 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.