FCI-Helwan blog

Just another FCI-H weblog

Dependency Injection

What is Dependency injection?
A software concept at which when an object relies on another one in its functionality the using object is not responsible for knowing how the needed object will be available . In other words; when an object have a reference to another object as a member variable, it is not responsible for determining how that reference will be available.

Example:
Suppose the next fragment of code:

public class car
{
public Engine engine;

Public void GO(int Acceleration_Factor)
{
this.Engine.Start();
this.Engine.Accelerate(Acceleration_Factor);
}
}

public class Engine
{
public SparkPlug sparkplug;

public void Start()
{
sparkplug.getElectricCharge();
sparkplug.startspark();
—–;
——;
——-;
}
}

It is clear that we are demonstrating a simple car operation which consists of an engine with a simple spark plug. ok, lets review this case in terms of code. When you instantiate an object of type car, you have to supply it with an object of type engine which in turn needs an object of type spark plug which might need another object of another type and so on.

Imagine a way to let you just specify the characteristics of your objects and the creation is some one else’s concern, imagine that when you create the car object, the engine object is created and ready for use which in turn have a spark plug object created and ready for use.
doesn’t make sense yet?? Ok, lets see this scenario.

Suppose you need to unit test the previously mentioned example, a test case will be written, so all what you need is to test the functionality of the car object assuming it has more references to objects rather than an engine. When you write this kind of test, you have to successfully create an engine object, which requires the creation of a spark plug object, those last creations are totally out of scope for testing the car object. In this case, all what you need is some one who creates these objects for you in the right way to make sure that you are only testing the car object, That “some one” is the Dependency injection frame work.

Frameworks like spring offer this feature, all what you do is specifying what your business objects look like in an XML file, and the framework is responsible for supplying any other objects using your objects. It might not be clear that this is an amazing feature, but in enterprise applications, relations between objects get very complicated and dependencies might be cyclic at some points, where an object depend on another which in turn depend on another which depends on the first one. In real cases; dependency injection become very useful, you just declare your variables and generate their accessors and the frame work does the rest.

The importance of dependency injection might not be clear by now, but when you start writing code with this trend you will notice the difference.

Advertisements

October 27, 2007 - Posted by | Concepts

1 Comment »

  1. Very greate article, thank you

    Comment by mhewedy | February 21, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: