12

我已经阅读了许多解释IoCDI之间差异的线程,虽然许多解释相互矛盾,但我认为它们仍然帮助我理解了差异。

所以在这里我想问一下我的理解是否正确,并发布对我有帮助的摘录(尽管其中一些相互矛盾)。

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

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

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

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

c) DI通过将依赖项的实际创建和选择的控制权交给对其他两个相关方中立的第 3 方来帮助我们实现DIP ?

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

  1. 界面的控制。现在高层模块正在控制低层模块需要遵守的接口,而不是相反

  2. 流量的控制。--> 现在框架代码(而不是用户/业务代码)控制程序的流程(换句话说 -他们(即框架)调用你(即业务代码)

  3. 依赖创建的控制。这种反转将依赖项的实际创建和选择的控制权传递给第三方,该第三方对所涉及的其他 2 中的任何一方都是中立的。

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

  1. 界面的控制。现在高层模块正在控制低层模块需要遵守的接口,而不是相反

  2. 依赖创建的控制。这种反转将依赖项的实际创建和选择的控制权传递给第三方,该第三方对所涉及的其他 2 中的任何一方都是中立的。

?

Here are excerpts that helped:

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.

thank you

4

1 回答 1

19

Well this is my point of view:

DI Overview

DIP means that you program against an abstraction. You invert the kind of a dependency from an implementation to an abstraction.

IOC means that somebody else is responsible for getting the implementation for the given abstraction. Normally the consumer would use the new keyword to get a dependency. With IoC you invert the control, so that the consumer is not responsible for creating the instance anymore.

Dependency Injection and Service Location are a part of Inversion of Control. DIvsSL

See also: https://stackoverflow.com/a/10053413/175399

于 2012-07-03T20:55:32.043 回答