Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News The .NET Reactive Framework (Rx) Enables LINQ over Events

The .NET Reactive Framework (Rx) Enables LINQ over Events

Leia em Português


Erik Meijer and Wes Dyer have created the .NET Reactive Framework (Rx), the mathematical dual of LINQ to Objects, allowing programmers to use LINQ over events. Erik and Brian Beckman demonstrate that IObservable is a continuation monad.

Reactive Programming isn’t something new. It’s already used by Cells, a dataflow extension for Common Lisp, or by Trellis, a Python extension supporting high-order reactive programming. In C#, a programmer uses reactive programming whenever he specifies a call-back for an asynchronous operation or a handler for an event. When the asynchronous operation finishes or when the handled event takes place, a method gets called and executed as a reaction to the event.

LINQ over Objects uses the pair of interfaces IEnumerable and IEnumerator to iterate over collections of data. The Enumerator’s MoveNext() method is used to go from one element of the enumeration to the next one, and the Current property is used to retrieve the respective element. This mechanism uses a “pull” process which is simple, well understood and widely used. Erik has discovered that IEnumerable has a dual characteristic: as one can pull a piece of data from a collection, he can also push one into the same collection. This represents another approach to reactive programming.

Theoretically, the IEnumerable interface could be used to add objects to a collection, but it can’t be used in asynchronous operations because it blocks. That’s why the team has introduced a new pair of interfaces: IObservable and IObserver. One can assign an IObserver to a collection of data and subscribe it to an IObservable. When a new data is available, it can be pushed into the collection by passing it to the IObservable, which in turn gives it to IObserver. Jafar Husain, a developer on the Presentation Platform Controls team at Microsoft, explains how the IObserver pattern is used:

To traverse an IObservable you go through the same actions as an IEnumerable but in reverse.  You create an IObserver, give it to an IObservable, and the IObservable “pushes” data into the IObserver by invoking its methods.  When an IObservable invokes the “OnUpdate” method on an Observer it is equivalent to an IEnumerable method using the yield keyword to give information to an IEnumerable.  Similarly when an IObservable invokes the “OnCompleted” method on an Observer it is equivalent to an IEnumerable using the break keyword to indicate that there is no more data.

What is this good for? 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. To demonstrate its power in handling GUI events, the Rx framework has been used in Silverlight Toolkit Unit Test and it’s been included in its source code, packaged in System.Reactive.dll. It will also be part of .NET 4.0.

Brian Beckman, a software architect and astrophysicist, demonstrates in a video interview with Erik Meijer that IObservable is a continuation monad, the dual of the IEnumerable monad. Erik also shows how the IObservable interface has been constructed by dualizing IEnumerable.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Observing for java/scala implementations

    by Srdan Srepfler,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Great, now all we need is someone implementing it with Scala :))))
    It's also interesting how it's similar to the Future API.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.