IoC and ASP.NET MVC Controllers

Do I have to do something about this? I think of all my controllers inheriting from BaseController. Does this mean IoC design disruption? What else should I do instead?

public class BaseController: Controller
{
    protected ICookieService CookieService {
        get {
            return ServiceResolver.Resolve<ICookieService>(new { HttpContext = HttpContext });
        }
    }
    protected IDateTimeService DateTimeService { 
        get {
            return ServiceResolver.Resolve<IDateTimeService>();
        }
    }
    protected ISettingsService SettingsService {
        get {
            return ServiceResolver.Resolve<ISettingsService>();
        }
    }

}
+5
source share
3 answers

It would be much easier to go with the installation of the constructor, and you have a controllerfactory. If you can, do not use a service locator (your ServiceResolver) if you can avoid constructor injection.

There is information about it on Adding a factory controller to ASP MVC

, StructureMap, , Unity, .

+10

, . IoC . , , . BaseController, , IoC , .

0

Inversion of control - Driven Development - , ( : - ).

, , IoC, .

Discard the existing integration between the Autofac IoC container and ASP.NET MVC (other containers should also do this).

0
source

All Articles