Dependency injection for beginners

Post on 22-May-2015

1.009 views 2 download

Tags:

description

Dependancy Injection in its simplest form. Helpful for beginners.

Transcript of Dependency injection for beginners

Dependency Injection for Beginnersfor Beginners

Bhushan MulmuleBhushan MulmuleBhushan MulmuleBhushan Mulmulebhushan.mulmule@gmail.comwww.dotnetvideotutorial.com

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.

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

We are developing bus ticket reservation system for client

BigBus.

When development started client had pretty simple requirements

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.

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()…………

And to Book Tickets

Created BookingBookingBookingBooking class with a method Book()Book()Book()Book()Created BookingBookingBookingBooking class with a method Book()Book()Book()Book()

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

Here Booking class internally use EmailNotification class.

Such classes are said to be

tightly coupledtightly coupled.

[Change in one class can break other]

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

And the output will be…

And we had happy client !!!

But requirements keep changing…But requirements keep changing…

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

To fulfill this requirement we need to create new class

SMSNotificationSMSNotificationSMSNotificationSMSNotification

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

And also need to modify class

BookingBookingBookingBooking to use SMSNotificationSMSNotificationSMSNotificationSMSNotificationBookingBookingBookingBooking to use SMSNotificationSMSNotificationSMSNotificationSMSNotification

instead of EmailNotification

Output

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

Output

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

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.

How to write

loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled loosely coupled System?

Classes should always communicate

With each other viaWith each other via

Interfaces

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

Implement interface in EmailNotification.

Call method of EmailNotification from Booking using reference of INotification interface

Using interfaces we have

Decoupled Decoupled Booking and EmailNotification classes.

Still there is dependency of EmailNotification in Booking class. As we are creating object of EmailNotification inside Booking class.

Can we move this dependency dependency

out of Booking class?

And the answer isAnd the answer is

Inversion of Control (IoC) or or

Dependency InjectionDependency InjectionDependency InjectionDependency Injection

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

Constructor of Booking class accepts object of class that implements INotification

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.

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

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

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.

Now if client will come to us with SMS requirement.

We just need to create SMSNotification class, implementing INotification interface

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

Now we are free to use any implementation of

INotificationINotificationwithout single modification in Booking class.

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...

like this…

or this…

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.

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.

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.

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

Thank you

Bhushan MulmuleBhushan MulmuleBhushan MulmuleBhushan Mulmulebhushan.mulmule@gmail.comwww.dotnetvideotutorial.com