Download - Dependency injection for beginners
![Page 1: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/1.jpg)
Dependency Injection for Beginnersfor Beginners
Bhushan MulmuleBhushan MulmuleBhushan MulmuleBhushan [email protected]
![Page 2: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/2.jpg)
![Page 3: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/3.jpg)
Dependency Injection is a software design
pattern that allows the removal of hard-
coded dependencies and makes it possible to coded dependencies and makes it possible to
change them, whether at run-time or
compile-time.
![Page 4: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/4.jpg)
Let us try to understand using simple exampleLet us try to understand using simple example
![Page 5: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/5.jpg)
We are developing bus ticket reservation system for client
BigBus.
![Page 6: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/6.jpg)
When development started client had pretty simple requirements
![Page 7: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/7.jpg)
One of the requirement was:
System should able to book seat. System should able to book seat.
And after booking confirmation email should go to customer.
![Page 8: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/8.jpg)
To send emails
we have created class EmailNotification EmailNotification EmailNotification EmailNotification with a we have created class EmailNotification EmailNotification EmailNotification EmailNotification with a method SendEmail()SendEmail()SendEmail()SendEmail()…………
![Page 9: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/9.jpg)
![Page 10: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/10.jpg)
And to Book Tickets
Created BookingBookingBookingBooking class with a method Book()Book()Book()Book()Created BookingBookingBookingBooking class with a method Book()Book()Book()Book()
![Page 11: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/11.jpg)
To send notification;Book() method is calling SendEmail() method of EmailNotification class.
![Page 12: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/12.jpg)
Here Booking class internally use EmailNotification class.
Such classes are said to be
tightly coupledtightly coupled.
[Change in one class can break other]
![Page 13: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/13.jpg)
Booking class can be consumed in application as follow...
![Page 14: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/14.jpg)
And the output will be…
![Page 15: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/15.jpg)
And we had happy client !!!
![Page 16: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/16.jpg)
But requirements keep changing…But requirements keep changing…
![Page 17: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/17.jpg)
Now client wanted to send Booking details to cell phone via SMS . details to cell phone via SMS .
![Page 18: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/18.jpg)
To fulfill this requirement we need to create new class
SMSNotificationSMSNotificationSMSNotificationSMSNotification
with method SendSMS()SendSMS()SendSMS()SendSMS()
![Page 19: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/19.jpg)
![Page 20: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/20.jpg)
And also need to modify class
BookingBookingBookingBooking to use SMSNotificationSMSNotificationSMSNotificationSMSNotificationBookingBookingBookingBooking to use SMSNotificationSMSNotificationSMSNotificationSMSNotification
instead of EmailNotification
![Page 21: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/21.jpg)
![Page 22: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/22.jpg)
Output
And System is up again with new changes in place...
Output
![Page 23: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/23.jpg)
Will be dealing with such change requestschange requeststhat easy in real life?
![Page 24: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/24.jpg)
System can have hundreds of tightly coupled classes. Because of that accommodating such changes can be time Because of that accommodating such changes can be time
consuming and frustrating.
![Page 25: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/25.jpg)
How to write
loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled System?
![Page 26: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/26.jpg)
Classes should always communicate
With each other viaWith each other via
Interfaces
![Page 27: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/27.jpg)
Let us write an interface INotification with a method declaration SendMessage()
![Page 28: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/28.jpg)
Implement interface in EmailNotification.
![Page 29: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/29.jpg)
Call method of EmailNotification from Booking using reference of INotification interface
![Page 30: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/30.jpg)
Using interfaces we have
Decoupled Decoupled Booking and EmailNotification classes.
![Page 31: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/31.jpg)
Still there is dependency of EmailNotification in Booking class. As we are creating object of EmailNotification inside Booking class.
![Page 32: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/32.jpg)
Can we move this dependency dependency
out of Booking class?
![Page 33: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/33.jpg)
And the answer isAnd the answer is
![Page 34: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/34.jpg)
Inversion of Control (IoC) or or
Dependency InjectionDependency InjectionDependency InjectionDependency Injection
![Page 35: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/35.jpg)
Instead of creating object of EmailNotification in Booking class we will pass it via constructorBooking class we will pass it via constructor
![Page 36: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/36.jpg)
Constructor of Booking class accepts object of class that implements INotification
![Page 37: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/37.jpg)
We have moved out dependency of Booking class outside of it.
And we will inject it to Booking class while creating And we will inject it to Booking class while creating its object.
![Page 38: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/38.jpg)
We are injecting object of EmailNotification to Booking class via constructor while creating its object.
![Page 39: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/39.jpg)
Now Booking class is completely independent of any implementation of INotificationimplementation of INotification
![Page 40: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/40.jpg)
Congratulations you have just done something known as
Dependency InjectionDependency InjectionDependency InjectionDependency InjectionDependency InjectionDependency InjectionDependency InjectionDependency Injection
Note: This was constructor injection same thing can also be achieved using property injection.
![Page 41: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/41.jpg)
Now if client will come to us with SMS requirement.
![Page 42: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/42.jpg)
We just need to create SMSNotification class, implementing INotification interface
![Page 43: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/43.jpg)
To use SMSNotification we just have to create object of Booking as follow…
![Page 44: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/44.jpg)
Now we are free to use any implementation of
INotificationINotificationwithout single modification in Booking class.
![Page 45: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/45.jpg)
Now Object of Booking class can be created...Now Object of Booking class can be created...Now Object of Booking class can be created...Now Object of Booking class can be created...Now Object of Booking class can be created...Now Object of Booking class can be created...Now Object of Booking class can be created...Now Object of Booking class can be created...
![Page 46: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/46.jpg)
like this…
or this…
![Page 47: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/47.jpg)
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.
![Page 48: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/48.jpg)
![Page 49: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/49.jpg)
Dependency Injection is a software design
pattern that allows the removal of hard-
coded dependencies and makes it possible to coded dependencies and makes it possible to
change them, whether at run-time or
compile-time.
![Page 50: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/50.jpg)
In Part – 2 we will see what is
Dependency Injection Container Dependency Injection Container and how to use it to move all the dependencies to it.
![Page 51: Dependency injection for beginners](https://reader034.vdocuments.us/reader034/viewer/2022052619/555e35e7d8b42aad058b4802/html5/thumbnails/51.jpg)
For video visitFor video visitFor video visitFor video visitwww.dotnetvideotutorial.comwww.dotnetvideotutorial.comwww.dotnetvideotutorial.comwww.dotnetvideotutorial.comwww.dotnetvideotutorial.comwww.dotnetvideotutorial.comwww.dotnetvideotutorial.comwww.dotnetvideotutorial.com
References: References: References: References: http://martinfowler.com/articles/injection.html http://martinfowler.com/articles/injection.html http://martinfowler.com/articles/injection.html http://martinfowler.com/articles/injection.html
Pro Pro Pro Pro ASP.NET MVCASP.NET MVCASP.NET MVCASP.NET MVC ---- ApressApressApressApress