1 / 26

Introduction to Rx

Introduction to Rx. Reactive Extension to .NET. What is Rx?. Observer pattern implementation . Hollywood principle Allows you to provide delegates to be called by another object. Favours Functional over OO. Plenty of extension methods Encourages monadic programming. Multiple targets.

stormy
Download Presentation

Introduction to Rx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Rx Reactive Extension to .NET

  2. What is Rx?

  3. Observer pattern implementation Hollywood principle Allows you to provide delegates to be called by another object

  4. Favours Functional over OO Plenty of extension methods Encourages monadic programming

  5. Multiple targets .NET 3.5 .NET 4.0 Silverlight 3 Silverlight 4 Windows Phone JavaScript!

  6. Linq benefits Composable Transformative Declarative Extensible Unitive Integrated

  7. Why Rx? Why would you use Rx and why do you care?

  8. Asynchronous Data Data being pushed Async requests

  9. Responsive UI WPF or Silverlight GUIs AJAX

  10. Composition of streams of data • Concatenation • Concat, OnError/Catch, Retry • Merging streams • Merge, Zip, SelectMany, Amb, CombineLatest, ForkJoin • Grouping stream data • GroupBy, Window, Buffer, GroupJoin

  11. ...so what? Observer pattern is old news Observer pattern is ~15years old Java has already got the interface .NET has events

  12. But... The Java interface is bloated .NET events are pretty silly.

  13. Core bits of Rx

  14. Core bits of Rx IObserver<T> IObservable<T> ISubject<T1, T2> Factory Methods Extension Methods Scheduling and Concurrency

  15. IObserver<T> namespace System { public interface IObserver<in T> { void OnNext(T value); void OnError(Exception error); void OnCompleted(); } }

  16. IObservable<T> namespace System { public interface IObservable<out T> { IDisposableSubscribe( IObserver<T> observer); } }

  17. Subjects namespaceSystem.Collections.Generic { public interface ISubject<in T1, out T2>  : IObserver<T1>, IObservable<T2>{  } public interface ISubject<T>  : ISubject<T, T>,  IObserver<T>,  IObservable<T>{ } }

  18. Factory Methods Generate / GenerateWithTime Range / Interval Create / CreateWithDisposable Empty / Return / Never / Throw FromAsyncPattern / FromEvent

  19. Extension Methods • Aggregates • Aggregate, Sum, Count, Max, Min, First, Last... • Filter • Where, Skip, Take, First, Last • Grouping • Window, Buffer, GroupJoin • Composition • Concat, Merge, • Flow control • Amb, Case, If, While

  20. Scheduling and Concurrency namespaceSystem.Concurrency { public interface IScheduler { DateTimeOffset Now { get; } IDisposableSchedule(Action action); IDisposableSchedule( Action action, TimeSpandueTime); } }

  21. Scheduler implementations namespace System.Concurrency { public static class Scheduler { public static ImmediateScheduler Immediate; public static CurrentThreadSchedulerCurrentThread; public static ThreadPoolSchedulerThreadPool; public static NewThreadSchedulerNewThread; public static TaskPoolSchedulerTaskPool; public static DispatcherScheduler Dispatcher; } } //EventLoopScheduler //TestScheduler //ControlScheduler

  22. Who would benefit? • WPF/Silverlight developers • Allowing Rx to schedule work on to the ThreadPool/TaskPool • Return results on the dispatcher • Easily testable (without pushing a Dispatcher frame!) • Serverside teams working on streaming data (finance, instrumentation) • Compose many feeds • Easily testable • IQbservable may allow you to send the declaration to the server to process (GPUs/FSharp expressions)

  23. Code time

  24. Best Practices Use marble diagrams (esp first 12 months) Honour the Completion contracts Apply scheduling only at the end consumer Avoid breaking the monad (.First() etc) Avoid Side effects (or be explicit via .Do()) Favour composition over creating new operators Avoid use of Subjects, favour factories. Google “Rx Design Guidelines”

  25. Thanks to... Mitch Wheat Erik Meijer’s team at Microsoft Lab49

  26. More content Data Developer Center(for the download or get via Nuget) LeeCampbell.blogspot.com EnumerateThis.com (or just talk to James) Rx Forums (which is the same as talking to James)

More Related