Why does all dependency injection in ASP.NET MVC3 happen at the controller level instead of at the action level. Controller creation is typically overridden in order to inject dependencies when the controller is instantiated. However, the controller is only instantiated as the result of a request for an action. Why not handle the dependency injection at the action level?
Why does all dependency injection in ASP.NET MVC3 happen at the controller level instead
Share
Because this is a known pattern and the hooks are in place in MVC to inject into a controller, not an action. There is a controller factor, but not an action factory. You create an instance of the controller, not the method, so thats where the injection needs to take place.
Plus theres a known pattern of constructor injection which would be more appropriate here than some other method (i.e. action method) injection and it also allows any other setup in your constructor that may be necessary.