Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Association Injection or Dependency Injection?

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.

like image 360
walsh Avatar asked Oct 29 '22 21:10

walsh


2 Answers

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(); ... }

like image 100
granier Avatar answered Nov 03 '22 04:11

granier


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.

like image 36
Gholamali-Irani Avatar answered Nov 03 '22 05:11

Gholamali-Irani