NInject - DI Container
-
Upload
bhushan-mulmule -
Category
Technology
-
view
871 -
download
1
description
Transcript of NInject - DI Container
NInjectDI Container
Bhushan Mulmule | [email protected] | http://dotnetvideotutorial.com
I f you are new to DI Cl ick here to view
Dependency Inject ion 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
Inject object of EmailNotification
to constructor of Booking
To create Object of Booking
class in HomeController
HomeController
Right Click on Index Method of HomeController Add View
Modify Views/Home/Index.cshtml
Final Structure
And the Output will be…
Inject object of SMSNotification
to constructor of Booking
To create Object of Booking
class we can also
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.
Unity, Ninject, Autofac,
StructureMap, Spring.NET
Few of the DI Containers are
NInject
Or you can also download dll and add the reference
Installing NInject
Steps to Set it up
Getting object of bounded concrete class
INotification notification = kernel.Get<INotification>();
Binding interface with concrete class
kernel.Bind<INotification>().To<EmailNotification>();
Creating NInject kernel object
IKernel kernel = new StandardKernel();
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.
MVCNinject
DependecyResolver
type HomeController
Client Request for index
method of HomeController
MVCNinject
DependecyResolver
Ninject
type HomeController type HomeController
Client Request for index
method of HomeController
MVCNinject
DependecyResolver
Ninject
type HomeController type HomeController
Client Request for index
method of HomeController
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.
MVCNinject
DependecyResolver
Ninject
type HomeController type HomeController
Object HomeControllerWith
Object EmailNotification
Object HomeControllerwith
Object EmailNotification
Client Request for index
method of HomeController
Inspects constructor of
HomeController for dependencies
“Test Driven Development using Unit
Testing”.
kernel.Bind<INotification>().To<SMSNotification>().WithConstructorArgument("Param", "value");
kernel.Bind<INotification>().To<SMSNotification>().WithPropertyValue("PropertyName", "Value");
Find it out
kernel.Bind<INotification>().To<SMSNotification>().When(//"lamda expression returning bool");
kernel.Bind<INotification>().To<SMSNotification>().WhenClassHas</*attribute type*/>();
.WhenInjectedInto()
.WhenMemberHas()
.WhenTargetHas()
.WhenAnyAnchestorNamed()
Few more…
Credits:
Apress Pro
ASP.NET MVC 4
http://dotnetvideotutorial.com
Thank YouBhushan Mulmule | [email protected] | http://dotnetvideotutorial.com