Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyResolver vs. ControllerFactory

ASP.NET MVC 3 introduced DependencyResolver and I saw a lot of articles how cool it is. But wait... what is the difference comparing to ControllerFactory approach? Looks pretty similar for me.

like image 449
SiberianGuy Avatar asked Jul 24 '11 23:07

SiberianGuy


1 Answers

The ServiceLocator is designed as a layer of abstraction that will allow you to implement your own ServiceLocator Adapter which determines how services are resolved within your applications. One such example is the Windsor Service Locator Adapter.

The service locator has the ability to resolve controllers registered with the service locator according to Brad Wilson:

This is a new feature for MVC 3. The MVC framework (specifically, the DefaultControllerFactory class) has been updated to attempt to create all controller instances with the registered service locator.

The service locator can also be used throughout the framework to resolve a lot of other dependencies that the ASP.NET MVC framework uses like dependencies required by ActionFilters, ResultHandlers and even ViewEngines.

If you make all your instances of your controllers available, for a standard app, it's highly unlikely that you would need a custom controller factory.

However, if you need to do something specific before, during or after the instantiation of your controller, you can use the controllerfactory to take care of these specific implementations before it is returned for use by the rest of the framework.

like image 192
lomaxx Avatar answered Oct 07 '22 22:10

lomaxx