260 likes | 361 Views
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.
E N D
Introduction to Rx Reactive Extension to .NET
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 .NET 3.5 .NET 4.0 Silverlight 3 Silverlight 4 Windows Phone JavaScript!
Linq benefits Composable Transformative Declarative Extensible Unitive Integrated
Why Rx? Why would you use Rx and why do you care?
Asynchronous Data Data being pushed Async requests
Responsive UI WPF or Silverlight GUIs AJAX
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
...so what? Observer pattern is old news Observer pattern is ~15years old Java has already got the interface .NET has events
But... The Java interface is bloated .NET events are pretty silly.
Core bits of Rx IObserver<T> IObservable<T> ISubject<T1, T2> Factory Methods Extension Methods Scheduling and Concurrency
IObserver<T> namespace System { public interface IObserver<in T> { void OnNext(T value); void OnError(Exception error); void OnCompleted(); } }
IObservable<T> namespace System { public interface IObservable<out T> { IDisposableSubscribe( IObserver<T> observer); } }
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>{ } }
Factory Methods Generate / GenerateWithTime Range / Interval Create / CreateWithDisposable Empty / Return / Never / Throw FromAsyncPattern / FromEvent
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
Scheduling and Concurrency namespaceSystem.Concurrency { public interface IScheduler { DateTimeOffset Now { get; } IDisposableSchedule(Action action); IDisposableSchedule( Action action, TimeSpandueTime); } }
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
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)
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”
Thanks to... Mitch Wheat Erik Meijer’s team at Microsoft Lab49
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)