DCSIMG
Rx - for beginners (part 3): IObservable Vs. IEnumerable - Bnaya Eshet

Bnaya Eshet

Disclaimer

Rx - for beginners (part 3): IObservable Vs. IEnumerable

Rx - for beginners (part 3): IObservable Vs. IEnumerable

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

the series TOC can found here.

this post will focus on how exactly the IObservable/IObserver mirror IEnumerable/IEnumerator.

 1266073365_kghostview 

IEnumerator operations

IEnumerable expose the following operations:

Code Snippet
  1. public interface IEnumerator<T> : IDisposable, IEnumerator
  2. {
  3.     T Current { get; }
  4. }
  5.  
  6. public interface
  7. {
  8.     object Current { get; }
  9.     bool MoveNext();
  10.     void Reset();
  11. }
  • Move next: indicate whether the operation completed
    (no more item available on the item source).
    and it may also throw Exception if something have got wrong.
  • Current: is handing the current item.
  • Reset: is restarting the iteration.

 

IObserver mirrored operations

IObserver is mirroring IEnumerator, instead of asking the IEnumerator,

IObserver will tell you what's is happening.

this is known as "don't call us we will call you" :-)

Code Snippet
  1. public interface IObserver<T>
  2. {
  3.     void OnCompleted();
  4.     void OnError(Exception exception);
  5.     void OnNext(T value);
  6. }

the IObserver is actually a callback interface which will be called by the items feeder,

whenever something occurs.

  • On complete: is mirroring move next, by notifying that the feeding source
    has no more items.
  • On error: is also mirroring move next, but this time it mirror the move next throw exception behavior.
  • On next: is mirroring the current property, by pushing new value into
    the the observer OnNext implementation.
  • because of the push nature of the IObserver the reset does not mirrored.

 

IEnumerable Operations
Code Snippet
  1. public interface IEnumerable<T> : IEnumerable
  2. {
  3.     IEnumerator<T> GetEnumerator();
  4. }

IEnumerable has one method that return IEnumerator with can be use

to iterate through the items by pulling the items sequentially.

 

IObservable mirrored operations
Code Snippet
  1. public interface IObservable<T>
  2. {
  3.     IDisposable Subscribe(IObserver<T> observer);
  4. }

  • through IObservable we can subscribe to content feeders by handing
    implementation IObserver, then the feeder can use the IObserver interface
    for each of the operation we discussed earlier.
  • the subscribe operation return IDisposable  instance that is used
    for unsubscribe the observer from the feeder.
    this is match safer then using unsubscribe method because it doesn't has side effects, 
    just think of what's happens when you subscribe anonymous delegate

    in fact the disposable instance is mirroring IEnumerator<T> which is inheriting from IDisposable.

 

Summary

by mirroring IEnumerable/IEnumerator, IObservable/IObserver can change the data

flow direction from pull to push.

 

Code Sample

you can find code sample that implement simple scenario of feeder

that feed increasing integer sequence as long as the integer value is below 100.

the solution contain 2 projects, one implement the scenario using enumerable and the

other is using observable.

the code sample can be download from here.

 

תגים של Technorati:‏ ,,

 

kick it on DotNetKicks.com


Comments

No Comments