NInject
DI Container
Bhushan Mulmule | bhushan.mulmule@gmail.com | 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
INotification
EmailNotification
SMSNotification
Booking
Add New Controller: HomeController
To create Object of Booking
class in HomeController

Inject object of EmailNotification
to constructor of Booking
HomeController
Right Click on Index Method of HomeController 
Add View
Modify Views/Home/Index.cshtml
Final Structure
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…
Dependency Injection!
Problems…
DI Container
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
NInject
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
NInjectDependencyResolver.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
http://dotnetvideotutorial.com
Thank You
Bhushan Mulmule | bhushan.mulmule@gmail.com | http://dotnetvideotutorial.com

NInject - DI Container