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.
IEnumerator operations
IEnumerable expose the following operations:
Code Snippet
- public interface IEnumerator<T> : IDisposable, IEnumerator
- {
- T Current { get; }
- }
-
- public interface
- {
- object Current { get; }
- bool MoveNext();
- void Reset();
- }
- 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
- public interface IObserver<T>
- {
- void OnCompleted();
- void OnError(Exception exception);
- void OnNext(T value);
- }
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
- public interface IEnumerable<T> : IEnumerable
- {
- IEnumerator<T> GetEnumerator();
- }
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
- public interface IObservable<T>
- {
- IDisposable Subscribe(IObserver<T> observer);
- }
- 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:
IObservable,
IObserver,
Rx