.NET Explored

Archive for the ‘SilverLight’ Category

.NET Reactive(Rx) Framework

17 November 2009 | 1 Comment » | admin

Reactive Programming is a new buzz word in the Microsoft world when late this summer they release a Framework name “Reactive Framework” in the latest version of Silver Light toolkit (System.Reactive.dll) from Microsoft. A complete version is expected to be part of Visual Studio 2010 and will be supported by .NET Framework 4.

But before dwelling deep into the reactive programming lets see what does reactive programming means. It basically means how the producer be able to publish data both static and dynamic and how the consumer be able to view the data seamlessly and automatically.

For example, in an imperative programming setting, a: = b + c would mean that a is being assigned the result of b + c in the instant the expression is evaluated.

Later, the values of b and c can be changed with no effect on the value of a. In reactive programming, the value of a would be automatically updated based on the new values.

A modern spreadsheet program is an example of reactive programming. Spreadsheet cells can contain literal values, or formulas such as “=B1+C1″ that are evaluated based on other cells. Whenever the value of the other cells change, the value of the formula is automatically updated.

Similarly another easy concept in microsoft programming world is the event handler or constantly running services which publishes the data constantly and we write a program to handle that event to show data.

Coming back to the “Reactive Framework” or “Rx Framework” it is based on the same principal discussed above. With the advance of the asynchronous programming reactive framework definitely comes to help.

 

This team has been able to implement the dual mode of communication channel of push – pull or publisher-subscriber model using the following interfaces.

  • The Iterator pattern of IEnumerable/IEnumerator to pull\subscribe to the data used by the listner
  • Iterator pattern of IObservable/IObserver for push\publish the data used by the source

 

To dwell deep into the Reactive framework have a look at the video from Eric Meijer explaining this concept.

 

Lets take a quickly look at how we can use Rx to simplify our code, we will write an extension method that will call a web client’s DownloadWeatherData method and return a String.

public static class WebClientExtender
{

public static IObservable<string> GetSite(this WebClient client, string url)
{
var downloaded = Observable.FromEvent<DownloadWeatherDataCompletedEventArgs>
                 (client, ” DownloadWeatherData”);
client.DownloadWeatherDataAsync(new Uri(url));
return downloaded.Select(x=>((DownloadWeatherDataCompletedEventArgs)x.EventArgs).Result);
}

}

To call this we can now simple add the following line of code to our Silverlight application.

new WebClient().GetSite(“http://localhost/”).Subscribe(x => MessageBox.Show(x));

This way we can very easily clean up the code and structure how the Asynchronous events are handled.

 

To have a look at some of the programming concepts of it have a look at the blog introducing reactive (Rx) Linq to Events

  • Share/Bookmark