NInject - DI Container

69
NInject DI Container Bhushan Mulmule | [email protected] | http://dotnetvideotutorial.com

description

Simple NInject demonstration using ASP.NET MVC application.

Transcript of NInject - DI Container

Page 1: NInject - DI Container

NInjectDI Container

Bhushan Mulmule | [email protected] | http://dotnetvideotutorial.com

Page 2: NInject - DI Container

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

Page 3: NInject - DI Container

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

Page 4: NInject - DI Container

Create New ASP.NET MVC Project: DIContainerDemo

Page 5: NInject - DI Container

Select Empty Template and Razor View Engine

Page 6: NInject - DI Container

Add Three Classes and One Interface

Page 7: NInject - DI Container

INotification

Page 8: NInject - DI Container

EmailNotification

Page 9: NInject - DI Container

SMSNotification

Page 10: NInject - DI Container

Booking

Page 11: NInject - DI Container

Add New Controller: HomeController

Page 12: NInject - DI Container

Inject object of EmailNotification

to constructor of Booking

To create Object of Booking

class in HomeController

Page 13: NInject - DI Container

HomeController

Page 14: NInject - DI Container

Right Click on Index Method of HomeController Add View

Page 15: NInject - DI Container

Modify Views/Home/Index.cshtml

Page 16: NInject - DI Container

Final Structure

Page 17: NInject - DI Container

And the Output will be…

Page 18: NInject - DI Container

Inject object of SMSNotification

to constructor of Booking

To create Object of Booking

class we can also

Page 19: NInject - DI Container

Modified HomeController to use SMSNotification

Page 20: NInject - DI Container

Output will be…

Page 21: NInject - DI Container

Dependency Injection!

Page 22: NInject - DI Container
Page 23: NInject - DI Container

Problems…

Page 24: NInject - DI Container
Page 25: NInject - DI Container
Page 26: NInject - DI Container
Page 27: NInject - DI Container

DI Container

Page 28: NInject - DI Container

DI container is about removing need of

this object instantiation from client code.

Page 29: NInject - DI Container

Unity, Ninject, Autofac,

StructureMap, Spring.NET

Few of the DI Containers are

Page 30: NInject - DI Container

NInject

Page 31: NInject - DI Container

Or you can also download dll and add the reference

Installing NInject

Page 32: NInject - DI Container

Steps to Set it up

Page 33: NInject - DI Container

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();

Page 34: NInject - DI Container

Modified HomeController to use NInject

Page 35: NInject - DI Container

Output as expected…

Page 36: NInject - DI Container
Page 37: NInject - DI Container

Note: It will act as centralized location for all Ninject configuration.

Page 38: NInject - DI Container

Create Folder DIResolver Add class NInjectDependencyResover.cs

Page 39: NInject - DI Container

NInjectDependencyResolver.cs

Page 40: NInject - DI Container
Page 41: NInject - DI Container

Register our Dependency Resolver in Gloabal.aspx

Page 42: NInject - DI Container

Modify HomeController to use DependencyResolver

Page 43: NInject - DI Container

Time to enjoy fruits of labor

Page 44: NInject - DI Container
Page 45: NInject - DI Container
Page 46: NInject - DI Container

Lets try to understand

What's happening behind the scene

Page 47: NInject - DI Container
Page 48: NInject - DI Container
Page 49: NInject - DI Container
Page 50: NInject - DI Container

Steps break up…

Page 51: NInject - DI Container
Page 52: NInject - DI Container

Note that GetService() receives type parameter in this case it will be HomeController.

Page 53: NInject - DI Container

MVCNinject

DependecyResolver

type HomeController

Client Request for index

method of HomeController

Page 54: NInject - DI Container
Page 55: NInject - DI Container

MVCNinject

DependecyResolver

Ninject

type HomeController type HomeController

Client Request for index

method of HomeController

Page 56: NInject - DI Container
Page 57: NInject - DI Container

MVCNinject

DependecyResolver

Ninject

type HomeController type HomeController

Client Request for index

method of HomeController

Inspects constructor of

HomeController for dependencies

Page 58: NInject - DI Container

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.

Page 59: NInject - DI Container
Page 60: NInject - DI Container
Page 61: NInject - DI Container
Page 62: NInject - DI Container

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

Page 63: NInject - DI Container

“Test Driven Development using Unit

Testing”.

Page 64: NInject - DI Container

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*/>();

Page 65: NInject - DI Container

.WhenInjectedInto()

.WhenMemberHas()

.WhenTargetHas()

.WhenAnyAnchestorNamed()

Few more…

Page 67: NInject - DI Container

Credits:

Apress Pro

ASP.NET MVC 4

Page 68: NInject - DI Container

http://dotnetvideotutorial.com

Page 69: NInject - DI Container

Thank YouBhushan Mulmule | [email protected] | http://dotnetvideotutorial.com