InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

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

Posted by Abel Avram on Jul 23, 2009

Sections
Development,
Architecture & Design
Topics
Design Pattern ,
.NET ,
Language Design
Tags
.NET Framework 4.0

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.

Observing for java/scala implementations by Srgjan Srepfler Posted
  1. Back to top

    Observing for java/scala implementations

    by Srgjan Srepfler

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

Educational Content

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.