统一使用 IObservable
我想使用 iobservable 模式来公开事件流。问题是我使用 unity 来创建观察者和事件生成器。我宁愿不必在应用程序启动时更新这两个内容,这样我就可以开始侦听事件。有人对此有什么建议吗?
I want to use the iobservable pattern to expose a stream of events. The problem is that I'm using unity to create both the observer and the event generator. I would rather not have to new up both of these at application start just so I can start listening for events. Does any one have any suggestions about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读您对我的评论的回复后(很抱歉延迟,请参阅我的评论)我可以想到两种解决方法。
首先,让订阅者订阅一个包装(即订阅)尚未创建的 IObservable 源的 IObservable。这样,订阅者可以立即订阅,但在创建源之前,值不会开始传递。
另一种选择是创建随 棱镜(2.2,我还没有检查过4)。
EventAggregator
充当广播事件系统,任何代码都可以请求一个可以订阅或发布的Event
。在您的情况下,该事件将实现ISubject
(即IObservable
和IObserver
)。After reading your reply to my comment (sorry about the delay, see my comment) I can think of two ways of solving it.
Firstly, have the subscriber subscriber to an
IObservable
that wraps (ie. subscribes to) the yet-to-be-createdIObservable
source. This way, the subscriber can subscribe immediately, but the values won't start coming through until the source has been created.The other choice is to create an
IObservable
flavoured version of theEventAggregator
that ships with Prism (2.2, I haven't checked out 4). TheEventAggregator
acts as a broadcast event system, whereby any piece of code can ask for anEvent
which can be either subscribed to or published to. In your case, the event would implementISubject
(that is, bothIObservable
andIObserver
).