Intro to RX

3,114 views

Published on

Introduction to Reactive Extensions for .Net

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,114
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
78
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Intro to RX

  1. 1. Reactive Extensions for .Net<br />Scott Weinstein<br />Principal<br />Lab49<br />weblogs.asp.net/sweinstein / @ScottWeinstein<br />
  2. 2. What is it?<br />The Reactive Extensions for .Net are a new API from MS Dev labs to enable “Linq over Events”<br />Why should I care?<br />Offers a better programming model then Events<br />
  3. 3. Some Concepts<br />
  4. 4. Please sir, can I have some more?<br />
  5. 5. Bid 20, bid 20.2, bid 20.8,…<br />
  6. 6. Pull<br />For pull, data is processed at the leisure of the consumer. <br />The consumer “pulls” the next item from the producer<br />Common examples include:<br />reading from a file<br />summing the numbers in an array<br />iterating though a database query<br />traversing a directory listing<br />paging through an Amazon search<br />
  7. 7. Push <br />For push, data is send via demands of the source. <br />The producer pushes the data to the consumer.<br />Common examples include:<br />Device measurements such as<br />time<br />light <br />heat<br />User triggered data such as <br />Mouse & Keyboard events<br />UI events<br />Sales transactions<br />Asynchronous code<br />
  8. 8. Push & Pull<br />In both scenarios, data moves from the producer to the consumer<br />
  9. 9. Push & Pull in .Net<br />In .Net Pulled data is exposed via common interface pair<br />IEnumerable/IEnumerator<br />There is no other way<br />Foreach and Linq are build on these interfaces<br />Pushed data is exposed via<br />Events<br />Ad-hoc delegate callbacks<br />Ad-hoc subscribe/callback interfaces<br />BeginXXX/EndXXX Async pattern<br />3rd party attempts at Linq for Pushed data (Clinq,Slinq,PushLinq)<br />Each implementation is unique is its own special way<br />
  10. 10. LINQ<br />LINQprovides a composable and standard way to do list manipulations<br />The Reactive Extensions (RX) attempt to<br />Provide a common interface for Pushed data<br />IObservable/IObserver<br />Enable Linq over Pushed data, providing composiblity and standard operators<br />
  11. 11. DemoSimple Temperature Alerts<br />
  12. 12. Events to Observables Demo<br />
  13. 13. Func and Action and Lambdas<br />Func<int> a;<br />Func<int,int,int> add = (a,b) => a+b;<br />Action<T> t;<br />Action <T,T> tt;<br />
  14. 14. Some (Category) Theory<br />There is a formal basis for the RX<br />Duality, as such, is the assertion that truth is invariant under this operation on statements. In other words, if a statement is true about C, then its dual statement is true about Cop. Also, if a statement is false about C, then its dual has to be false about Cop.<br />Informally, these conditions state that the dual of a statement is formed by reversing arrows and compositions.<br />What this means is that if we can create a Dual of IEnumerable then all of Linq will work over pushed data<br />
  15. 15. Dualizing IEnumerator<br />interface IEnumerator<T> <br />{<br /> T Current { get; } // but can throw an exception<br /> bool MoveNext();<br /> void Reset(); // not used<br />}<br />Reverse the arrows<br />interface IObserverDraft1<T> <br />{<br /> SetCurrent(T value); // but need to handle an exception<br /> MoveNext(bool can) ;<br />}<br />
  16. 16. Dualizing IEnumerator (2/2)<br />interface IObserverDraft2<T><br />{<br /> OnNext(T value); <br /> OnError(Exception ex);<br /> MoveNext(bool can) ; //but called every time!<br />}<br />interface IObserver<T> <br />{<br /> OnNext(T value); <br /> OnError(Exception ex);<br /> OnCompleted(); // only called once<br />}<br />
  17. 17. Dualizing IEnumerable<br />interface IEnumerable<T> <br />{<br /> IEnumerator<T> GetEnumerator();<br />}<br />Reverse the arrows<br />public interface IObservableDraft1<T> <br />{<br />// But how do I stop observing?<br /> SetObserver(IObserver<T> obs); <br />}<br />
  18. 18. Dualizing IEnumerable (2/2)<br />interface IObservableDraft2<T> <br />{<br /> Subscribe(IObserver<T> obs);<br />// can I be more composable?<br /> Unsubscribe(IObserver<T> obs); <br />}<br />interface IObservable<T> <br />{<br /> IDisposable Subscribe(IObserver<T> obs);<br />}<br />
  19. 19. Terse functional explanation<br />Enumerable:<br />() –> (() –> Option<T>)<br />FuncEnumerable<T> –> Func<Func<Option<T>>><br />Observable<br />(Option<T> –> ()) –> ()<br />FuncObservable<T> –> Action<Action<Option<T>>><br />
  20. 20. Combinators<br />Demo <br />Implement Where()<br />
  21. 21. Some useful Combinators<br />CombineLatest<br />Do<br />ForkJoin<br />GroupBy<br />Scan<br />HoldUntilChanged<br />Interval<br />Merge<br />ObserveOnDispatcher<br />Sample<br />Throttle<br />Select<br />SelectMany<br />Where<br />Zip<br />
  22. 22. How to create Observables?<br />Create<br />CreateWithDisposable<br />FromAsyncPattern<br />FromEvent<br />Generate<br />ISubject<br />In general, it’s a mistake to create custom implementations of IObservable or IObserver<br />When might it be ok to break this rule?<br />
  23. 23. Streaming OLAP Demo<br />Scan, GroupBy, Where, Zip, Merge, SelectMany<br />
  24. 24. Merge<br />IObservable<T> a<br />IObservable<T> b<br />A.Merge(B) == IObservable<T> ==<br />
  25. 25. Zip<br />IObservable<T> a<br />IObservable<Y> b<br />a.Zip(b, selector) == IObservable<Z> ==<br />
  26. 26. Grouping<br />IObservable<T><br />IGroupedObservable<TKey, TElement><br />Key<br />IObservable<IGroupedObservable<Tkey,TElement>><br />KeyA<br />KeyB<br />KeyC<br />
  27. 27. Hot & Cold<br />Observables (and Enumerables) come in two flavors<br />Hot<br />Values exist outside of subscription<br />Cold<br />Value only exist because of subscription<br />Cold observables can be prone to side-effects<br />ToArray() is one method for making cold Enumerables hot, Replay() and Publish for Observables (and now Enumerables)<br />
  28. 28. Hot and Cold Demo<br />
  29. 29. Code vs. Data<br />RX has an alternate method of representing stream events<br />Interface defines a code-centric view<br />OnNext<br />OnError<br />OnCompleted<br />Notification defines a data-centric view<br />new Notification<T>.OnNext(T next)<br />Where is this useful?<br />
  30. 30. Schedulers<br />RX introduces Schedulers for explicit control<br /> Which thread do subscriptions run on<br />How is time represented<br />ControlSchedulerCurrentThreadScheduler<br />DispatcherSchedulerImmediateScheduler<br />NewThreadSchedulerSynchronizationContextScheduler<br />TaskPoolSchedulerThreadPoolScheduler<br />
  31. 31. Topics not covered<br />Joins<br />Subjects <br />combine Observability and Enumerablility into a single class. <br />BehaviorSubject<T><br />AsyncSubject<T><br />ReplaySubject<T><br />Subject<T><br />
  32. 32. How to get it?<br />Download from the DevLabs: Reactive Extensions for .NET (Rx) site<br />.Net 4<br />In .Net 4 – IObservable/IObserver are part of the BCL<br />.Net 3.5 SP1<br />Silverlight 3<br />Javascript<br />Current license is “go-live” however the API is still evolving<br />
  33. 33. Resources<br />Reactive Extensions for .Net<br />Forums<br />Videos<br />http://weblogs.asp.net/sweinstein<br />
  34. 34. Delayed Expensive Search<br />
  35. 35. DemoAsync webpage download<br />

×