Download presentation
Presentation is loading. Please wait.
Published byAlexina Golden Modified over 9 years ago
1
Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code
2
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
3
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
4
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.
5
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
6
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
7
Why should I use it? Events are high-maintenance Enumerating collections is a blocking operation Observables are asynchronous by nature, Observables introduce the notion of time
8
The assemblies
9
What you need to know (show me the code) ● The Interfaces ● The Contract OnNext* (OnError | OnCompleted)? Publish next value An Error occured And we’re done
10
Migrating into the Observable ● Strategy: move tricky problem into observable domain, solve problem easily, move back to regular domain ● Methods exist for converting existing objects to observable objects
11
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:
12
Code Demo – Creating Observables From raw data From events From async patterns Custom observables
13
Error handling (you be illin’) ● Dissecting the OnError lambda ● Error handling strategy ● Keep it local
14
Code Demo – Error Handling
15
Schedulers (time is of the essence) ● Calls are asynchronous, so… ● Where do observables run? ● Observable.Return(42) OnNext(42 ) OnCompleted
16
Code Demo – Schedulers
17
Unit Testing (you better check yourself, before you wreck yourself) ● TestScheduler - allows testing of asynchronous code in real-time ● Helps you learn and understand the inner workings of the Rx libraries. ● This is what’s available: ● NewThreadScheduler ● TaskPoolScheduler ● DispatcherScheduler ● ImmediateScheduler ● ThreadPoolScheduler
18
Code Demo – Testing Schedulers
19
Resource Disposal (I must clean) ● In Essence: how and when do you Unsubscribe? – OnCompleted AutoDispose ● What happens if you don’t? ● Best practices
20
Code Demo – Unsubscribe
21
Hot vs Cold ● There are two types of streams: ● Hot – producing values all the time, regardless of subscribers – Examples: mouse events, streaming data ● Cold – producing values only when someone is subscribing – Examples: web requests ● Important to know the difference, especially when it comes to side effects
22
Code Demo – Sharing resources hot and cold subjects
23
Code Demo Search Twitter Common Operators
24
Why do I recommend Rx? ● Allows me to write modular, reusable code, which is composable ● Allows me to orchestrate message flows ● Relieves me of a lot of state management ● Allows me to write fully asynchronous UI’s from day one. ● Provides full unit testing of timeouts, delays, etc ● Is feature rich => less work for me
25
Further Reading ● introtorx.com introtorx.com ● http://social.msdn.microsoft.com/Forums/en-US/rx/threads http://social.msdn.microsoft.com/Forums/en-US/rx/threads ● http://rxx.codeplex.com/ http://rxx.codeplex.com/ ● http://www.reactiveui.net/ http://www.reactiveui.net/
26
The End www.capsor.se @capsorab
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.