270 likes | 418 Views
Reactive Extensions. Ye olde introduction and walk-through, with plenty o’ code. OK, what is it?. A library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators. Keywords: Compose, LINQ, Asynchronous,
E N D
Reactive Extensions Ye oldeintroduction and walk-through, with plenty o’ code
OK, what is it? A library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators. Keywords: Compose, LINQ, Asynchronous, Compose, LINQ, Asynchronous Compose, LINQ, Asynchronous Compose, LINQ, Asynchronous Compose, LINQ, Asynchronous
Reactive what? Wikipedia: a programming paradigm oriented around data flows and the propagation of change Interactive: a = b + c Meaning: a will store the current values of b and c (added), but future updates to b and c will not affect a Reactive: Meaning: a will be updated when either b or c (or both) changes in the future
Reactive vs Interactive, part II(push vs pull) Interactive (pull, compare with polling) Reactive (push, compare with notifications) Key concept: create a query now, receive updates in the future.
The library Created by Microsoft Built on Parallel extensions library (PFX, meaning fully multi-threaded from day one) Open source as of November 2012 Based on functional programming Runs on Windows Forms, WPF, Silverlight, WP7, WP8, JavaScript, ASP.NET Is written using extension methods
Why should I use it? Today’s applications are complex Managing applications with complex message flows, threads and states tend to get real messy real soon An asynchronous and responsive UI is a necessity for the demanding users (and apps) of today Using a library that handles threading and synchronization for you leads to using fewer locks (and having fewer dead-locks or bottlenecks) Allows you to orchestrate message flows in an easy manner
Whyshould I use it? Events arehigh-maintenance Enumeratingcollections is a blocking operation Observablesareasynchronous by nature, Observablesintroduce the notionoftime
Whatyouneedtoknow(show me the code) • The Interfaces • The Contract OnNext* (OnError | OnCompleted)? Publishnextvalue An Erroroccured And we’redone
Migratinginto the Observable • Strategy: movetricky problem intoobservabledomain, solve problem easily, move back toregulardomain • Methodsexist for converting existing objectstoobservableobjects
Creating Observables( - what is it really?) Think 'streams' (of events, data, etc) Do not implement your own IObservable, use available combinators, or the factory methods:
Code Demo – Creating Observables • From raw data • From events • From async patterns • Custom observables
Error handling(yoube illin’) • Dissecting the OnError lambda • Error handling strategy • Keep it local
Schedulers(timeis of the essence) • Calls areasynchronous, so… • Where do observablesrun? • Observable.Return(42) OnNext(42) OnCompleted
UnitTesting(youbettercheck yourself, beforeyouwreckyourself) • TestScheduler - allowstestingofasynchronouscode in real-time • Helpsyoulearn and understand the inner workingsof the Rxlibraries. • This is what’savailable: • NewThreadScheduler • TaskPoolScheduler • DispatcherScheduler • ImmediateScheduler • ThreadPoolScheduler
ResourceDisposal(I must clean) • In Essence: how and when do youUnsubscribe? • OnCompletedAutoDispose • Whathappensifyoudon’t? • Best practices
Hot vs Cold • Therearetwotypesofstreams: • Hot – producingvalues all the time, regardlessofsubscribers • Examples: mouse events, streaming data • Cold – producingvaluesonlywhensomeone is subscribing • Examples: web requests • Importanttoknow the difference, especiallywhen it comestosideeffects
Code Demo – Sharing resources • hot and cold • subjects
Code Demo • Search Twitter • Common Operators
Why do I recommendRx? • Allowsmetowritemodular, reusablecode, which is composable • Allowsmetoorchestratemessageflows • Relievesmeof a lotofstate management • AllowsmetowritefullyasynchronousUI’s from dayone. • Provides full unittestingof timeouts, delays, etc • Is feature rich => less work for me
Further Reading • introtorx.com • http://social.msdn.microsoft.com/Forums/en-US/rx/threads • http://rxx.codeplex.com/ • http://www.reactiveui.net/
The End www.capsor.se @capsorab