di – ioc (ninject)
TRANSCRIPT
Dependency Injection (Ninject)Ruchir Shah
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.
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()
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
Dependency Injection
▪ Booking class can be consumed in application as follow:
Dependency Injection
▪ But requirements keep changing…▪ Now client wanted to send Booking details to cell phone
via SMS .
Dependency Injection
▪ To fulfill this requirement we need to create new class SMSNotification with method SendSMS()
Dependency Injection
▪ And also need to modify class Booking Booking to use SMSNotification SMSNotification instead of EmailNotification:
Dependency Injection
▪ And System is up again with new changes in place...
Dependency Injection
▪ Classes should always communicate With each other via Interfaces.
▪ Interface INotification with a method declaration SendMessage()
Dependency Injection
▪ Implement interface in EmailNotification.
Dependency Injection
▪ Call method of EmailNotification from Booking using reference of INotification interface.
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.
Dependency Injection
▪ Can we move this dependency out of Booking class?▪ Yes, with the help of
Inversion of Control (IoC)or
Dependency Injection
Dependency Injection
▪ Instead of creating object of EmailNotification in Booking class we will pass it via constructor.
Dependency Injection
▪ To create Object of Booking class in HomeController, we need to Inject object of EmailNotification to constructor of Booking.
Dependency Injection
▪ To create Object of Booking class in HomeController, we can also Inject object ofSMSNotification to constructor of Booking.
Dependency Injection
We have just achieved Dependency Injection!
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
Dependency Injection
DI container is about removing need ofthis object instantiation from client code.
Dependency Injection – Ninject
And to help developers from this situation we have…
Get started with Ninject
Install NuGet package for Ninject from Package Manager Console.
Get started with Ninject
Configure and setup Ninject to use
Modify HomeController to use Dependency Resolver
Dependency Injection – Ninject
▪ And to user SMS Notification, we just have to change binding in dependency resolver file as:
Dependency Injection