DCSIMG
Rx - for beginners (part 4): anonymous observer handler - Bnaya Eshet

Bnaya Eshet

Disclaimer

Rx - for beginners (part 4): anonymous observer handler

Rx - for beginners (part 4): anonymous observer handler

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

the series TOC can found here.

 

this post will focus on anonymous observer handler.

Rx, IObserver, IObservable

the code sample for this post can be download from here.

 

Anonymous Observer

as we mention on the previous posts, IObserver is used as a callback interface

which can be subscribe to the IObservable,

but this is a bit of overkill, when what we subscribe is relatively small (like Console.Write).

wouldn't it be nice to use Lamda expression instead?

doesn't we want to have something similar to button1.Click += (s,e)=>Console.WriteLine(…); .

 

IObservable extensions

using Lamda expression syntax in order to subscribe delegate into the IObservable is

available because IObservable got the following extension options for its Subscribe method

(under the hood we are speaking about extension methods).

  • Action<TSource> onNext
  • Action<TSource> onNext, Action onCompleted
  • Action<TSource> onNext, Action<Exception> onError
  • Action<TSource> onNext, Action<Exception> onError, Action onCompleted

so assuming we got IObservable instance called foo, the following code is perfectly OK:

  1. IDisposable unsubscribe = foo.Subscribe(value => Console.WriteLine(value));

for simple subscription only for the on next callback.

subscribing both for on next and on error will look as follow:

  1. IDisposable unsubscribe = foo.Subscribe(
  2.     value => Console.WriteLine(value),
  3.     exc => Console.WriteLine(exc));

what is actually happens is that under the hood the subscribe method creating

instance of IObserver, attaching the delegate, and hand it to the IObservable subscribe method.

 

Code Sample

I took the previous post's sample, which look as follow:

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         var obs = new Observer();
  6.         var observable = new FakeObservableFeeder();
  7.         IDisposable unsubscribe = observable.Subscribe(obs);
  8.         Console.ReadKey();
  9.         unsubscribe.Dispose();
  10.     }
  11.     private class Observer : IObserver<int>
  12.     {
  13.         void IObserver<int>.OnCompleted()
  14.         {
  15.             Console.WriteLine("Done");
  16.         }
  17.         void IObserver<int>.OnError(Exception exception)
  18.         {
  19.             Console.WriteLine("Error: " + exception.Message);
  20.         }
  21.         void IObserver<int>.OnNext(int value)
  22.         {
  23.             Console.WriteLine(value);               
  24.         }
  25.     }
  26. }

you can see at line 11, that we implementing IObserver with code one line

for each methods (Console.WriteLine ).

and at line 7, we subscribing this observer into the IObservable instance.

 

Code Sample using anonymous observer

this can be match simpler using anonymous observer, as shown in the following snippet:

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         var observable = new FakeObservableFeeder();
  6.         // subscribing handlers (Anonymous observer)
  7.         IDisposable unsubscribe = observable.Subscribe(
  8.             value => Console.WriteLine(value),
  9.             exc => Console.WriteLine(exc),
  10.             () => Console.WriteLine("Done"));
  11.         Console.ReadKey();
  12.         unsubscribe.Dispose();
  13.     }
  14. }

definitely less code was written.

you can see the subscription at line 7.

  • line 8, is handling the on next action.
  • line 9, is handling the on error action.
  • line 10, is handling the on complete action.

 

Summary

what we have seen is that simple action can be subscribed using anonymous observer handlers.

 

תגים של Technorati:‏ ,,

 

kick it on DotNetKicks.com


Comments

No Comments