What Is Dependency Injection?

Article Details
  • Written By: Alex Newth
  • Edited By: Angela B.
  • Last Modified Date: 06 October 2019
  • Copyright Protected:
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The average American has around 60 "bad days" a year; lack of sleep is the biggest contributing factor.  more...

October 20 ,  1973 :  The "Saturday Night Massacre"  more...

Dependency injection is a technique used in object-oriented programming (OOP). This technique references, or connects, with another program and makes the original program dependent on the referenced program. This allows programmers to instantly add functions or extra features to the original program, but also makes coding more complicated and adds excessive coding that may slow down the program. Most OOP programs allow programmers to reference a variety of installed programs without needing to add code.

The use of dependency injection is often sparing in most programs. To make the program dependent, the programmer writes code that references another program, either in part or whole. Normally, any installed program can be referenced, but some coding schemas allow only specific programs to be referenced. For example, if the programmer wants to add text capabilities to the program, instead of making the feature by hand, he or she can reference a word processing program. By doing this, the original program now has a new feature but is dependent on that other program working and remaining installed on the computer.

Most new programmers prefer to use dependency injection instead of building features by hand. That is because building features can be complicated and new programmers may not know how to effectively make the feature. By referencing a program, the programmer does not need to know the coding, and any feature can be added with just a few lines of code.


There are many problems with dependency injection, especially if the program is meant for consumers, which is why it is often unused or used sparingly by professionals and software providers. One of the most obvious problems with dependency injection on the consumer side is the possibility that the consumer may not have the referenced program. If the program is constructed to reference a feature in Program A, but the consumer does not have Program A installed, the feature or the entire program will encounter a debilitating error.

At high levels of dependency, where several programs are referenced, other problems manifest. Referencing one or two programs creates a small amount of code, but doing more can necessitate excessive code that slows down the program. If the program has an error, there is now a large amount of complicated coding that the programmer has to dive through to find the error, especially because the error may be with the referenced program and not the original program.

Many new programmers use dependency injection, so most OOP schemas offer an automatic way of referencing a program. This allows the programmer, with just a few mouse clicks, to make the reference connection. Automatically referencing a program often adds slightly more coding than writing the reference by hand, but since the programmer is new and the program will most likely not go to consumers, this should not present any serious problems.


You might also Like


Discuss this Article

Post your comments

Post Anonymously


forgot password?