12 out of 19 rated this helpful - Rate this topic

Reactive Extensions for .NET Framework Overview for Windows Phone

Windows Phone

March 22, 2012

This topic provides a brief introduction to the Reactive Extensions for .NET Framework and describes some ways that they can be used in your Windows Phone application.

Reactive Extensions for .NET Framework is a managed library that provides APIs for writing reactive applications. Reactive applications are driven by their environment. In the reactive model, data streams, asynchronous requests, and events are represented as observable sequences. The application can subscribe to these observable sequences to receive asynchronous messages as new data arrives. Reactive Extensions allows the application to compose these sequences using query operators.

If your application interacts with multiple sources of data, such as user input events, web services requests, and system notifications, the conventional way to manage all of these interactions is to implement separate handlers for each of these data streams. Inside these handlers, you must provide the code to coordinate between all of the different data streams and process this data into a useable form. Reactive Extensions allows you to write a query that combines all of these data streams into a single stream that triggers a single handler. The work of filtering, synchronizing, and transforming the data is performed by the Reactive Extensions query so that your handler can just react to the data it receives and do something with it.

Reactive Extensions is a programming model that has applications for a broad range of applications, but it can be particularly useful for applications that run on mobile devices, because they tend to function as a hub for receiving and processing a user’s data from multiple sources. This section will highlight some Reactive Extensions scenarios that are of particular interest to Windows Phone developers.

Filtering Events

The Microsoft Location Service exposes an event that will notify your application when location data is obtained. The built-in API lets you filter the data and allows you to request to be notified only if the location data changes by at least a specified amount. Reactive Extensions lets you further filter the events so that, for example, you could have your application be notified only when the location falls within a specified bounding rectangle. Reactive Extensions lets you define a query that filters a data stream so that only the data that meets the specified criteria ends up in the final stream.

Composing Multiple Asynchronous Web Service Requests

Applications for mobile devices are often created by blending together the content and functionality from multiple web services. The amount of time it will take for a web service to return a request is unpredictable by nature as it depends on the connection speed of the device, internet traffic, and the implementation of the service itself. Reactive Extensions enables you to make some number of web service requests and only receive responses that fall within a specified time window. You could also use it to compose search queries so that a search that is posted to multiple web services will return to your app once a specified minimum number of the search queries have returned.

Emulating Data Streams

Application development for mobile devices can be challenging because of the many conditions that applications face in the working environment that are not present in the development environment. Accelerometer and location data, for example, can be difficult to simulate when developing applications on a device emulator. Because the Reactive Extensions model wraps consumers and producers of data into observer and observable objects with a defined interface, it is possible to write a consumer, such as a handler for accelerometer data, and swap out the producer, from a file stream containing simulated data to the live sensor, with very little overhead.

Did you find this helpful?
(1500 characters remaining)