关联注入还是依赖注入? [英] Association Injection or Dependency Injection?

查看:101
本文介绍了关联注入还是依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究依赖项注入模式.我已经阅读了许多示例,其中一个典型示例是使用XxxService/XxxRepository作为示例.但是从我的角度来看,根据UML概念,类XxxRepository应该是类XxxService的关联.为什么不将这种情况称为 Association 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?:)

谢谢!

2018年1月6日更新

我目前认为dependency injection的概念适用于此问题中描述的情况.因为关联在UML中只是特殊的依赖项.

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.

请参阅这篇文章,马丁·福勒(Martin Fowler)说:

Please refer to this article, Martin Fowler said:

如果一个元素(供应商)的定义更改可能导致另一个元素(客户)的更改,则两个元素之间存在依赖关系". 这是一个非常模糊和普遍的关系,这就是为什么UML 对于不同形式的依赖有很多刻板印象.

"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.

关联也意味着依赖,如果两个类之间存在关联,则也存在依赖.

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.

推荐答案

首先:我想澄清一些有关依赖注入的提示.

First of all: I want to clarify some hints about Dependency Injection.

在依赖注入中(请参阅参考(第4段)):

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;

    //...
}

我们实际上并不知道哪个类(实现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.

因此,我们不能在类A和其他类(哪个类?或哪个对象?)之间使用关联

So, we can not use an association between class A and other class (which class? or which object?)

因此,类A和接口IB之间仅存在依赖关系.

Therefore, there is only dependency between class A and interface IB.

在小型项目中,也许我们的项目中只有一个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.

要从Martin Fowler找到好的解释和好的例子,请参见参考2 .

To find good explanation from Martin Fowler and good example, see reference 2.

这篇关于关联注入还是依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆