Reactive Extensions

My First experience with Reactive Extensions and .NET 4.5.

What are reactive extensions?

Reactive extension come to collect the commonality between things, if we look at collections, all .NET collections are implement IEnumerable interface, and this collections are pull-based collections, so if we iterate over the collection we need to pull each element we want by GetEnumerator().

However, Reactive Extensions come to replace pull-based collections into push-based collections.

Why?

Because we want to support Async programming model. When the client send request to the server,  the server will push the result (event) to the client when the data ready.

Enumerables  – a pull based world

interface IEnumerable<out T>

{

IEnumerator<T> GetEnumerator();

}

Basically if we look at IEnumerable, this interface is very synchronous interface because the client need to pool the data, and if the data not ready yet,  and we do it on the UI thread then the UI will get stuck.

Observable  – a push based world

    interface IObservable<out T>
    {
        IDisposable Subscribe(IObserver<T> observer);
    }
public interface IObserver<in T>
{
      void OnCompleted();
      void OnError(Exception error);
      void OnNext(T value);
}

In push-based world, we have the Observer that react to events that coming from the
Observable. For example if we subscribe to MSDN magazine rss we post a mail address to react when new magazine come out. So the mail address is the “Call back” and the producer have the ability to “Send event” when new Magazine is out – so the idea is that the data pushed into the Observer.

Therefore, my first application with RX,  is a simple observable and observer:

class Program
{
static void Main(string[] args)
{
//Sender and a reciever of Data
var subject = new Subject<int>();
subject.OnNext(42);
//First subscriber to the observable
var d1 = subject.Subscribe(x => Console.WriteLine(“1>” + x));

subject.OnNext(43);

//Second subscriber
var d2 = subject.Subscribe(x => Console.WriteLine(“2>” + x));

subject.OnNext(44);

d1.Dispose();

subject.OnNext(45);
Console.ReadLine();

}
}

And the result is:

1>43

1>44

2>44

2>45

Conclusion:

With Reactive extension the client can subscribe to events streams coming from the server and in the meantime, the client can do anything else till event coming and the call back method will react to the event from the Observer.

Advertisements

About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in .NET 4.5. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s