Dependency injection for beginners

1.779 Aufrufe

Veröffentlicht am

Dependancy Injection in its simplest form. Helpful for beginners.

Veröffentlicht in: Technologie
1 Kommentar
8 Gefällt mir
Keine Downloads
Aufrufe insgesamt
Auf SlideShare
Aus Einbettungen
Anzahl an Einbettungen
Gefällt mir
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie

Dependency injection for beginners

  1. 1. Dependency Injection for Beginners Bhushan Mulmule
  2. 2. Dependency Injection is a software design pattern that allows the removal of hardcoded dependencies and makes it possible to change them, whether at run-time or compile-time.
  3. 3. Let us try to understand using simple example
  4. 4. We are developing bus ticket reservation system for client BigBus.
  5. 5. When development started client had pretty simple requirements
  6. 6. One of the requirement was: System should able to book seat. And after booking confirmation email should go to customer.
  7. 7. To send emails we have created class EmailNotification with a method SendEmail()… SendEmail()…
  8. 8. And to Book Tickets Created Booking class with a method Book()
  9. 9. To send notification; Book() method is calling SendEmail() method of EmailNotification class.
  10. 10. Here Booking class internally use EmailNotification class. Such classes are said to be tightly coupled. [Change in one class can break other]
  11. 11. Booking class can be consumed in application as follow...
  12. 12. And the output will be…
  13. 13. And we had happy client !!!
  14. 14. But requirements keep changing…
  15. 15. Now client wanted to send Booking details to cell phone via SMS .
  16. 16. To fulfill this requirement we need to create new class SMSNotification with method SendSMS()
  17. 17. And also need to modify class Booking to use SMSNotification instead of EmailNotification
  18. 18. And System is up again with new changes in place... Output
  19. 19. Will be dealing with such change requests that easy in real life?
  20. 20. System can have hundreds of tightly coupled classes. Because of that accommodating such changes can be time consuming and frustrating.
  21. 21. How to write loosely coupled System?
  22. 22. Classes should always communicate With each other via Interfaces
  23. 23. Let us write an interface INotification with a method declaration SendMessage()
  24. 24. Implement interface in EmailNotification.
  25. 25. Call method of EmailNotification from Booking using reference of INotification interface
  26. 26. Using interfaces we have Decoupled Booking and EmailNotification classes.
  27. 27. Still there is dependency of EmailNotification in Booking class. As we are creating object of EmailNotification inside Booking class.
  28. 28. Can we move this dependency out of Booking class?
  29. 29. And the answer is
  30. 30. Inversion of Control (IoC) or Dependency Injection
  31. 31. Instead of creating object of EmailNotification in Booking class we will pass it via constructor
  32. 32. Constructor of Booking class accepts object of class that implements INotification
  33. 33. We have moved out dependency of Booking class outside of it. And we will inject it to Booking class while creating its object.
  34. 34. We are injecting object of EmailNotification to Booking class via constructor while creating its object.
  35. 35. Now Booking class is completely independent of any implementation of INotification
  36. 36. Congratulations you have just done something known as Dependency Injection Note: This was constructor injection same thing can also be achieved using property injection.
  37. 37. Now if client will come to us with SMS requirement.
  38. 38. We just need to create SMSNotification class, implementing INotification interface
  39. 39. To use SMSNotification we just have to create object of Booking as follow…
  40. 40. Now we are free to use any implementation of INotification without single modification in Booking class.
  41. 41. Now Object of Booking class can be created...
  42. 42. like this… or this…
  43. 43. DI is also very helpful for unit testing. If we wanted to test Booking class and don’t want to worry about EmailNotification or SMSNotification we can create mock implementation of INotification and pass it to Booking class object during the test. Will discuss it in Unit Testing session.
  44. 44. Dependency Injection is a software design pattern that allows the removal of hardcoded dependencies and makes it possible to change them, whether at run-time or compile-time.
  45. 45. In Part – 2 we will see what is Dependency Injection Container and how to use it to move all the dependencies to it.
  46. 46. For video visit References: Pro ASP.NET MVC - Apress
  47. 47. Thank you Bhushan Mulmule