26
Introduction to Rx Reactive Extension to .NET

Reactive Extension to.NET. Hollywood principle Allows you to provide delegates to be called by another object

Embed Size (px)

Citation preview

  • Slide 1

Reactive Extension to.NET Slide 2 Slide 3 Hollywood principle Allows you to provide delegates to be called by another object Slide 4 Plenty of extension methods Encourages monadic programming Slide 5 .NET 3.5 .NET 4.0 Silverlight 3 Silverlight 4 Windows Phone JavaScript! Slide 6 Composable Transformative Declarative Extensible Unitive Integrated Slide 7 Why would you use Rx and why do you care? Slide 8 Data being pushed Async requests Slide 9 WPF or Silverlight GUIs AJAX Slide 10 Concatenation Concat, OnError/Catch, Retry Merging streams Merge, Zip, SelectMany, Amb, CombineLatest, ForkJoin Grouping stream data GroupBy, Window, Buffer, GroupJoin Slide 11 Observer pattern is ~15years old Java has already got the interface .NET has events Slide 12 The Java interface is bloated .NET events are pretty silly. Slide 13 Slide 14 IObserver IObservable ISubject Factory Methods Extension Methods Scheduling and Concurrency Slide 15 namespace System { public interface IObserver { void OnNext(T value); void OnError(Exception error); void OnCompleted(); } Slide 16 namespace System { public interface IObservable { IDisposable Subscribe( IObserver observer); } Slide 17 namespace System.Collections.Generic { public interface ISubject : IObserver, IObservable { } public interface ISubject : ISubject, IObserver, IObservable { } } Slide 18 Generate / GenerateWithTime Range / Interval Create / CreateWithDisposable Empty / Return / Never / Throw FromAsyncPattern / FromEvent Slide 19 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 Slide 20 namespace System.Concurrency { public interface IScheduler { DateTimeOffset Now { get; } IDisposable Schedule(Action action); IDisposable Schedule( Action action, TimeSpan dueTime); } Slide 21 namespace System.Concurrency { public static class Scheduler { public static ImmediateScheduler Immediate; public static CurrentThreadScheduler CurrentThread; public static ThreadPoolScheduler ThreadPool; public static NewThreadScheduler NewThread; public static TaskPoolScheduler TaskPool; public static DispatcherScheduler Dispatcher; } //EventLoopScheduler //TestScheduler //ControlScheduler Slide 22 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) Slide 23 Slide 24 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 Slide 25 Mitch Wheat Erik Meijers team at Microsoft Lab49 Slide 26 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)