控制反转与依赖注射 [英] Inversion of Control vs Dependency Injection

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

问题描述

根据 Martin Fowler撰写的论文,控制倒置是控制的原理程序的流程被反转:代替程序员控制程序的流程,外部源(框架,服务和其他组件)控制它。这就像我们把东西插入别的东西。他提到了一个关于EJB 2.0的例子:

According to the paper written by Martin Fowler, inversion of control is the principle where the control flow of a program is inverted: instead of the programmer controlling the flow of a program, the external sources (framework, services, other components) take control of it. It's like we plug something into something else. He mentioned an example about EJB 2.0:


例如Session Bean接口
定义了ejbRemove,ejbPassivate
到二级存储)和
ejbActivate(从被动
状态恢复)。你不会控制
这些方法被调用,只需要
就可以了。容器叫我们,我们
不叫它。

For example the Session Bean interface defines ejbRemove, ejbPassivate (stored to secondary storage), and ejbActivate (restored from passive state). You don't get to control when these methods are called, just what they do. The container calls us, we don't call it.

这导致了框架和库之间的区别: / p>

This leads to the difference between framework and library:


反转控制是
的一个关键部分,它使框架与
库不同。一个图书馆基本上是一个
的函数集,您可以调用这些函数,
这些日子通常组织成
类。每个调用都有一些工作,
返回到客户端的控制。

Inversion of Control is a key part of what makes a framework different to a library. A library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.

我认为,DI是IOC的观点,意味着对象的依赖性被反转:相反,它控制着它自己的依赖关系,生命周期...还有一些它为你而设。但是,正如你通过双手告诉我DI,DI不一定是IOC。我们仍然可以拥有DI,而不是IOC。

I think, the point of view that DI is IOC, means the dependency of an object is inverted: instead it controls its own dependencies, life cycle... something else does it for you. But, as you told me about DI by hands, DI is not necessarily IOC. We can still have DI and no IOC.

然而,在本文中(来自pococapsule,另一个用于C / C ++的IOC框架),它表明,由于IOC和DI,IOC容器和DI框架远比J2EE更优越,因为J2EE将框架代码混合到组件中,因此不会使其成为普通Java / C ++对象(PO​​JO / POCO)。

However, in this paper (from the pococapsule, another IOC Framework for C/C++), it suggests that because of IOC and DI, the IOC containers and DI frameworks are far more superior to J2EE, since J2EE mixes the framework code into the components, thus not making it Plain Old Java/C++ Object (POJO/POCO).

依赖注入模式以外的控制容器的反转 http://www.pocomatic.com/docs/whitepapers/ioc-vs-di/

了解什么是其他阅读旧的基于组件的开发框架的问题,导致上面的第二篇论文:为什么和什么是反转控制 http://www.pocomatic.com/docs/whitepapers/ioc/

Additional reading to understand what's the problem with old Component-Based Development Framework, which leads to the second paper above: Why and what of Inversion of Control: http://www.pocomatic.com/docs/whitepapers/ioc/

我的问题:什么是xactly是IOC和DI?我很困惑。基于pococapsule,IOC比仅仅反转对象控件或程序员和框架之间更重要。

My Question: What's exactly is IOC and DI? I am confused. Based on pococapsule, IOC is something more significant than just invert the control of objects or between programmers and frameworks.

推荐答案

IoC 是一个通用术语,而不是使用应用程序,在框架中调用方法,框架调用应用程序提供的实现。

IoC is a generic term meaning rather than having the application, call the methods in a framework, the framework calls implementations provided by the application.

DI 是一种IoC的形式,其中通过构造函数/设置器/服务查找将实现传递到对象中,对象将依赖以正常运行。

DI is a form of IoC, where implementations are passed into an object through constructors/setters/service lookups, which the object will 'depend' on in order to behave correctly.

IoC而不使用DI ,例如将是Template模式,因为实现只能通过子类进行更改。

IoC without using DI, for example would be the Template pattern because the implementation can only be changed through sub-classing.

DI框架旨在利用DI,并可以定义接口(或Java中的注释),以便轻松实现。

DI Frameworks are designed to make use of DI and can define interfaces (or Annotations in Java) to make it easy to pass in the implementations.

IoC容器是DI框架这可以在编程语言之外工作。在某些情况下,您可以配置哪些实现在不太侵入的元数据文件(例如XML)中使用。有些人可以通过切入点注入实施通常不可能做的IoC。

IoC Containers are DI frameworks that can work outside of the programming language. In some you can configure which implementations to use in metadata files (e.g. XML) which are less invasive. With some you can do IoC that would normally be impossible like inject an implementation at pointcuts.

另请参阅 Martin Fowler的文章

这篇关于控制反转与依赖注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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