di – ioc (ninject)
TRANSCRIPT
![Page 1: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/1.jpg)
Dependency Injection (Ninject)Ruchir Shah
![Page 2: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/2.jpg)
What is Dependency Injection?
▪ Dependency Injection is a software design pattern that allows the removal of hard coded dependencies and makes it possible to change them, whether at run-time or compile-time.
![Page 3: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/3.jpg)
Dependency Injection
▪ 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 we have created class Email Notification with a method SendEmail()
![Page 4: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/4.jpg)
Dependency Injection
▪ To send notification; Book() method is calling SendEmail() method of EmailNotification class.
▪ Here Booking class internally use EmailNotificationclass. Such classes are said to be tightly coupled
![Page 5: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/5.jpg)
Dependency Injection
▪ Booking class can be consumed in application as follow:
![Page 6: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/6.jpg)
Dependency Injection
▪ But requirements keep changing…▪ Now client wanted to send Booking details to cell phone
via SMS .
![Page 7: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/7.jpg)
Dependency Injection
▪ To fulfill this requirement we need to create new class SMSNotification with method SendSMS()
![Page 8: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/8.jpg)
Dependency Injection
▪ And also need to modify class Booking Booking to use SMSNotification SMSNotification instead of EmailNotification:
![Page 9: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/9.jpg)
Dependency Injection
▪ And System is up again with new changes in place...
![Page 10: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/10.jpg)
Dependency Injection
▪ Classes should always communicate With each other via Interfaces.
▪ Interface INotification with a method declaration SendMessage()
![Page 11: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/11.jpg)
Dependency Injection
▪ Implement interface in EmailNotification.
![Page 12: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/12.jpg)
Dependency Injection
▪ Call method of EmailNotification from Booking using reference of INotification interface.
![Page 13: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/13.jpg)
Dependency Injection
▪ 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 Booking class.
![Page 14: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/14.jpg)
Dependency Injection
▪ Can we move this dependency out of Booking class?▪ Yes, with the help of
Inversion of Control (IoC)or
Dependency Injection
![Page 15: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/15.jpg)
Dependency Injection
▪ Instead of creating object of EmailNotification in Booking class we will pass it via constructor.
![Page 16: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/16.jpg)
Dependency Injection
▪ To create Object of Booking class in HomeController, we need to Inject object of EmailNotification to constructor of Booking.
![Page 17: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/17.jpg)
Dependency Injection
▪ To create Object of Booking class in HomeController, we can also Inject object ofSMSNotification to constructor of Booking.
![Page 18: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/18.jpg)
Dependency Injection
We have just achieved Dependency Injection!
![Page 19: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/19.jpg)
Dependency Injection
But there are 2 problems…1. We are manually injecting dependency into Booking class
by providing the implementation for INotification interface.2. We are creating EmailNotification or SMSNotification in
HomeController; making HomeController tightly coupled with these classes.
But there problems can be resolved using…
DI Container
![Page 20: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/20.jpg)
Dependency Injection
DI container is about removing need ofthis object instantiation from client code.
![Page 21: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/21.jpg)
Dependency Injection – Ninject
And to help developers from this situation we have…
![Page 22: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/22.jpg)
Get started with Ninject
Install NuGet package for Ninject from Package Manager Console.
![Page 23: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/23.jpg)
Get started with Ninject
Configure and setup Ninject to use
![Page 24: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/24.jpg)
Modify HomeController to use Dependency Resolver
![Page 25: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/25.jpg)
Dependency Injection – Ninject
▪ And to user SMS Notification, we just have to change binding in dependency resolver file as:
![Page 26: Di – ioc (ninject)](https://reader036.vdocuments.net/reader036/viewer/2022062401/58a2f25d1a28ab5d1c8b5075/html5/thumbnails/26.jpg)
Dependency Injection