14

I am new to ASP.NET MVC 4. I have used a custom dependency resolver in my ASP.NET MVC 4 project in order to use Dependency injection framework.

What is the role of dependency resolver in ASP.NET MVC 4 ?

Aslam Jiffry
  • 1,306
  • 6
  • 23
  • 57
  • so you used it without knowing what it does? Why not stop using it? – evhen14 Nov 20 '13 at 02:56
  • 1
    I have used using global.asax.cs file to register the custom dependency resolver. But in global.asax.cs file I didn't find any registering about default dependency resolver. – Aslam Jiffry Nov 20 '13 at 02:59

2 Answers2

24

It allows you to abstract away from Dependency Injection implementation. Later if you decide to switch from Unity to Windsor, you can do it much easier without having to re-write lots of code

It means that you can resolve your instances using this code

DependencyResolver.Current.GetService<IMyController>();
evhen14
  • 1,839
  • 12
  • 16
  • 1
    lets say what if we didn't use a dependency injection framework. how default dependency resolver going to work. – Aslam Jiffry Nov 20 '13 at 03:02
  • 1
    It should not work. For example, for controllers it will use Default Controller Factory that creates controllers using their default constructors – evhen14 Nov 20 '13 at 03:05
  • @evhen14, I think DefaultControllerfactory uses DependencyResolver to create controller instances. – user1176058 Jul 17 '14 at 21:18
0

I use a different approach using Ninject

  1. Using NuGet I downloaded Ninject(just it without any special modification for MVC). So you will have Ninject in references.
  2. Now I create a custom controller factory class (class derived from DefaultControllerFactory).My goal is to make MVC use my controller factory when it tries to create a controller object.

    public class NinjectControllerFactory : DefaultControllerFactory
    {
    
        #region Member Variables
    
        private IKernel ninjectKernel = null;
    
        #endregion
    
        public NinjectControllerFactory(IKernel kernel)
        {
            this.ninjectKernel = kernel;
            AddBindings();
        }
    
        private void AddBindings()
        {
            //BO
            ninjectKernel.Bind<IAuthenticationBO>().To<AuthenticationBO>();
    
            //DAO
            ninjectKernel.Bind<ISharedDAO>().To<SharedDAO>();
    
        }
    
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }
    }
    
  3. Make MVC to use my custom controller factory. In Global.asax in Application_Start()

    public class MvcApplication : System.Web.HttpApplication
    {
        private IKernel kernel = new StandardKernel();
    
        protected void Application_Start()
        {
    
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
            //register a cutom controller factory
            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(kernel));                       
    
        }
    }
    

So now when MVC creates controller objects it uses our custom controller factory and as you saw it resolves all dependencies using Ninject.

For example

public class MainController : Controller
{
#region Member Variables

   private IAuthenticationBO authentication = null;

#endregion

   public MainController(IAuthenticationBO authentication)
   {
       this.authentication = authentication;
   }
}

Ninject injects the implementation of IAuthenticationBO (in our case AuthenticationBO) and we can use it. Also it's very easy to use mocking and TDD, but it's beyond the scope of the question.

Anton
  • 731
  • 4
  • 5