NInject - DI Container

1,337 views

Published on

Simple NInject demonstration using ASP.NET MVC application.

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
No Downloads
Views
Total views
1,337
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
32
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

NInject - DI Container

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

×