Here I explain the use of an IDependencyResolver interface with ASP.NET MVC. The first question in my mind is that, when I begin learning about Dependency injection, what is the need of IDependencyResolver and after Googling I learned that somewhere in MVC, dependence on strict is not necessary, like in Common Service Locators. So, for the purpose of simplifying the requirements for service location/dependency resolution with MVC, "IDependencyResolver " is introduced. And since I have used and read about the advantages and reviews, use of Ninject IDependencyResolver for dependency resolving is the best option.
To begin learning more about "IDependencyResolver", first you shoud have an idea about Dependency Injection and Ninject.
What is Dependency Injection?
Dependency injection is a software design pattern that allows removal of hard-coded dependencies and making it possible to change them. Simply it's a concept to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface.
What is Ninject?
Ninject is a dependency injection framework for .NET applications that is very lightweight to use. It helps you to split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner.
It is free for both personal and commercial projects. It's also open source, so you can fork the code and make any changes you like.
To learn to work with Ninject for MVC, first you need to download and intall the extension (download it from here) that allows integration between the Ninject core and ASP.NET MVC projects. To use it, just make your HttpApplication extend NinjectHttpApplication. How?
After doing the same as above your controllers will be activated via Ninject, meaning you can expose dependencies on their constructors to request injections.
Now you are ready to configure your project.
Step 1
First you need to configure the Ninject container instance that is available to your application for a full lifetime and for configuration you need to download the NuGet package Ninject.Web.Common into your project. Now using the NinjectConfigurator class from the NinjectWebCommon.RegisterServices() method, register the type mappings in the Ninject container instance.
Step 2
Then your application (MVC) shoud know about this container instance to activate the controllers. So, for that you need to perform two main steps:
- Create an implementation of IDependencyResolverin that you use the Ninject container to resolve the requested dependencies.
- Register an instance of theIDependencyResolver implementation with MVC.
Step 3
Register the IDependencyResolver class with MVC.
Example Code
In the code above you see we use the TryGet() method, why? Because the NinjectDependencyResolver class will use the TryGet() method to resolve all dependencies.
Step 4
Now your IDependencyResolver class is ready, then you just need to register it with MVC using the following code:
That's it; you are now done, your MVC application will hit the configured Ninject container instance to resolve all dependencies needed during controller activation.