We can do DI in MVC either by implementing IDependencyResolver, or extending DefaultControllerFactory
I used to think that there was not much difference between these two different ways to do it.
However, I'm finishing my MVC book and it has me implement my own ControllerFactory (instead of extending the default), and in the CreateController method, it actually has:
(IController) DependencyResolver.Current.GetService(targetType);
So it looks like DefaultControllerFactory actually uses the DependencyResolver
There has to be a difference between the two, and I think it's just confusing me.
Questions
1) Is the book having me use the dependency resolver in order to simplify their implementation of a CustomControllerFactory, and the actual DefaultControllerFactory doesn't use it?
2) I'm having a hard time understanding the purposes of these two. I used to think it was just two different ways to implement DI, but the more I dig in the more I get a feeling they're completely different. It looks like the dependency resolver is where all the controllers get instantiated
3) Is there a best practice which trying to choose between the two? Perhaps some pro's and cons?
EDIT: For clarity, I've decided to upload the whole CreateController method:
public IController CreateController(RequestContext requestContext, string controllerName)
{
Type targetType = null;
switch (controllerName)
{
case "Product":
targetType = typeof (ProductController);
break;
case "Customer":
targetType = typeof (CustomerController);
break;
default:
requestContext.RouteData.Values["controller"] = "Product";
targetType = typeof (ProductController);
break;
}
return targetType == null ? null : (IController) DependencyResolver.Current.GetService(targetType);
}