作者:用途回家 | 来源:互联网 | 2023-05-19 18:41
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 个解决方案
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容器。