NInject - DI Container

50 %
50 %
Information about NInject - DI Container

Published on March 6, 2014

Author: bhushanmulmule



Simple NInject demonstration using ASP.NET MVC application.

NInject DI Container Bhushan Mulmule | |

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:

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

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!


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


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 | |

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Ninject - Open source dependency injector for .NET

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.
Read more

Ninject: Shared DI/IoC container - Stack Overflow

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 ...
Read more

Learn more about Ninject - Ninject - Open source ...

The best place to learn about Ninject is from the documentation or the official ... May - Ninject & IoC Container Advanced Workshop; 8. Oct - Dependency ...
Read more

Dependency Injection Using Ninject In ASP.NET MVC

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, ...
Read more

dependency injection - How do the major C# DI/IoC ...

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 ] ...
Read more

Dependency injection - Wikipedia, the free encyclopedia

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 ...
Read more

Dependency Injection With Ninject · ninject/Ninject Wiki ...

This binding means that whenever Ninject encounters a dependency on IWeapon, it will resolve an instance of Sword and inject it. This dependency resolution ...
Read more

List of .NET Dependency Injection Containers (IOC) - Scott ...

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 ...
Read more

What is IoC Container or DI Container - Dot Net Tricks ...

Keywords : C# DI Container, IoC container for .net , popular IoC container for C# The terms Dependency Injection (DI ... Ninject. Easy to learn API
Read more

IoC Container Benchmark - Performance comparison - www ...

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, ...
Read more