Dependency injection for beginners

52
Dependency Injection for Beginners Bhushan Mulmule Bhushan Mulmule Bhushan Mulmule Bhushan Mulmule [email protected] www.dotnetvideotutorial.com

description

Dependancy Injection in its simplest form. Helpful for beginners.

Transcript of Dependency injection for beginners

Page 1: Dependency injection for beginners

Dependency Injection for Beginnersfor Beginners

Bhushan MulmuleBhushan MulmuleBhushan MulmuleBhushan [email protected]

Page 2: Dependency injection for beginners
Page 3: Dependency injection for beginners

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

Let us try to understand using simple exampleLet us try to understand using simple example

Page 5: Dependency injection for beginners

We are developing bus ticket reservation system for client

BigBus.

Page 6: Dependency injection for beginners

When development started client had pretty simple requirements

Page 7: Dependency injection for beginners

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

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
Page 10: Dependency injection for beginners

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

To send notification;Book() method is calling SendEmail() method of EmailNotification class.

Page 12: Dependency injection for beginners

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

Booking class can be consumed in application as follow...

Page 14: Dependency injection for beginners

And the output will be…

Page 15: Dependency injection for beginners

And we had happy client !!!

Page 16: Dependency injection for beginners

But requirements keep changing…But requirements keep changing…

Page 17: Dependency injection for beginners

Now client wanted to send Booking details to cell phone via SMS . details to cell phone via SMS .

Page 18: Dependency injection for beginners

To fulfill this requirement we need to create new class

SMSNotificationSMSNotificationSMSNotificationSMSNotification

with method SendSMS()SendSMS()SendSMS()SendSMS()

Page 19: Dependency injection for beginners
Page 20: Dependency injection for beginners

And also need to modify class

BookingBookingBookingBooking to use SMSNotificationSMSNotificationSMSNotificationSMSNotificationBookingBookingBookingBooking to use SMSNotificationSMSNotificationSMSNotificationSMSNotification

instead of EmailNotification

Page 21: Dependency injection for beginners
Page 22: Dependency injection for beginners

Output

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

Output

Page 23: Dependency injection for beginners

Will be dealing with such change requestschange requeststhat easy in real life?

Page 24: Dependency injection for beginners

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

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

Classes should always communicate

With each other viaWith each other via

Interfaces

Page 27: Dependency injection for beginners

Let us write an interface INotification with a method declaration SendMessage()

Page 28: Dependency injection for beginners

Implement interface in EmailNotification.

Page 29: Dependency injection for beginners

Call method of EmailNotification from Booking using reference of INotification interface

Page 30: Dependency injection for beginners

Using interfaces we have

Decoupled Decoupled Booking and EmailNotification classes.

Page 31: Dependency injection for beginners

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

Can we move this dependency dependency

out of Booking class?

Page 33: Dependency injection for beginners

And the answer isAnd the answer is

Page 34: Dependency injection for beginners

Inversion of Control (IoC) or or

Dependency InjectionDependency InjectionDependency InjectionDependency Injection

Page 35: Dependency injection for beginners

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

Constructor of Booking class accepts object of class that implements INotification

Page 37: Dependency injection for beginners

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

We are injecting object of EmailNotification to Booking class via constructor while creating its object.

Page 39: Dependency injection for beginners

Now Booking class is completely independent of any implementation of INotificationimplementation of INotification

Page 40: Dependency injection for beginners

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

Now if client will come to us with SMS requirement.

Page 42: Dependency injection for beginners

We just need to create SMSNotification class, implementing INotification interface

Page 43: Dependency injection for beginners

To use SMSNotification we just have to create object of Booking as follow…

Page 44: Dependency injection for beginners

Now we are free to use any implementation of

INotificationINotificationwithout single modification in Booking class.

Page 45: Dependency injection for beginners

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

like this…

or this…

Page 47: Dependency injection for beginners

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
Page 49: Dependency injection for beginners

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

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

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

Page 52: Dependency injection for beginners

Thank you

Bhushan MulmuleBhushan MulmuleBhushan MulmuleBhushan [email protected]