加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖注入 – 控制反转与依赖注入与选定的引号 – 我的理解正确吗

发布时间:2020-12-13 20:24:14 所属栏目:百科 来源:网络整理
导读:我读了一些线程,解释了IoC和DI之间的区别,虽然许多解释与之相矛盾,但我仍然帮助我理解差异. 所以在这里,我想问一下我的理解是否正确,并且还会提供帮助我的摘录(尽管其中一些与之相互矛盾). 我知道已经有很多关于这个问题的线程,但我希望这个线程不会关闭,因
我读了一些线程,解释了IoC和DI之间的区别,虽然许多解释与之相矛盾,但我仍然帮助我理解差异.

所以在这里,我想问一下我的理解是否正确,并且还会提供帮助我的摘录(尽管其中一些与之相互矛盾).

我知道已经有很多关于这个问题的线程,但我希望这个线程不会关闭,因为我不认为所提到的线程中的任何OP都显示了所有相关的帖子(来自各种线程),帮助他们终于明白了.

无论如何,这里是我如何理解(如果可能,请单独处理/回答每个问题):

a)当我们在框架层面应用DIP原则时,我们使用术语IoC?在框架层面实施DIP的机制之一是DI?

b)当我们在较低级别/非框架级别实施DIP(使用DI)时,术语IoC不适用,在这种情况下,我们简称为DI?

c)DI帮助我们通过将依赖关系的实际创建和选择的控制传递给对其他2个参与者中立的第三方来实现DIP.

d)当框架级(IoC)应用DIP(使用DI)时,三种类型的控制将被反转:

>界面的控制.现在,高级模块正在控制下级模块需要遵循的接口,而不是相反
>流量的控制. – >现在框架代码(而不是用户/业务代码)控制程序的流程(换句话说 – 他们(即框架)调用你(即业务代码))
>依赖创建的控制.这种反转将对依赖关系的实际创建和选择的控制传递给对其他任何一方参与的第三方的中立.

e)当在非框架级使用DIP(使用DI)时,两种类型的控制被反转:

>界面的控制.现在,而不是相反
>依赖创建的控制.这种反转将对依赖关系的实际创建和选择的控制传递给对其他任何一方参与的第三方的中立.

这是摘录:

Why so many terms to say the same thing? IoC and DIP

Inversion of Control is the generic term. Dependency Injection is a
specific type of IoC

Inversion of Control is when the framework/infrastructure invokes
application code,rather than the other way around

can do DI without doing IoC. If you inject aConsoleStringWriter into a
HelloWorld I don’t really think of this as IoC because there is no
“framework” or “infrastructure”.

Inversion of Control < Dependency Injection

If you accept Fowler’s definition,Inversion of Control is a much
broader term than DI that covers allframework usage where you plug
into a framework,but the framework is still in control. Dependency
Injection is a specialization of IoC that applies IoC specifically to
manage dependencies.

Where exactly is the difference between IoC and DI

IoC is the ability to vary the implementation of a contract. DI is the
ability to supply the implementation.

In traditional applications,developers would write business code and
framework code. The business code would then call the framework code
to accomplish tasks. Under an IoC model,you “invert” that model and
create a framework that accepts business modules and calls them to
accomplish tasks

Dependency Injection is a technique (hard to call it a pattern,
really) of removing internal dependencies from implementations by
allowing dependent objects to be injected into the class/method by an
external caller. IoC frameworks use dependency injection to supply
user modules and other dependent code to framework routines that “glue
it all together.” Dependency injection is used heavily by IoC
frameworks because that is the mechanism that allows them to “Call
You.”

DIP vs. DI vs. IoC

DIP is the principle that guides us towards DI. Basically,loose
coupling is the goal,and there are at least two ways to achieve it.
? Dependency Injection ? Service Locator

Does anyone have a good analogy for dependency injection?

The essence of Inversion of Control (of which Dependency Injection is
an implementation) is the separation of the use of an object from the
management thereof.

Difference between ioc and dependency injection

The terms Dependency Injection (DI) & Inversion of Control (IoC) are
generally used interchangeably to describe the same design pattern
(although not everyone agrees on that point,and some people tend to
apply them in slightly different ways). The pattern was originally
called IoC,but Martin Fowler proposed the shift to DI because all
frameworks invert control in some way and he wanted to be more
specific about which aspect of control was being inverted.

Inversion of Control vs Dependency Injection

Inversion of Control (IoC) means that objects do not create other
objects on which they rely to do their work. Instead,they get the
objects that they need from an outside source (for example,an xml
configuration file). Dependency Injection (DI) means that this is done
without the object intervention,usually by a framework component that
passes constructor parameters and set properties.

谢谢

这是我的观点:

DIP意味着您针对抽象进行编程.您将依赖关系的一种从实现转换为抽象.

IOC意味着其他人负责获取给定抽象的实现.通常消费者将使用新的关键字来获得依赖.使用IoC,您可以反转控件,以便消费者不再负责创建该实例.

依赖注入和服务位置是控制反转的一部分.

参见:https://stackoverflow.com/a/10053413/175399

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读