SlideShare a Scribd company logo
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!
NInject - DI Container
Problems…
NInject - DI Container
NInject - DI Container
NInject - DI Container
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…
NInject - DI Container
Note: It will act as centralized location for all Ninject configuration.
Create Folder DIResolver  Add class
NInjectDependencyResover.cs
NInjectDependencyResolver.cs
NInject - DI Container
Register our Dependency Resolver in Gloabal.aspx
Modify HomeController to use DependencyResolver
Time to enjoy fruits of labor
NInject - DI Container
NInject - DI Container
Lets try to understand

What's happening behind the scene
NInject - DI Container
NInject - DI Container
NInject - DI Container
Steps break up…
NInject - DI Container
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
NInject - DI Container
Client Request
for index
method of
HomeController

type HomeController
MVC

Ninject
Dependecy
Resolver

type HomeController
Ninject
NInject - DI Container
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.
NInject - DI Container
NInject - DI Container
NInject - DI Container
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()
NInject - DI Container
Credits:

Apress Pro
ASP.NET MVC 4
http://dotnetvideotutorial.com
Thank You
Bhushan Mulmule | bhushan.mulmule@gmail.com | http://dotnetvideotutorial.com

More Related Content

PDF
An introduction to Angular2
PPTX
Angular 2 KTS
PDF
React native app with type script tutorial
PPTX
Angularjs Basics
PDF
Angular2 with TypeScript
PPTX
Angular Presentation
PPTX
Creating custom Validators on Reactive Forms using Angular 6
PDF
Angular 2.0 - What to expect
An introduction to Angular2
Angular 2 KTS
React native app with type script tutorial
Angularjs Basics
Angular2 with TypeScript
Angular Presentation
Creating custom Validators on Reactive Forms using Angular 6
Angular 2.0 - What to expect

What's hot (20)

PPT
React js
PPT
jQuery Plugin
PPTX
Top 10 Mistakes AngularJS Developers Make
PPTX
Rethinking Best Practices
PDF
React.js+Redux Workshops
PDF
React on es6+
PDF
Angular 2 - An Introduction
PPTX
AngularJS (1.x) as fast as a lightning
PDF
Deep Dive into React Hooks
PPTX
Angularjs Anti-patterns
ODP
Angularjs
PPTX
Introduction to React JS for beginners
PDF
3 Simple Steps to follow to Create React JS Components
PPT
Google Guice
PPTX
AngularJs
PDF
React for Dummies
PDF
An introduction to React.js
PPTX
Introduction to Google Guice
PDF
Best Practice-React
PDF
Testing React hooks with the new act function
React js
jQuery Plugin
Top 10 Mistakes AngularJS Developers Make
Rethinking Best Practices
React.js+Redux Workshops
React on es6+
Angular 2 - An Introduction
AngularJS (1.x) as fast as a lightning
Deep Dive into React Hooks
Angularjs Anti-patterns
Angularjs
Introduction to React JS for beginners
3 Simple Steps to follow to Create React JS Components
Google Guice
AngularJs
React for Dummies
An introduction to React.js
Introduction to Google Guice
Best Practice-React
Testing React hooks with the new act function
Ad

Similar to NInject - DI Container (20)

PPTX
Di – ioc (ninject)
PDF
Dependency injection for beginners
PPTX
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
PPTX
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
PPTX
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
PPTX
Clean Code II - Dependency Injection
PDF
Dependency Injection
PPTX
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
PDF
Dependency Injection with Unity Container
PPTX
Oleksandr Valetskyy - DI vs. IoC
PPTX
Dependency Injection and Autofac
PPTX
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
PPTX
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
PPTX
Dependency injection
PPTX
Clean Code Part II - Dependency Injection at SoCal Code Camp
PPTX
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
PDF
What is Dependency Injection in Spring Boot | Edureka
PPTX
I gotta dependency on dependency injection
PPTX
Dependency Injection або Don’t call me, I’ll call you
ODP
Dependency Injection, Zend Framework and Symfony Container
Di – ioc (ninject)
Dependency injection for beginners
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection
Dependency Injection
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Dependency Injection with Unity Container
Oleksandr Valetskyy - DI vs. IoC
Dependency Injection and Autofac
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Dependency injection
Clean Code Part II - Dependency Injection at SoCal Code Camp
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
What is Dependency Injection in Spring Boot | Edureka
I gotta dependency on dependency injection
Dependency Injection або Don’t call me, I’ll call you
Dependency Injection, Zend Framework and Symfony Container
Ad

More from Bhushan Mulmule (15)

PDF
Implementing auto complete using JQuery
PDF
Windows Forms For Beginners Part 5
PDF
Windows Forms For Beginners Part - 4
PDF
Windows Forms For Beginners Part - 3
PDF
Windows Forms For Beginners Part - 2
PDF
Windows Forms For Beginners Part - 1
PPTX
Understanding Interfaces
PPTX
Polymorphism
PPTX
Inheritance
PPTX
Classes and objects
PPTX
PPTX
Arrays, Structures And Enums
PPTX
Flow Control (C#)
PPTX
Getting started with C# Programming
PPTX
Overview of .Net Framework 4.5
Implementing auto complete using JQuery
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 1
Understanding Interfaces
Polymorphism
Inheritance
Classes and objects
Arrays, Structures And Enums
Flow Control (C#)
Getting started with C# Programming
Overview of .Net Framework 4.5

Recently uploaded (20)

PPTX
Tartificialntelligence_presentation.pptx
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Machine Learning_overview_presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Tartificialntelligence_presentation.pptx
SOPHOS-XG Firewall Administrator PPT.pptx
OMC Textile Division Presentation 2021.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation_ Review paper, used for researhc scholars
Assigned Numbers - 2025 - Bluetooth® Document
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine Learning_overview_presentation.pptx
A Presentation on Artificial Intelligence
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
A comparative study of natural language inference in Swahili using monolingua...
A comparative analysis of optical character recognition models for extracting...
Diabetes mellitus diagnosis method based random forest with bat algorithm

NInject - DI Container