di – ioc (ninject)

26
Dependency Injection (Ninject) Ruchir Shah

Upload: zealousysdev

Post on 13-Apr-2017

249 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Di – ioc (ninject)

Dependency Injection (Ninject)Ruchir Shah

Page 2: Di – ioc (ninject)

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)

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)

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)

Dependency Injection

▪ Booking class can be consumed in application as follow:

Page 6: Di – ioc (ninject)

Dependency Injection

▪ But requirements keep changing…▪ Now client wanted to send Booking details to cell phone

via SMS .

Page 7: Di – ioc (ninject)

Dependency Injection

▪ To fulfill this requirement we need to create new class SMSNotification with method SendSMS()

Page 8: Di – ioc (ninject)

Dependency Injection

▪ And also need to modify class Booking Booking to use SMSNotification SMSNotification instead of EmailNotification:

Page 9: Di – ioc (ninject)

Dependency Injection

▪ And System is up again with new changes in place...

Page 10: Di – ioc (ninject)

Dependency Injection

▪ Classes should always communicate With each other via Interfaces.

▪ Interface INotification with a method declaration SendMessage()

Page 11: Di – ioc (ninject)

Dependency Injection

▪ Implement interface in EmailNotification.

Page 12: Di – ioc (ninject)

Dependency Injection

▪ Call method of EmailNotification from Booking using reference of INotification interface.

Page 13: Di – ioc (ninject)

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)

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)

Dependency Injection

▪ Instead of creating object of EmailNotification in Booking class we will pass it via constructor.

Page 16: Di – ioc (ninject)

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)

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)

Dependency Injection

We have just achieved Dependency Injection!

Page 19: Di – ioc (ninject)

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)

Dependency Injection

DI container is about removing need ofthis object instantiation from client code.

Page 21: Di – ioc (ninject)

Dependency Injection – Ninject

And to help developers from this situation we have…

Page 22: Di – ioc (ninject)

Get started with Ninject

Install NuGet package for Ninject from Package Manager Console.

Page 23: Di – ioc (ninject)

Get started with Ninject

Configure and setup Ninject to use

Page 24: Di – ioc (ninject)

Modify HomeController to use Dependency Resolver

Page 25: Di – ioc (ninject)

Dependency Injection – Ninject

▪ And to user SMS Notification, we just have to change binding in dependency resolver file as:

Page 26: Di – ioc (ninject)

Dependency Injection