Your SlideShare is downloading. ×
0
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
NInject - DI Container
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

NInject - DI Container

878

Published on

Simple NInject demonstration using ASP.NET MVC application.

Simple NInject demonstration using ASP.NET MVC application.

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
878
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
27
Comments
1
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  1. NInject DI Container Bhushan Mulmule | bhushan.mulmule@gmail.com | http://dotnetvideotutorial.com
  2. 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
  3. 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
  4. Create New ASP.NET MVC Project: DIContainerDemo
  5. Select Empty Template and Razor View Engine
  6. Add Three Classes and One Interface
  7. INotification
  8. EmailNotification
  9. SMSNotification
  10. Booking
  11. Add New Controller: HomeController
  12. To create Object of Booking class in HomeController Inject object of EmailNotification to constructor of Booking
  13. HomeController
  14. Right Click on Index Method of HomeController  Add View
  15. Modify Views/Home/Index.cshtml
  16. Final Structure
  17. And the Output will be…
  18. To create Object of Booking class we can also Inject object of SMSNotification to constructor of Booking
  19. Modified HomeController to use SMSNotification
  20. Output will be…
  21. Dependency Injection!
  22. Problems…
  23. DI Container
  24. DI container is about removing need of this object instantiation from client code.
  25. Few of the DI Containers are Unity, Ninject, Autofac, StructureMap, Spring.NET
  26. NInject
  27. Installing NInject    Or you can also download dll and add the reference  
  28. Steps to Set it up
  29. 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>();
  30. Modified HomeController to use NInject
  31. Output as expected…
  32. Note: It will act as centralized location for all Ninject configuration.
  33. Create Folder DIResolver  Add class NInjectDependencyResover.cs
  34. NInjectDependencyResolver.cs
  35. Register our Dependency Resolver in Gloabal.aspx
  36. Modify HomeController to use DependencyResolver
  37. Time to enjoy fruits of labor
  38. Lets try to understand What's happening behind the scene
  39. Steps break up…
  40. Note that GetService() receives type parameter in this case it will be HomeController.
  41. Client Request for index method of HomeController type HomeController MVC Ninject Dependecy Resolver
  42. Client Request for index method of HomeController type HomeController MVC Ninject Dependecy Resolver type HomeController Ninject
  43. Client Request for index method of HomeController type HomeController MVC Ninject Dependecy Resolver type HomeController Ninject Inspects constructor of HomeController for dependencies
  44. 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.
  45. 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
  46. “Test Driven Development using Unit Testing”.
  47. 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*/>();
  48. Few more… .WhenInjectedInto() .WhenMemberHas() .WhenTargetHas() .WhenAnyAnchestorNamed()
  49. Credits: Apress Pro ASP.NET MVC 4
  50. http://dotnetvideotutorial.com
  51. Thank You Bhushan Mulmule | bhushan.mulmule@gmail.com | http://dotnetvideotutorial.com

×