Rx – Reactive Extension – for beginner (part 1)

2010/02/19

RxReactive Extension (part 1)

Rx, reacrive , IObservable, IObserver

this post is the first in a series of posts about the new Reactive Framework (Rx).

 

What is the Reactive Framework Extension?

the Rx formalize the push based programming pattern.

it is dealing with event like collection, in manner that can be describe as the mirroring of IEnumerable<T>.

 

How does it mirror the IEnumerable<T>?

the Rx framework most fundamental interface are,

IObservable<T> which is mirroring IEnumerable<T>.

Code Snippet
  1. // Summary:
  2. //     Represents a push-style collection.
  3. public interface IObservable<T>
  4. {
  5.     // Summary:
  6.     //     Subscribes an observer to the observable sequence.
  7.     IDisposable Subscribe(IObserver<T> observer);
  8. }

and IObserver<T> which is the mirroring IEnumerator<T>

Code Snippet
  1. // Summary:
  2. //     Supports push-style iteration over an observable sequence.
  3. public interface IObserver<T>
  4. {
  5.     // Summary:
  6.     //     Notifies the observer of the end of the sequence.
  7.     void OnCompleted();
  8.     //
  9.     // Summary:
  10.     //     Notifies the observer that an exception has occurred.
  11.     void OnError(Exception exception);
  12.     //
  13.     // Summary:
  14.     //     Notifies the observer of a new value in the sequence.
  15.     void OnNext(T value);
  16. }

 

actually we getting formalized pattern for push collection (collection that may be endless)

which can push data into our IObserver<T> instance.

on of the benefit of this formalization is that we can use Linq query upon that push collection.

we will discuss IObservable<T> and IObserver<T> in more details on the following post of this series.

 

Interactive Vs. Reactive (pull Vs. Push)

we will discuss Interactive (IEnumerable<T>) Vs. Reactive (IObservable<T>) paradigms

by using journal reading analogy.

let say that you do like to read several journals at your home each day.

before the reading part the journals should reach your home.

you do have 2 options (Interactive or Reactive approach):

 

  • Interactive: going each day to the nearby store (or several stores in
    case that the nearby store does not have all of your favorite journals), collect the journals,
    wait on the payment queue, pay for it and return home for reading.

Rx, reacrive , IObservable, IObserver

 

  • Reactive: subscribe to journal delivery service which will send the journals to
    your door front each day.

Reactive Extensions for .NET (Rx)

 

Summary

the Reactive model is more native for parallel computing

and it can save the collecting time:

  • go to the store
  • wait on the payment queue (in the software world waiting for blocking threads)

in the next pasts we will discuss more benefit, scenarios and techniques.

 

How to get started?

in order of start using the Rx you have to download the library from here (it is available both for framework 3.5 and 4.0).

then you have to add reference to System.Reactive

 

 

kick it on DotNetKicks.com


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*