1 / 26

Reactive Extensions

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,

jalila
Télécharger la présentation

Reactive Extensions

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. Reactive Extensions Ye oldeintroduction 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 Compose, LINQ, Asynchronous 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. Whyshould I use it? Events arehigh-maintenance Enumeratingcollections is a blocking operation Observablesareasynchronous by nature, Observablesintroduce the notionoftime

  8. The assemblies

  9. Whatyouneedtoknow(show me the code) • The Interfaces • The Contract OnNext* (OnError | OnCompleted)? Publishnextvalue An Erroroccured And we’redone

  10. Migratinginto the Observable • Strategy: movetricky problem intoobservabledomain, solve problem easily, move back toregulardomain • Methodsexist for converting existing objectstoobservableobjects

  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(yoube illin’) • Dissecting the OnError lambda • Error handling strategy • Keep it local

  14. Code Demo – Error Handling

  15. Schedulers(timeis of the essence) • Calls areasynchronous, so… • Where do observablesrun? • Observable.Return(42) OnNext(42) OnCompleted

  16. Code Demo – Schedulers

  17. 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

  18. Code Demo – Testing Schedulers

  19. ResourceDisposal(I must clean) • In Essence: how and when do youUnsubscribe? • OnCompletedAutoDispose • Whathappensifyoudon’t? • Best practices

  20. Code Demo – Unsubscribe

  21. 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

  22. Code Demo – Sharing resources • hot and cold • subjects

  23. Code Demo • Search Twitter • Common Operators

  24. 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

  25. Further Reading • introtorx.com • http://social.msdn.microsoft.com/Forums/en-US/rx/threads • http://rxx.codeplex.com/ • http://www.reactiveui.net/

  26. The End www.capsor.se @capsorab

More Related