热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么依赖注入框架支持容器层次结构?-WhydoDependencyInjectionframeworkssupportcontainerhierarchies?

IvebeenfollowingDanielCazzulinosseriesaboutbuildingaDIcontainerusingTDD.Inpartfive

I've been following Daniel Cazzulino's series about building a DI container using TDD. In part five of the series, he adds support for container hierarchies without commenting on what makes this feature useful. I've seen mention of support for hierarchies in many of the DI frameworks, but I'm having trouble understanding when they'd be used, and why. Can someone offer some insight?

我一直在关注Daniel Cazzulino关于使用TDD构建DI容器的系列文章。在本系列的第五部分中,他添加了对容器层次结构的支持,而没有评论使该功能有用的原因。我已经看到在许多DI框架中提到对层次结构的支持,但是我很难理解它们何时被使用,以及为什么。有人可以提供一些见解吗?

4 个解决方案

#1


Here's a sample that uses child containers in a scenario similar to the one Matt describes. It uses child containers to select between different database configurations.

这是一个在类似于Matt描述的场景中使用子容器的示例。它使用子容器在不同的数据库配置之间进行选择。

The key here is that most of the configuration is shared between the child containers (that shared part belongs in the parent container)

这里的关键是大多数配置在子容器之间共享(共享部分属于父容器)

#2


I left a comment on kzu's blog asking the same question. It's a shame he didn't clarify the use-case for such a feature before coding it.

我在kzu的博客上留下了一个评论问同样的问题。令人遗憾的是,他在编码之前没有澄清这种功能的用例。

The only thing I could think of is if you wanted to have different types resolved from your container in different parts of your app. For example, if you had an order-entry system with two separate sections, and each section was identical except that they needed to present a different product list, you could create a child container for each section, and "override" the registration of your product repository in each. Whenever a section tried to resolve a product repository (or anything that depended on one) it would get the instance you set up in the child container rather than the parent. Sort of like overriding a virtual method.

我唯一能想到的是,如果你想在应用程序的不同部分从容器中解析出不同的类型。例如,如果您有一个包含两个单独部分的订单输入系统,并且每个部分都相同,只是他们需要提供不同的产品列表,您可以为每个部分创建一个子容器,并“覆盖”您的注册每个产品库。每当某个部分试图解析产品存储库(或任何依赖于它的东西)时,它将获得您在子容器而不是父容器中设置的实例。有点像重写虚拟方法。

This might be way off base, but it's the best I could come up with.

这可能是偏离基础的,但这是我能想到的最好的。

#3


There is good reason for having child containers if dependency injection is fully embraced by the project. Let's imagine an application that has processes messages from two different, but similar systems. Most of the processing is similar, but there are variations to support compatability from those systems. Our aim is to re-use the code we can, while writing different code as requirements differ.

如果项目完全接受依赖注入,那么拥有子容器是有充分理由的。让我们假设一个应用程序处理来自两个不同但类似系统的消息。大多数处理都是类似的,但是有一些变化可以支持这些系统的兼容性。我们的目标是重用我们可以使用的代码,同时根据需求编写不同的代码。

In OO programming, we wire together a series of classes that will collaborate to meet the system requirements. The DI container takes this responsibility. When a message arrives from a system, we want to build a set of collaborating classes suitable for processing a message from that particular system.

在面向对象编程中,我们将一系列类连接在一起,以协作满足系统要求。 DI容器承担这一责任。当消息从系统到达时,我们希望构建一组适合处理来自该特定系统的消息的协作类。

We have a top level container which has items that do not vary between the two systems. Then, we have child containers that do vary between systems. When a message arrives, we ask the approriate child DI container for a messageProcessor. Based on the configuration of that container (falling back to the parent container as necessary) the DI framework can return a messageProcessor (being an object backed by approriate collaborators) for the system in question.

我们有一个顶级容器,其中包含两个系统之间不同的项目。然后,我们有不同系统的子容器。当消息到达时,我们向approriate child DI容器询问messageProcessor。根据该容器的配置(根据需要回退到父容器),DI框架可以为相关系统返回messageProcessor(是由合作伙伴支持的对象)。

Please leave a comment if this is not a clear answer. Also, you can search for "robot legs problem". Each leg is identical but one needs a left foot and the other needs a right foot. We could have a child DI container for each leg.

如果这不是一个明确的答案,请发表评论。此外,您可以搜索“机器人腿问题”。每条腿都相同,但一只需要左脚,另一只需要右脚。我们每条腿都可以有一个儿童DI容器。

#4


The best example that I'm aware of for nested containers is a windowing system. It's very nice for just separation of concerns to have each tab/window have it's own container independent of the other tabs/windows, with all window containers inheriting global dependencies from a parent container.

我知道嵌套容器的最好例子是一个窗口系统。只关注问题的分离是非常好的,让每个选项卡/窗口都拥有独立于其他选项卡/窗口的自己的容器,所有窗口容器都从父容器继承全局依赖关系。

This is especially needed if you can have duplicate tab/windows, since in many cases you want to distinct instances of various classes for each duplicate tab/window

如果您可以有重复的选项卡/窗口,则尤其需要这样做,因为在许多情况下,您希望为每个重复的选项卡/窗口显示不同类的不同实例


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
author-avatar
用途回家
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有