Published on March 6, 2014
NInject DI Container Bhushan Mulmule | firstname.lastname@example.org | http://dotnetvideotutorial.com
If you are new to DI Click here to view Dependency Injection for Beginners And then come back! If link will not work you can copy and paste following url: http://www.slideshare.net/bhushanmulmule/dependency-injection-for-beginners-31272832
Let us create sample ASP.NET MVC application to see DI in action We will be using ASP.NET MVC Application To Demonstrate NInject Follow the Walkthrough to create one
Create New ASP.NET MVC Project: DIContainerDemo
Select Empty Template and Razor View Engine
Add Three Classes and One Interface
Add New Controller: HomeController
To create Object of Booking class in HomeController Inject object of EmailNotification to constructor of Booking
Right Click on Index Method of HomeController Add View
And the Output will be…
To create Object of Booking class we can also Inject object of SMSNotification to constructor of Booking
Modified HomeController to use SMSNotification
Output will be…
DI container is about removing need of this object instantiation from client code.
Few of the DI Containers are Unity, Ninject, Autofac, StructureMap, Spring.NET
Installing NInject Or you can also download dll and add the reference
Steps to Set it up
Creating NInject kernel object IKernel kernel = new StandardKernel(); Binding interface with concrete class kernel.Bind<INotification>().To<EmailNotification>(); Getting object of bounded concrete class INotification notification = kernel.Get<INotification>();
Modified HomeController to use NInject
Output as expected…
Note: It will act as centralized location for all Ninject configuration.
Create Folder DIResolver Add class NInjectDependencyResover.cs
Register our Dependency Resolver in Gloabal.aspx
Modify HomeController to use DependencyResolver
Time to enjoy fruits of labor
Lets try to understand What's happening behind the scene
Steps break up…
Note that GetService() receives type parameter in this case it will be HomeController.
Client Request for index method of HomeController type HomeController MVC Ninject Dependecy Resolver
Client Request for index method of HomeController type HomeController MVC Ninject Dependecy Resolver type HomeController Ninject
Client Request for index method of HomeController type HomeController MVC Ninject Dependecy Resolver type HomeController Ninject Inspects constructor of HomeController for dependencies
Note: Here Ninject will create object of EmailNotification as we have bounded it to INotification in AddBinding() method. Same way it can be bounded to SMSNotification.
Client Request for index method of HomeController type HomeController MVC Object HomeController with Object EmailNotification Ninject Dependecy Resolver type HomeController Ninject Object HomeController With Object EmailNotification Inspects constructor of HomeController for dependencies
“Test Driven Development using Unit Testing”.
Find it out kernel.Bind<INotification>().To<SMSNotification>() .WithConstructorArgument("Param", "value"); kernel.Bind<INotification>().To<SMSNotification>() .WithPropertyValue("PropertyName", "Value"); kernel.Bind<INotification>().To<SMSNotification>() .When(//"lamda expression returning bool"); kernel.Bind<INotification>().To<SMSNotification>() .WhenClassHas</*attribute type*/>();
Few more… .WhenInjectedInto() .WhenMemberHas() .WhenTargetHas() .WhenAnyAnchestorNamed()
Credits: Apress Pro ASP.NET MVC 4
Thank You Bhushan Mulmule | email@example.com | http://dotnetvideotutorial.com
Ninject allows you to embrace change in your software's design, so it will be easier to adjust it to match changing business needs and expectations.
I want to share the container across various layers in my application. I started creating a static class which initialises the container and register types ...
The best place to learn about Ninject is from the documentation or the official ... May - Ninject & IoC Container Advanced Workshop; 8. Oct - Dependency ...
That covers up our migration from Poor Man’s DI to using Ninject as your IoC Container in ASP.NET MVC. For a sample project like ours, ...
If you do have experience with more than one DI/IoC container, please rank and summarise the pros and cons of ... [Ninject vs Unity for DI ] ...
Dependency injection for five-year-olds. When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door ...
This binding means that whenever Ninject encounters a dependency on IWeapon, it will resolve an instance of Sword and inject it. This dependency resolution ...
Autofac IoC container ... I'd loved to take a look at Ninject ... Looking over this 'complete' list reminds me of Ayende's concern that DI containers ...
Keywords : C# DI Container, IoC container for .net , popular IoC container for C# The terms Dependency Injection (DI ... Ninject. Easy to learn API
Added feature comparison. 25.04.2012: Updated to Ninject 3.0.015 and Petite 0.3.2. 14.05.2012: ... While performance for a DI container is important, ...