Difference between DI implementation by extending DefaultControllerFactory versus implementation of IDependencyResolver

We can do DI in MVC either by implementing IDependencyResolver, or by extending DefaultControllerFactory

I used to think that there wasn’t much difference between the two different ways.

However, I am finishing my MVC book, and I have an implementation of my own ControllerFactory (instead of the default extension), and the CreateController method actually has:

(IController) DependencyResolver.Current.GetService(targetType);

So it looks like DefaultControllerFactory is using DependencyResolver

There should be a difference between the two, and I think it just baffles me.

Questions

1) Is the book in which I use the dependency tool to simplify the implementation of CustomControllerFactory, and the actual DefaultControllerFactory does not use it?

2) It is difficult for me to understand the goals of these two. I used to think that these are just two different ways to implement DI, but the more I dig, the more I get the feeling that they are completely different. It looks like the dependency converter is where all the controllers get the instance

3) Is there any best practice that is trying to choose between the two? Perhaps some pro and disadvantages?

EDIT . For clarity, I decided to load the entire 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);
  }
+4
source share
1

DefaultControllerFactory.

IDependencyResolver - Locator Service, IMO MVC ( Web API, ). , , , .

, Service Locator DI, , Service Locator -, , . , , DI .

+6

Source: https://habr.com/ru/post/1532887/


All Articles