IObservable vs Plain Events或为什么要使用IObservable? [英] IObservable vs Plain Events or Why Should I use IObservable?

查看:155
本文介绍了IObservable vs Plain Events或为什么要使用IObservable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft推出了 IObservable< T> 界面到.NET Framework 4的BCL,我想:太好了,最后我必须使用它!所以我挖了很深,阅读帖子和文档,甚至实现了模式。

Microsoft introduced the IObservable<T> interface to the BCL with .NET Framework 4, and I thought, "Great, finally, I must use it!" So I dug deep and read posts and documentation and even implemented the pattern.

这样做后,我意识到基本的实现实际上发送了所有的 T 所有订阅者的事件,而不对其进行过滤;即纯广播。我读到某个地方,可观察模式是为普通广播而设计的。我觉得这不是真的,而且我错过了一些东西。

After doing so I've realized that the basic implementation actually sends all the T events to all of its subscribers without any filtering on it; i.e. plain broadcast. I read somewhere that the Observable pattern is meant for plain broadcast. I feel that this is not true and that I am missing something.

我的问题:


  1. 如果我添加过滤机制,使用Observable模式和使用纯CLR事件有什么区别?

  1. If I add a filtering mechanism, what is the difference between using the Observable pattern and just using plain CLR events?

什么时候应该使用这种模式,什么时候应该选择使用简单的CLR事件?

When should one use this pattern, and when should one choose to use plain CLR events?

观察模式的主要优点是什么

What are the main advantages of the Observable pattern?


推荐答案

可观察是 Rx库。它们几乎提供了您需要的所有实现和运算符。 IObservable< T> Rx 背后的想法不仅仅是事件的处理,而是使LINQ to活动。因此,您可以轻松地撰写事件流,与常规事件处理相比,您可以获得更多的权力。

Observable is the cornerstone of the Rx library. They provide pretty much all the implementations and operators you'll need. The idea behind IObservable<T> and Rx is not just the "handling" of events, but enabling "LINQ to Events." So you can easily compose "event streams," which gives you a lot of power compared to regular event handling.

请注意,MSDN实现的 IObservable< T> 不正确;文档小组已被通知。

Note that the sample MSDN implementation of IObservable<T> is incorrect; the doc team has been notified.

这篇关于IObservable vs Plain Events或为什么要使用IObservable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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