Dependency Injection
for Beginners
Bhushan Mulmule
bhushan.mulmule@gmail.com
www.dotnetvideotutorial.com
Dependency Injection is a software design
pattern that allows the removal of hardcoded dependencies and makes it possible ...
Let us try to understand using simple example
We are developing bus ticket
reservation system for client
BigBus.
When development started client had pretty simple requirements
One of the requirement was:
System should able to book seat.
And after booking confirmation

email should go to customer.
To send emails
we have created class EmailNotification with a
method SendEmail()…
SendEmail()…
And to Book Tickets
Created Booking class with a method Book()
To send notification; Book() method is calling SendEmail()
method of EmailNotification class.
Here Booking class internally use EmailNotification class.
Such classes are said to be

tightly coupled.
[Change in one cl...
Booking class can be consumed in application as follow...
And the output will be…
And we had happy client !!!
But requirements keep changing…
Now client wanted to send Booking
details to cell phone via SMS .
To fulfill this requirement we need to create new class

SMSNotification
with method SendSMS()
And also need to modify class

Booking to use SMSNotification
instead of EmailNotification
And System is up again with new changes in place...
Output
Will be dealing with such
change requests
that easy in real life?
System can have hundreds of tightly coupled classes.
Because of that accommodating such changes can be time
consuming and ...
How to write

loosely coupled
System?
Classes should always
communicate
With each other via

Interfaces
Let us write an interface INotification with a method
declaration SendMessage()
Implement interface in EmailNotification.
Call method of EmailNotification from Booking using
reference of INotification interface
Using interfaces we have

Decoupled
Booking and EmailNotification classes.
Still there is dependency of EmailNotification in Booking class.
As we are creating object of EmailNotification inside Boo...
Can we move this

dependency

out of Booking class?
And the answer is
Inversion of Control (IoC)
or
Dependency Injection
Instead of creating object of EmailNotification in
Booking class we will pass it via constructor
Constructor of Booking class accepts object of class that implements INotification
We have moved out dependency of Booking class
outside of it.
And we will inject it to Booking class while creating
its obj...
We are injecting object of EmailNotification to Booking class
via constructor while creating its object.
Now Booking class is completely independent of any
implementation of INotification
Congratulations you have just done something known as
Dependency Injection

Note: This was constructor injection same thin...
Now if client will come to us with SMS requirement.
We just need to create SMSNotification class,
implementing INotification interface
To use SMSNotification we just have to create object of Booking as follow…
Now we are free to use any implementation of

INotification
without single modification in Booking class.
Now Object of Booking class can be created...
like this…

or this…
DI is also very helpful for

unit testing.
If we wanted to test Booking class and don’t want to worry about EmailNotificat...
Dependency Injection is a software design
pattern that allows the removal of hardcoded dependencies and makes it possible ...
In Part – 2 we will see what is

Dependency Injection Container
and how to use it to move all the dependencies to it.
For video visit

www.dotnetvideotutorial.com

References:
http://martinfowler.com/articles/injection.html
Pro ASP.NET MVC ...
Thank you
Bhushan Mulmule
bhushan.mulmule@gmail.com
www.dotnetvideotutorial.com
Dependency injection for beginners
Dependency injection for beginners
Dependency injection for beginners
Dependency injection for beginners
Dependency injection for beginners
Nächste SlideShare
Wird geladen in …5
×

Dependency injection for beginners

1.807 Aufrufe

Veröffentlicht am

Dependancy Injection in its simplest form. Helpful for beginners.

Veröffentlicht in: Technologie
1 Kommentar
9 Gefällt mir
Statistik
Notizen
Keine Downloads
Aufrufe
Aufrufe insgesamt
1.807
Auf SlideShare
0
Aus Einbettungen
0
Anzahl an Einbettungen
99
Aktionen
Geteilt
0
Downloads
81
Kommentare
1
Gefällt mir
9
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie

Dependency injection for beginners

  1. 1. Dependency Injection for Beginners Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com
  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 www.dotnetvideotutorial.com References: http://martinfowler.com/articles/injection.html Pro ASP.NET MVC - Apress
  47. 47. Thank you Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com

×