的IObservable VS普通活动或者我为什么要使用的IObservable? [英] IObservable vs Plain Events or Why Should I use IObservable?

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

问题描述

微软推出了 的IObservable< T> 接口以使用.NET Framework 4的首创置业,我想,太好了,终于,我必须用它!所以我挖深和阅读文章和文件,甚至实现的模式。

这方面,我已经意识到,基本实现实际上将所有的 T 事件给所有的用户不会对任何过滤之后;即普通广播。我读的地方可观察到的模式是为普通的广播。我觉得这是不正确的,我失去了一些东西。

我的问题:

  1. 如果我添加一个过滤机制,什么是使用可观察到的模式,只是使用普通的CLR事件之间的区别是什么?

  2. 当一个人应该使用这种模式,而当人应该选择使用纯CLR事件?

  3. 什么是可观察的模式的主要优势是什么?

解决方案

可观察到的是接收的基石库。他们提供了pretty的多所有的实现,您需要运营商。 T> 和接收不仅是处理的事件,但使LINQ到后面的IObservable&LT的想法事件。所以,你可以很容易地撰写事件流,它给你很大的功率比一般的事件处理。

请注意,样品MSDN实施的IObservable&LT; T&GT; 是<一个href="http://social.msdn.microsoft.com/Forums/en-US/rx/thread/19be939b-d257-4d8e-b104-4dfcc59b3ff8">incorrect;商务部团队已通知。

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.

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.

My questions:

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

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

  3. What are the main advantages of the Observable pattern?

解决方案

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.

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

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

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