Dependency injection for beginners

77 %
23 %
Information about Dependency injection for beginners
Technology

Published on February 17, 2014

Author: bhushanmulmule

Source: slideshare.net

Description

Dependancy Injection in its simplest form. Helpful for beginners.

Dependency Injection for Beginners Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com

Dependency Injection is a software design pattern that allows the removal of hardcoded dependencies and makes it possible to change them, whether at run-time or compile-time.

Let 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. And after booking confirmation email should go to customer.

To send emails we have created class EmailNotification with a method SendEmail()… SendEmail()…

And to Book Tickets Created Booking class with a method 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 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…

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

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

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

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

Will be dealing with such change requests that easy in real life?

System can have hundreds of tightly coupled classes. Because of that accommodating such changes can be time consuming and frustrating.

How to write loosely coupled System?

Classes should always communicate With 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 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 out of Booking class?

And the answer is

Inversion of Control (IoC) or Dependency Injection

Instead of creating object of EmailNotification in Booking 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 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 INotification

Congratulations you have just done something known as Dependency 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 INotification without single modification in Booking class.

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 hardcoded 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 and how to use it to move all the dependencies to it.

For video visit www.dotnetvideotutorial.com References: http://martinfowler.com/articles/injection.html Pro ASP.NET MVC - Apress

Thank you Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com

Add a comment

Related presentations

Related pages

Dependency Injection For Beginner - I - CodeProject

Introduction . In this Article I am going to discuss about "Dependency injection" on which I am working and exploring. In this article I am going to ...
Read more

A beginners guide to Dependency Injection

This article presents a high level overview of Dependency Injection (DI). It aims to present the overall concept of Dependency Injection to the junior ...
Read more

An Absolute Beginner's Tutorial on Dependency Inversion ...

An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection
Read more

dependency injection - IOC framework (.net) for beginners ...

... I found Ninject to be the most intuitive dependency injection framework, ... I obviously don't have the ability to choose a good beginner framework, ...
Read more

Dependency Injection: A Beginner’s Guide | Steves Coding ...

This article intends to outline the concept of Dependency Injection and its uses within a software engineering project. From Wikipedia.org: “it is a ...
Read more

Spring Framework 4 And Dependency Injection For Beginners ...

Spring Framework 4 Getting Started And Dependency Injection Fundamentals Tutorial For Real World Application Development - Free Course
Read more

c# - Beginner on Dependency Injection - Stack Overflow

So we have a class that does needs to output the result of an operation. Now this was tightly-coupled to emails, however with dependency injection I ...
Read more

A Beginner’s Tutorial on Dependency Inversion, Inversion ...

In this article we will talk about the Dependency Inversion Principle, Inversion of Control and Dependency Injection. We will start by looking at the...
Read more

Dependency Injection - Software Development ...

Dependency Injection (DI) has been a hot topic since 2007-2008. Lots have already been said and written about depencency injection, yet I feel there is ...
Read more