Question

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?

Was it helpful?

Solution

.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.

Check out this set of articles

OTHER TIPS

You can read more about it here: http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html

My first exposure was on this blog, as I am reading his book on F#: http://tomasp.net/articles/reactive-ii-csevents.aspx

Basically my understanding of it is that when you write an event handler for mouse movements, for example, you may want to continuously capture these events, and perhaps be able to count how many movements or clicks, but, basically, you want to use that information in more ways than just as an event handler. So, just treat the events as a continuous stream.

Also, check out this Channel 9 video: Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx)

From the show description:

The .NET Reactive Framework (Rx) is the mathematical dual of LINQ to Objects. It consists of a pair of interfaces IObserver/IObservable that represent push-based, or observable, collections, plus a library of extension methods that implement the LINQ Standard Query Operators and other useful stream transformation functions.

I think the essence of the RX is changing the angle with which we look at information. Naturally, every piece of information is a result of some process, which takes place in the time-space continuum. Traditional approach works with the projection of the process to the "current" time plane, losing the details of the time dimension as the result.

RX works with the projection to some other dimension, capturing the time component as well, so it is no wonder LINQ to RX is a superset of the regular LINQ.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top