This is a question on a home work. I’ve been up and down my notes and I can’t even see how the two are related. Googling this gives me questions/answers on one or the other, but never both.
14
Dependency Injection is a technique for implementing Dependency Inversion.
The Inversion of Control page on Wikipedia actually says it a little better, even though Dependency Inversion and Inversion of Control may not be exactly the same thing.
Here is what it says:
Implementation Techniques
In object-oriented programming, there are several basic techniques to
implement inversion of control. These are:
- using a factory pattern
- using a service locator pattern
using a dependency injection, for example:
- Constructor injection
- Parameter injection
- Setter injection
- Interface injection
- using a contextualized lookup
- using Template method design pattern
- using Strategy design pattern
So no, Dependency Injection is not the only way to accomplish Dependency Inversion.
2
Answer: No. Here’s the difference:
Dependency Inversion is a principle.
High-level modules should not depend on low-level modules. Both should
depend on abstractions. Abstractions should not depend on details.
Details should depend on abstractions.
Dependency Injection is a technique, whereupon the dependencies — rather, their abstractions — are explicitly injected into an object, typically into the constructor, but sometimes exposed as properties. This can be done with Poor Man’s DI or with an IoC Container.
Example of Poor Man’s DI:
public class ConsumingClass
{
private readonly ISomeService SomeService;
public ConsumingClass(ISomeService someService)
{
SomeService = someService ?? new SomeServiceConcrete().
}
/* use SomeService elsewhere in the class */
}
Example of DI with IoC:
public class ConsumingClass
{
private readonly ISomeService SomeService;
public ConsumingClass(ISomeService someService)
{
SomeService = someService.
}
/* use SomeService elsewhere in the class */
}
The Service Locator Pattern, in contrast to Dependency Injection, requires the object to ask a service for its dependencies. This is considered an anti-pattern. The details of why is beyond the scope of the question and is left as an exercise for the reader.
Example of Service Locator:
public class ConsumingClass
{
private readonly ISomeService SomeService;
public ConsumingClass()
{
SomeService = SomeServiceLocator.GetInstance<ISomeService>();
}
/* use SomeService elsewhere in the class */
}
2
The wiki page on Dependency inversion principle tells :
Various patterns such as Plugin, Service Locator, or Dependency
Injection are then employed to facilitate the run-time provisioning of
the chosen low-level component implementation to the high-level
component.
Therefore, the answer is : dependency injection is a technique to apply the dependency inversion principle, but it is not the only one.
There are other techniques when you want to use this principle (the linked wiki page lists plugins and service locator as additional techniques).
The dependency injection is a specific kind of inversion of control in a way that you somehow inject a lower level object into higher level object. That way, the higher level object depends on an abstraction – and not on the concrete implementation of a lower level object. In other words, it access an object through it’s (usually abstract) interface.
5
The simple answer to your question is “No, Dependency Injection is not always present when Dependency Inversion is used.”
Simply put, “Dependency Inversion” is the principle and “Dependency Injection” is just one method of implementation. As noted in another answer, there are other implementations of the principle including Service Locators and Plug-In Frameworks.