Motivation For Dependency Injection
Motivation For Dependency Injection
Nickleus Jimenez
Software Engineer
Philippines
The following cites benefits of using dependency injection for large scale software projects
1 Introduction
Dependency injection removes the dependence of class a on class by adding a container and making it
responsible for the dependency look up. The result when following the technique is decoupling the usage
of an object from its creation. The technique involves using interfaces to break the dependencies between
higher and lower level classes. As a result, both classes depend on the interface and no longer on each
other.
Applying dependency injection is an added layer of work for functionalities but there are reasons why
such effort can be worthwhile. The reasons cited by Betss et al.(2013) Those reasons mentioned are
Maintainability, testability, extensibility, late binding, parallel development and crosscutting concerns
3 Testability
The interfaces makes it possible to isolate tests to a certain method of a class since other classes are
injected with mock results so setting up the values for the method to be tested is possible. Depending on
interfaces enables testing individual parts of the system.
4 Extensibility
Systems that are designed using interfaces can be extended by adding new kinds of objects that implement
the interface without requiring any changes to existing code. Hence, Dependency injection’s use of
interfaces provides extensibility. The practice makes the system work in different ways or add features as
requirements changes.
5 Late Binding
Primary usefulness of late binding is the ability to replace part of your system without recompiling.
Executing dependency injection with xml or json config makes it possible for changing to another imple-
mentation when the need arises like when the database change from one kind of sql to another without
recompiling. Architecture that benefit the most from this is the plug-in architecture.
6 Parallel development
It’s not practical in large project to have the entire development team work simultaneously on the same
component or feature. Having the dev teams configure the dependency to use mock implementations
for classes that are being currently worked on by others makes it possible for them to focus on their
assigned components to implement. after that, others can reconfigure when they receive the completed
implementation.
7 Crosscutting Concerns
Cross-cutting concerns are parts of a system that rely on or must affect many other parts of the system.
Examples include logging, exception handling and validation. These features may be needed in many
different areas of the application. It is desirable that those features be implemented in a standard and
consistent way. When situation changes, reconfiguration for different set of implementations of those cross
cutting concerns can be done. Of course those have to be implemented but the contract from interfaces
will tell the developers what needs to be done.
Advantages Disadvantages
Allows a client application the flexibility of being Creates clients that require configuration details pro-
configurable. Only the client’s behavior is fixed. The vided by construction code which may onerous when
client may act on anything that supports the intrin- obvious defaults are available
sic interface the client expects or needs
Externalize a system’s configuration details into con- Makes code difficult to trace (read) without debug-
figuration files, allowing the system to be reconfig- ging because behavior is separated from construction
ured without recompilation. Separate configurations
can be used for different scenarios that require differ-
ent implementations of components. One example is
testing.
Does not require any change in code behavior it can developers must refer to more files to follow how a
be applied to legacy code as a refactoring. system performs
Isolate the client from the impact of design changes Usually requires more upfront development effort
and defects by removing all knowledge of a concrete since one can not summon into being something right
implementation that needed. when and where it is needed but must ask that it be
injected and then ensure that it has been injected.
Two developers can independently develop classes Forces complexity to move out of classes and into the
that use each other, while only needing to know the linkages between classes
interface the classes to be implemented or injected.
References
[1] Betts, D., Melnik, G., Simonazzi, F., Subramanian, M., and Tavares, C. (2013). Dependency Injection with
Unity. Microsoft Patterns Practices.