33

Has anyone had a good look at the new Reactive Framework?

This implementation of "Linq over Events" was apparently distributed with the new Silverlight 3 Toolkit, as a way to manage asynchronous events.

This approach seems to be great at handling events. One could attach an IObserver to mouse events and record them asynchronously into a collection. Then one can iterate over the data with LINQ, and process it accordingly.

Have you used this, and what was your impression? How would a tool like this fit into a 3-tier, distributed world? Are there other interesting applications of the pattern?

11 accepted

I looked and it and my first thought was, "damn, this is nice!" I like the all-in-one-place declarative handling of events. Rather than set up a handler over here, handle the event down here, unhook it there, change some mutable state to keep track of things over there -- you get rid of all that. Declarative is good.

I've played with it only twice now, and haven't used it in any real projects, so that part remains to be seen.

Jafar from the Silverlight toolkit team has a great post about it: Introducing Reactive Framework: Linq to Events He says of Rx:

If you glanced quickly you?d miss it altogether but it?s one of the most exciting additions to the .NET framework since Linq.

There's a Channel9 video that goes deep into the Reactive Framework: Expert to Expert: Brian Beckman and Eric Meijer on the Reactive Framework

One thing to keep in mind, it's compiled against the Silverlight dlls, you can't use it in a WinForms or WPF project. There's a WPF version at Microsoft, but it's as yet unreleased. Of course, it will be part of .NET 4 framework.

The name sucks, IMO -- this promises to change the way we deal with asynchronous code; but a name like Linq to Events doesn't hint at this.

11

Wes and Erik demo'd this to me last year and I was mightily impressed. It is a very slick way to conceptualize events, and I'm excited to try it out. That said, (1) I haven't actually used it to build anything yet, and (2) those guys are way, way smarter than I am and so it can be hard to get my head around everything that they've done.

6

I actually think this is a great thing -

I can see this potentially providing an easier implementation than many places where standard events or IoC are often used. For example, this could dramatically simplify having a flexible logging system. Instead of having to implement some form of IoC, your logging implementations could just implement IObserver<T> and start listening.

Another source of info on this is Paul Batum's blog. He's written an interesting, multi-post series on the Reactive Framework.

6

This weekend I played around with this, and created a couple of POCs to get a feel. And I'm loving everything. Reactive extensions (Rx) is really cool, as it introduces lot of new possibilities.

Here is the essence and summary.

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

The source code as part of the second article contains a drawing application in WFP, with a version of System.Reactive.dll that I rebased to target .NET Framework 3.5 using Reflexil - You should be able to open the same in VS 2008 to play with Winfors or WPF

Check out this set of articles where I've posted my thoughts

4

The Reactive Extensions for .NET (Rx) is now available on DevLabs, with downloads for .NET 3.5, .NET 4, and Silverlight 3.

I'm really looking forward to seeing a variety of examples as opposed to the very few that have been shown so far. I'll be keeping an eye on the Rx Team Blog for info as it become available.

-4

I don't know if it is just me, but this seems like a recipe for disaster.