依赖注入系统中的事件会发生什么? [英] Events in a Dependency Injection system go which way?

查看:63
本文介绍了依赖注入系统中的事件会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向上还是向下?

我是一个非常有远见的人.我将我的应用程序视为一个层次结构,其中顶部是根,底部是叶.

I'm a very visual person. I'm thinking of my application as a hierarchy, where the top is the root and the bottom is a leaf.

我还理解到,在DI系统中,容器不了解其包含的对象的职责/功能.相反,包含的对象会知道其上下文,因为上下文(依赖项)已注入.

I'm also under the understanding that, in DI systems, containers are ignorant of their contained objects' responsibilities/functions. Instead, the contained objects know about their context because the context (a dependency) is injected in.

UP: (非DI方式?)
我的活动是否应该从层次结构的底部分发,并向上传播给他们的父母?例如.我的GUI中的一个按钮调度了一个CLICKED事件,该事件被侦听容器捕获,该容器通过执行适当的操作进行响应.

UP: (The non-DI way?)
Should my events be dispatched from the bottom of my hierarchy and bubble upwards to their parents? E.g. a button in my GUI dispatches a CLICKED event, which is caught by a listening container that responds by carrying out the appropriate action.

DOWN: (DI方式?)
我的事件是否应该由其子级倾听的父母从层次结构的 top 调度?例如. --...好吧,我很难解决这个问题.我正在考虑一个调解器,该调解器为包含的对象调度事件.

DOWN: (The DI way?)
Should my events be dispatched from the top of my hierarchy by a parent that its children listen to? E.g. --... ok, i'm having trouble coming up with this. I'm thinking along the lines of a Mediator that dispatches events for contained objects.

'Up'在我看来很自然,但是由于DI的容器不知道其包含对象的行为,所以我不想响应它们的事件.

'Up' seems natural to me, but since DI has a container being unaware of its contained objects' behavior, I wouldn't want to respond to their events.

我意识到几乎可以让系统的任何部分来监听事件,但是我想了解DI参与者之间的基本关系,因此可以适当地构建它们.我假设人们通常不只是在不考虑结构关系,依赖关系等因素的情况下散布有关其程序的事件.

I realize it's POSSIBLE to have nearly any part of a system listen to an event, but I want to understand the fundamental relationship between DI participants, so I can structure them properly. I'm assuming people don't usually just scatter events about their program without regard to structural relationships, dependencies, etc.

我的问题来自于DI系统中的责任放置-注入对象负责调用喷射器,而注入器负责为其依存对象提供服务的责任(这会颠倒非DI范式-因此是同义词)例如控制反转"和依赖反转").这似乎是DI的一个非常重要的基本组成部分-职责转移.我认为同时调用对象的函数或监听其事件都是依赖的示例.当一个对象基于另一个对象定义自己的行为时,我称其为依赖项,因为一个对象知道另一个对象,也知道另一个对象做什么.它已在另一个对象的上下文中定义了自己.据我了解,DI的设置应使注入的对象依赖于注射器,因此了解注入器的所有知识应由注入的对象负责.而且,了解注入对象不应该是注入者的责任.因此,在我看来,让注入器监听包含CONTAINED注入对象上的事件似乎像是责任的错位,因为这意味着注入者对其内容有所了解.

My question arises from the responsibility placement in a DI system -- it's the injected object's responsibility to make calls on the injector and the injector's reponsibility to provide services to its dependent objects (which inverts the non-DI paradigm -- hence synonyms like "Inversion of Control" and "Dependency Inversion"). This seems to be a very important, fundamental component of DI -- a shifting of responsibilities. I consider calling an object's functions or listening to its events to BOTH be examples of depending on another object. When an object defines its own behavior based on another object, I call that a dependency because one object KNOWS of the other and also KNOWS what the other does. It has defined itself within the context of the other object. As I understand, DI is set up so that the injected object is dependent on the injector, so it should be the injected object's responsiblity to know all about the injector. And it should NOT be the injector's responsiblity to know about the injected object. So, for the injector to be listening to events on the CONTAINED injected object seems to me like like a misplacement of responsibilities because that means it knows something about its contents.

请告诉我这是怎么回事,怎么错.

Please tell me if and how this is wrong.

推荐答案

依赖注入和观察者模式中的角色分配是正交的问题.一个对象可能扮演观察者的角色或被观察者的角色,并且同时要么是组成对象,要么是组成对象,要么两者都是,要么都不是.

Dependency Injection and the role assignments within the Observer Pattern are orthogonal concerns. An object may play the role of the observer or observed and simultaneously be either the composing object, the composed object, both, or neither.

考虑一个典型的示例,其中按钮由控件组成.单击该按钮时,将引发一个Clicked事件,该事件由包含的控件响应.在这种情况下,观察对象就是观察对象.

Consider a typical example where a button is composed by a control. When the button is clicked, it raises a Clicked event which is responded to by the containing control. In this case the observing object is composing the observed object.

现在考虑由许多控件组成的屏幕.屏幕可能会引发一个Closing事件,该事件允许所组成的控件在关闭整个屏幕之前执行自己的清理工作.在这种情况下,观察对象就是观察者.

Now consider a screen which composes a number of controls. The screen may raise a Closing event which allows the composed controls to perform their own cleanup work before the entire screen is closed. In this case, the observed object is composing the observers.

现在考虑由按钮和标签组成的屏幕.单击该按钮时,将调用标签的Clear()方法.在这种情况下,观察者和观察者都不会构成另一个,但是两者都是由屏幕对象组成的.

Now consider a screen which composes a button and a label. When the button is clicked, the label's Clear() method is called. In this case, neither observer nor observed composes the other, but both are composed by the screen object.

现在考虑一个屏幕,该屏幕引发它自己订阅的Closing事件(也许确保将自己作为已注册的最终事件处理程序).当它引发一个Closing事件时,它允许任何观察者先执行他们可能需要的任何操作,然后处理自己的事件.在这种情况下,观察者就是被观察者.

Now consider a screen which raises a Closing event which it itself subscribes to (perhaps ensuring itself to be the final event handler that is registered). When it raises a Closing event, allowing any observers to first perform any actions they might need, it then handles its own event. In this case, the observer is the observed.

依赖注入涉及对象如何获得其依赖.注入给定对象的依赖项可能包含该对象想要订阅的事件,或者该依赖项可能订阅了要注入的对象.在将一个对象注入另一个对象之后,两个对象如何交互实际上与依赖项注入没有任何关系.

Dependency injection concerns how an object obtains its dependencies. The dependency injected into a given object may contain an event the object wants to subscribe to, or the dependency may subscribe to the object it is being injected to. How two objects interact after one is injected into another doesn't really have anything to do with dependency injection.

关于您的澄清部分,我相信我理解您困惑的根源.传统上,对象创建自己的依赖关系.通过将关于如何获取依赖项的知识移到对象外部,依赖项注入可以逆转获得这些依赖项的责任.但是,依赖注入不会反转依赖关系.您可能会将依赖注入与依赖反转原理.依赖倒置原则的确可以逆转高级"和低级"对象之间的依赖关系,但是依赖注入仅与如何将依赖关系提供给给定对象有关.因此,使用依赖注入不会改变对象之间正常的交互方式.如果在使用依赖项注入之前,对象B订阅了对象A引发的事件(反之亦然),那么引入依赖项注入将不会对此进行更改.

Concerning your clarification section, I believe I understand the source of your confusion. Traditionally, an object creates its own dependencies. Dependency injection inverts the responsibility of obtaining these dependencies by moving the knowledge of how the dependency is obtained outside of the object. Dependency injection doesn't invert dependency relationships however. You may be confusing Dependency Injection with the Dependency Inversion Principle. The Dependency Inversion Principle does reverse the dependency relationship between "high-level" and "low-level" objects, but dependency injection is only concerned with how dependencies are supplied to a given object. Therefore, use of dependency injection doesn't change how objects normally interact with one another. If prior to using dependency injection object B subscribed to events raised by object A (or vice versa), introducing dependency injection will not change this.

这篇关于依赖注入系统中的事件会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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