1

What is Rx FrameWork in .Net i have gone through some articles on it but concept is still not clear to me. Can anybody explain it with a practical example.

Deepesh
  • 5,346
  • 6
  • 30
  • 45
  • 1
    It's the Observer Pattern made easy http://en.wikipedia.org/wiki/Observer_pattern you can subscribe to events or any change in a observablecollection this works great for the MVVM pattern. http://haacked.com/archive/2012/04/09/reactive-extensions-sample.aspx. Phil explained better and with a practical example – jjchiw May 30 '12 at 07:14

3 Answers3

9

Let's imagine the following scenario: after a long day you and your friend enter a pub to quench your thirst. All you want is a couple of beers and a table where you can discuss various topics (map-reduce implementations, gass prices etc).

In the pooling approach (non Rx) you'll perform these steps:

1. Ask bartender for two beers
2. While bartender pours the beer into the glasses you wait by the bar
3. When bartender gives you the beers you take them and find an available table, sit, drink and discuss

Using Rx approach you'd perform these steps:

1. Ask the bartender for two beers
2. Bartender starts pouring your beers and you and your frined find an available table, sit and  start discussing on various subjects
3. When bartender finishes pouring your beer, a waitress brings the beer to your table and you start drinking it.

Now the above could be translated into code as follows:

//Non Rx approach
var beers = beerKeg.Take(2); // Here you wait for the bartender to pour the beer
foreach(var beer in beers)   // Bartender handles you the beer
    GrabBeer(beer);
FindAvailableTable();        // You search for an available table
foreach(var beer in beers)   // You drink your beer
    beer.Drink();

//Rx approach
var beers = beerKeg.Take(2) 
                   .ToObservable() 
                   .Subscribe(beer => beer.Drink());
// Bartender is pouring the beer but meanwhile you can search for an available table.
// The waitress will bring beer to you.
FindAvailableTable();

Hope this helps.

PS: To see the difference use LINQPad; perform a non-Rx query and call Dump() on the results - the results will be displayed in a grid. Perform the same query using Rx and subscribe to the ObservableCollection with x=>x.Dump(); you'll see that each element is printed individually.

RePierre
  • 9,358
  • 2
  • 20
  • 37
5

Rx or Reactive Extensions is "LINQ to events". LINQ is based on IEnumerable<T> where you pull items from a sequence by iterating it:

var items = source.Where(x => x.Name == "foobar");
foreach (var item in items)
  Console.WriteLine(item);

Rx is the inverse based on IObservable<T> where you subscribe to a sequence of events that are pushed to you:

var events = source.Where(x => x.Name == "foobar");
events.Subscribe(item => Console.WriteLine(item));

In LINQ you control the iteration by using a foreach loop and pull items from the sequence. In Rx something else fires events and push items to you when they are ready.

Rx adds a lot of extension methods to IObservable<T> just as LINQ adds a lot of extension methods to IEnumerable<T> enabling you to write very compact and clear code for handling asynchronous events.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
2

Rx or Reactive Extensions is a library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.

Lots of articles are available on web such as :

MSDN

Rx Wiki

Visual Studio Magazine

Somasegar's Blog Entry

Rx API in Depth Intro

and our own SO

See Writing your first Rx App on Channel 9 and,

Getting Started with Rx Extensions for .NET on Channel 9

Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65