控制反转与依赖注入与选定的引号 - 我的理解正确吗? [英] Inversion Of Control vs Dependency Injection with selected quotes – is my understanding correct?

查看:139
本文介绍了控制反转与依赖注入与选定的引号 - 我的理解正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些线索,解释了 IoC DI 之间的区别,虽然许多解释与之相矛盾,但我仍然帮助我理解差异



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



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



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



a)当我们在框架级别应用 DIP 原则时,我们使用术语 IoC ?而在框架级别中实现 DIP 的机制之一是 DI ? b / b> b b)当实施 DIP (使用 DI )时, IoC 不适用>)在较低级别/非框架级别,在这种情况下,我们简单地称之为 DI



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



d)当应用 DIP (使用 DI )在框架级 IoC),然后三种类型的控件得到反转:


  1. 界面的控制。


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


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


e)当 DIP DI )应用于非框架级时,两种类型的控件会被反转:


  1. 界面的控制。现在,高级模块正在控制界面较低


  2. 依赖关系创建的控制需要坚持而不是其他方式。这种反转将对实际创建和依赖关系的选择的控制权传给了第三方,对于涉及的其他2中的任何一方来说都是中立的。




这里有一些帮助的摘录:




I've read a number of threads explaining the difference between IoC and DI and while many of explanations contradicted each other, I think they still helped me understand the difference.

So here I'd like to ask whether my understanding is correct and also post excerpts that helped me ( though some of them contradict each other ).

I know there have been lots of threads done on the subject, but I'm hoping this thread won't get closed since I don't think any of OPs in mentioned threads also showed all the relevant posts ( from various threads ) that helped them finally understand it.

Anyways, here is how I understand it (if possible,please address/answer each question individually ):

a) When we apply DIP principle at the framework level, then we use the term IoC? And one of the mechanisms to implement DIP at framework level is DI?

b) The term IoC does not apply when we implement DIP ( using DI ) at a lower level/non-framework level, in which case we simply call it DI?

c) DI helps us to achive DIP by passing the control of the actual creation of and selection of dependencies to a 3rd party which is neutral to either of the other 2 involved?

d) When DIP is applied ( using DI ) at framework level (IoC), then three types of control get inverted:

  1. The control of the interface. Now high level module is controlling the interface that the lower level modules need to adhere to instead of the other way around

  2. The control of the flow. --> Now framework code ( instead of user/business code ) controls the flow of the program ( in other words - they ( ie framework ) call you ( ie business code ) )

  3. The control of dependency creation. This inversion is passing the control of the actual creation and selection of dependencies to a 3rd party which is neutral to either of the other 2 involved.

e) When DIP is applied (using DI) at non-framework level, then two types of control get inverted:

  1. The control of the interface. Now high level module is controlling the interface that the lower level modules need to adhere to instead of the other way around

  2. The control of dependency creation. This inversion is passing the control of the actual creation and selection of dependencies to a 3rd party which is neutral to either of the other 2 involved.

?

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

解决方案

Well this is my point of view:

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.

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

这篇关于控制反转与依赖注入与选定的引号 - 我的理解正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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