什么是LINQ to事件a.k.a RX框架? [英] What is LINQ to events a.k.a RX Framework?
问题描述
什么是LINQ to event aka RX Framework 又称.NET 4.0中的Reactive Extensions(也可作为backported版本)?
换句话说,System.Reactive.dll中的所有东西是什么?
.NET Rx团队(这不是一个官方名称)发现任何推送序列(事件,回调)都可以被视为一个拉序列(正如我们通常在访问枚举时) - 或者它们是双重的。简而言之,观察者/可观察的模式是枚举模式的双重性。
那么关于这个二元性有什么好看的?使用拉序列(读声明样式编码)所做的任何事情也适用于推送序列。这里有几个方面。
您可以从现有事件中创建Observables,然后将它们作为.NET中的一级公民使用 - 即,您可以从事件创建一个observable,并将其显示为一个属性。
由于IObservable是IEnumerable的数学双重功能,因此.NET Rx方便LINQ通过推送序列,如事件,很像LINQ over IEnumerables
它提供了更大的自由组合新事件 - 您可以从一般事件中创建特定事件。
.NET Rx引入了两个接口,IObservable和IObserver 提供了使用输入和输出适配器作为事件源和接收器的生产者和消费者的替代方案,这将很快成为以声明方式编写异步代码的事实。这是一个简单的例子。
//为MouseLeftButtonDown创建一个可观察值
var mouseLeftDown = Observable。 FromEvent< MouseButtonEventArgs>
(mycontrol,MouseLeftButtonDown);
//查询上述observable只是为了选择点
var points = from mouse inEvents
select ev.EventArgs.GetPosition(this);
//在窗口标题中显示点数,用户
//按下鼠标左键
points.Subscribe(p => this.Title =位置=
+ pX +,+ pY);
您也可以通过这些帖子来详细了解头尾。另请参阅相关的源代码。
- 第一部分 - System.Reactive或.NET Reactive Extensions(Rx) - 概念和第一眼看
- 第二部分 - LINQ To活动 - 有关.NET无效扩展(Rx)的更多信息
- 第III部分 - LINQ To事件 - 使用T4文本模板生成GetEventName()包装器方法
What is LINQ to events a.k.a RX Framework aka the Reactive Extensions in .NET 4.0 (but also available as backported versions)?
In other words, what is all the stuff in System.Reactive.dll for?
.NET Rx team (this is not an official name) found that any push sequence (events, callbacks) can be viewed as a pull sequence (as we normally do while accessing enumerables) as well – or they are Dual in nature. In short observer/observable pattern is the dual of enumeration pattern.
So what is cool about about this duality?
Anything you do with Pull sequences (read declarative style coding) is applicable to push sequences as well. Here are few aspects. You can create Observables from existing events and then use them as first class citizens in .NET – i.e, you may create an observable from an event, and expose the same as a property.
As IObservable is the mathematical dual of IEnumerable, .NET Rx facilitates LINQ over push sequences like Events, much like LINQ over IEnumerables
It gives greater freedom to compose new events – you can create specific events out of general events.
.NET Rx introduces two interfaces, IObservable and IObserver that "provides an alternative to using input and output adapters as the producer and consumer of event sources and sinks" and this will soon become the de-facto for writing asynchronous code in a declarative manner. Here is a quick example.
//Create an observable for MouseLeftButtonDown
var mouseLeftDown=Observable.FromEvent<MouseButtonEventArgs>
(mycontrol,"MouseLeftButtonDown");
//Query the above observable just to select the points
var points = from ev in mouseEvents
select ev.EventArgs.GetPosition(this);
//Show points in the window's title, when ever user
//presses the left button of the mouse
points.Subscribe(p => this.Title = "Location ="
+ p.X + "," + p.Y);
You may go through these posts as well to get the head and tail in detail. Also have a look at the relates source code as well.
- Part I - System.Reactive or the .NET Reactive Extensions (Rx) – Concepts and First Look
- Part II - LINQ To Events - More on .NET Reactive Extensions (Rx)
- Part III - LINQ To Events - Generating GetEventName() Wrapper Methods using T4 Text Templates
Check out this set of articles
这篇关于什么是LINQ to事件a.k.a RX框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!