I'm studying the dependency injection pattern. I have read many examples, among which one typical example is to use XxxService/XxxRepository as example. But from my opinion, according to the UML concept, class XxxRepository should be an association of class XxxService. Why not call this situation Association Injection, but still Dependency Injection?:)
Thanks!
UPDATE ON 1/26/2018
I currently think the concept of dependency injection
is suitable for the situation described in this question. Because Association is just a special Dependency in UML.
Please refer to this article, Martin Fowler said:
"a dependency exists between two elements if changes to the definition of one element (the supplier) may cause changes to the other (the client)".
This is a very vague and general relationship, which is why the UML
has a host of stereotypes for different forms of dependency.
and
Associations also imply dependency, if there is an association between two classes, there is also a dependency.
So I cannot accept any answers right now. Or maybe this question is not a good question because every developer has his owned viewpoint. I'm seriously considering to close this question.
Because dependency injection is defined for coding not for design.
You can code an association with or without injection, but it is an association.
For example, in Java, the following codes show an association between a classe A and a class B implementing interface IB.
class A{
@Inject
private IB b;
...
}
or
Class A{
private IB b = new B();
...
}
First of all: I want to clarify some hints about Dependency Injection.
In Dependency Injection (see reference (4th paragraph)):
- The client does not need to know how to construct the services.
- The client does not need to know which actual services it is using.
- The client only needs to know about the intrinsic interfaces of the services because these define how the client may use the services.
When we are using the dependency injection like below code:
class A {
@Inject
private IB b;
//...
}
We do not actually know, which object of which class (that implements IB
) was injected to b
.
Maybe the injected class changes at runtime, maybe it changes because of some configurations that handled manually or automatically. Maybe it changes dynamically as period of time and etc.
So, we can not use an association between class A
and other class (which class? or which object?)
Therefore, there is only dependency between class A
and interface IB
.
In small projects, maybe we have only one implementation of IB
in our project. Or we do not have any mechanism to have dynamic injections. In this case, association may be used. However, in this case, there is no need to Dependency Injection.
To find good explanation from Martin Fowler and good example, see reference 2.
According to wikipedia:
Dependency injection is a technique whereby one object supplies the
dependencies of another object. (...) Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.
Note that the injection does not have to take place at construction, even if it often does.
A dependency relation is unidirectional: one object depends on another. The dependency injection has the goal of promoting one direction over the other (for example for inverting a dependency)
Associations are more general than dependencies: it can be unidirectional or bidirectional, depending on its navigability. So in most of the case, "injecting an association" would say nothing about the preferred direction: once the association is there, it could still be used in the wrong way.