Aim: This article explains about how to implement “NInject”
framework in MVC application to resolve object dependencies. There are three types of Injection available in NInject, 1.
Constructor Injection 2.
Property Setter Injection 3.
Method Injection
In this project, we will look into “Constructor” Injection pattern with easy steps, Step 1: Create empty MVC Project Step 2: Right click the project à
Click on “Manage NuGet Packages” à
Click on “Browse” and Type “NInject” and search 1.
Install NInject 2.
Install NInject.MVC5 a.
This will install the below components in your
solution
i.
NInject.Web.Common.3.3.0
ii.
NInject.Web.Common.WebHost.3.3.0
iii.
NInject.MVC5.3.3.0
Step3: 1.
Global.asax Changes:
a.
Remove “System.Web.HttpApplication”
and Inherit “NinjectHttpApplication”
as mentioned below. public class MvcApplication : NinjectHttpApplication { // App Start Methods should
come here. } b.
Remove “Application_Start”
and override the “OnApplicationStarted”
Method inside it and place its registration methods. protected override void OnApplicationStarted()
{ base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); DependencyResolver.SetResolver(DependencyResolver.Current.ToServiceResolver()); } c.
Override “CreateKernel”
Method and load the required assemblies. protected override IKernel
CreateKernel() { var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly(), Assembly.Load("NInjectSample.Models")); kernel.Load(Assembly.GetExecutingAssembly(),
Assembly.Load("NInjectSample.Business")); kernel.Load(Assembly.GetExecutingAssembly(),
Assembly.Load("NInjectSample.Common")); return kernel; } Step 4: 1.
Create Dependency Mapper Class by overriding the
NInjectModule’s à
Load Method public class DependencyMapper : NinjectModule { public override void Load() { this.Bind<IRegistrationManager>().To<RegistrationManager>(); } } Step 5: 1.
Create “Resolver” Adapter by implementing “IDependencyResolver” to resolve object
at run time. public class ServiceResolverAdapter : IDependencyResolver
{
private readonly System.Web.Mvc.IDependencyResolver
dependencyResolver;
public ServiceResolverAdapter( System.Web.Mvc.IDependencyResolver
dependencyResolver)
{ if (dependencyResolver == null) { throw new ArgumentNullException("dependencyResolver");
} this.dependencyResolver =
dependencyResolver;
}
public object GetService(Type serviceType)
{ return
dependencyResolver.GetService(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{ return
dependencyResolver.GetServices(serviceType);
} }
2.
Create Service Resolver Extension class public static class ServiceResolverExtensions { public static IDependencyResolver ToServiceResolver( this
System.Web.Mvc.IDependencyResolver dependencyResolver) { return new ServiceResolverAdapter(dependencyResolver); } }
3.
Register it in the Global.asax à
“OnApplicationStarted” Method protected override void OnApplicationStarted() { base.OnApplicationStarted(); AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); DependencyResolver.SetResolver(DependencyResolver.Current.ToServiceResolver());
}
Step 6: 1.
Decorate Controller’s Constructor with [Inject] attribute to inject the
required objects based on the registration done in the “DependencyMapper” class.
[Inject]
public HomeController(IRegistrationManager
registrationManager)
{ this._registrationManager =
registrationManager;
} |