Reactive Extension to .NET

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

Attie Naude 14 May 2013 Windows Azure Mobile Services.
Slides license: Creative Commons Attribution Non-Commercial Share Alike See
Reactive Extensions (Rx) for Silverlight Tim Greenfield Vertigo Software.
1 Reactive Programming with Rx (based on Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction.
Silverlight is dead! Long live MVVM!
Het Asynchrone Microsoft Landschap Kees Dijk Senior Software Developer, Vivens
An Introduction to the Reactive Extensions Ivan Towlson Mindscape.
James Kolpack, InRAD LLC popcyclical.com. CodeStock is proudly partnered with: Send instant feedback on this session via Twitter: Send a direct message.
Developing HTML5 Application using MVVM pattern Pekka Ylenius.
Bing it on, Reactive Extensions Building a quick search app from scratch in WPF with Rx and Bing.com.
How to be a C# ninja in 10 easy steps. Benjamin Day.
Rx Framework Reactive Extensions for.Net and JavaScript Andrea Nasoni & Diego Guidi.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Curing Your Event Processing Blues with Reactive Extensions (Rx)
Aptech Borivali(West) Hefin Dsouza. Agenda  What is.NET and What is Visual Studio? .NET Framework 3.5 Overview.  Visual Studio 2008 Enhancements. 
CVSQL 2 The Design. System Overview System Components CVSQL Server –Three network interfaces –Modular data source provider framework –Decoupled SQL parsing.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
AJAX in ASP.NET James Crowley Developer Fusion
September 15, 2015 Laszlo Overview. 2 Copyright (c) 2007 Laszlo Systems, Inc. Laszlo Systems: Leader in RIA Software Pioneer of Rich Internet Applications.
LINQ, Take Two Realizing the LINQ to Everything Dream Bart J.F. De Smet Senior Software Development Engineer Microsoft Corporation.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Building Web Sites with ASP.NET MVC Framework Noam King CTO Sela College
Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.
Web Mashups -Nirav Shah.
Week 3, Day 1: Processes & Threads Return Quiz Processes Threads Lab: Quiz Lab 3: Strategy & Factory Patterns! SE-2811 Slide design: Dr. Mark L. Hornick.
Functional Programming Shane Carr CSE 232, September 4, 2015.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Monads Steve Goguen. About Me Web Developer for Allied Building Supply  ASP.NET, SQL Server, LINQ, etc. Website:
Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Reactive Extensions (Rx) Explained Presenter: Kevin Grossnicklaus August 5 th, 2011.
LINQ, Take Two Realizing the LINQ to Everything Dream Bart J.F. De Smet blogs.bartdesmet.net/bart
How to be a C# ninja in 10 easy steps Benjamin Day.
Paul Using Rx; 8 November 2014 Raleigh Code Camp.
1 Lecture 21 Reactive Programming with Rx Duality between Push and Pull models, Iterable vs. Observable. Composing two asynchronous services. Ras Bodik.
Please visit m.ausalgo.com on your device and sign inm.ausalgo.com.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
1 Lecture 22 Reactive Programming with Rx Duality between Push and Pull models, Iterable vs. Observable. Composing two asynchronous services. Ras Bodik.
LINQ & PLINQ (Parallel) Language Integrated Query.
TAP into async programming
Java8 Released: March 18, Lambda Expressions.
Linq Overview Vincent GERMAIN. Evolution - Rappel Langage  C# 2.0  C# 3.0 (Local type inference, Lambda expression, Method extension,Anonymous type)
Aggregator  Performs aggregate calculations  Components of the Aggregator Transformation Aggregate expression Group by port Sorted Input option Aggregate.
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2016 © 1.
Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Intro To MVC Architecture.
Patterns in programming
DotNetSpider Editor Hefin Dsouza
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2016
Web Mashups -Nirav Shah.
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2013
Advanced Topics in Concurrency and Reactive Programming: The Observable Contract Majeed Kassis.
Intro to LINQ Part 2 – Concepts and LINQ to Objects
RxJS 5 JSConf China 2016.
RADE new features via JAVA
Out-of-Process Components
Reactive Android Development
WCF (Indigo): Under the Hood of the Channel Layer
Advanced Topics in Concurrency and Reactive Programming: ReactiveX
Reactive microservice end to end from RxJava to the wire with gRPC
Reactive Extensions for .NET
Advanced Topics in Concurrency and Reactive Programming
DEV422 Becoming a C# Time Lord Joe Albahari 2/5/ :46 AM
Out-of-Process Components
Rx Java intro Vaidas Kriščeliūnas.
WCF Data Services and Silverlight
Server & Tools Business
ERROR HANDLING AND DEBUGGING
Presentation transcript:

Reactive Extension to .NET 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 In my opinion, the easiest way to introduce the Reactive Extensions to .NET (here on in called Rx), is to describe it as an implementation of the Observer pattern. It allows you to provide delegates & interfaces to aid in communicating events and asyncrhonous data flows.

Favours Functional over OO Plenty of extension methods Encourages monadic programming While the Observer pattern is a nice introduction to Rx, Rx is more about functional programming. I think the Observer Pattern was really aimed at OO Languages like Java/C. The Observer pattern helps two classes communicate with each other, however you will see that with Rx queries that they are more interesting from a Functional rather than Object Orientated view point.

Multiple targets .NET 3.5 .NET 4.0 Silverlight 3 Silverlight 4 Windows Phone JavaScript! As the .NET language matures to allow better Functional syntax support it also allows wide support across it’s various build. The dynamic nature of JavaScript also makes it a prime candidate to target too. We wont cover any of the JavaScript features in this session.

Linq benefits Composable Transformative Declarative Extensible Unitive Integrated Linq was built to provide this benefits, it was also aimed to provide these not just for the IEnumerable interface. As rx has proven, you can easily add new interfaces to it and still get Linq support, specifically via extension methods and Query comprehension syntax.

Why Rx? Why would you use Rx and why do you care?

Asynchronous Data Data being pushed Async requests For any system where you are getting data pushed to you or you are making asynchronous requests, Rx is useful. Examples might be E-Commerce : Changes in Stock or pricing information Instrumentation : Changes in measurements. Computers or Industrial eg Temperatures, Power consumption, Memory, CPU utilisation. Finance : Price ticks, new Orders on the Book, Charting, CEP, Merging data streams to create new data streams (TWAP/VWAP, margining prices)

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. class Observable { void addObserver(Observer o) protected void clearChanged() int countObservers() void deleteObserver(Observer o) void deleteObservers() boolean hasChanged() void notifyObservers() void notifyObservers(Object arg) protected void setChanged() } .Net events have an odd syntax, dont really handle error scenarios dont have the concept of completing Have poor resource management symantics

Core bits of Rx

Core bits of Rx IObserver<T> IObservable<T> ISubject<T1, T2> Factory Methods Extension Methods Scheduling and Concurrency Next we will cover the Core bits of Rx. There is something for everyone GoF fan boys will love the Pattern mania with Iterator, Observable, Decorator, Disposable, Anonymous Implementation (via Factory) and Dependency Injection. Language geeks will be made content with the familiar Linq syntax and extension method explosion (and that .NET 5 is already being catered for with the AwaitableObservables) Concurrency and Multithreading nerds will get off on the Scheduling and Concurrency features Testers will crave the determinism of the TestScheduler

IObserver<T> namespace System { public interface IObserver<in T> void OnNext(T value); void OnError(Exception error); void OnCompleted(); } Can discuss the duality of the IEnumerable<T> interface here. A deep understanding of Iterators will give you a huge boost in understanding Rx. Discuss the Rx grammar and that this is a "formalised" version of the Observer Pattern (Iterator Dual).

IObservable<T> namespace System { public interface IObservable<out T> IDisposable Subscribe( IObserver<T> observer); } Explicitly point out the IDisposable return type that allows for easy resource management. Elude to the fact that there are extension methods for Subscribe that allow you just to pass delegates/actions for the OnNext/Completed/Error.

Subjects namespace System.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 Filter Grouping Composition Flow control 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 ..And include the important one IDisposable Subscribe(Action<T>);

Scheduling and Concurrency namespace System.Concurrency { public interface IScheduler DateTimeOffset Now { get; } IDisposable Schedule(Action action); IDisposable Schedule( Action action, TimeSpan dueTime); } A scheduler implementation effectively allows you to queue or schedule an Action to be performed. Some schedulers will ensure that actions are serialised (ie ones that schedule to the Dispatcher, EventLoop, CurrentThread) and other will not guarantee this (eg NewThread, TaskPool or ThreadPool) .

Scheduler implementations 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 Discuss the difference between the Immediate and CurrentThread implementations. CurrentThread is a trampoline, where Immediate is just actually invoking the Action immediately.

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) Who would immediately benefit from RX? WPF/Silverlight teams 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 data, instrumentation data ie Oil and Gas) Compose many feeds to get the final solution. Easily testable IQbservable may allow you to send the declaration to the server to process (GPUs/FSharp expressions)

Code time Break out to show case some code progressions from non-Rx to Rx, to Complex tested Rx

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” Rx design guidelines is a PDF document produced by the Rx team.

Thanks to... Mitch Wheat Erik Meijer’s team at Microsoft Lab49 Thanks to Mitch for organising this space in such short notice. Thanks to the team at Microsoft for the great work they are doing and thanks to Lab 49 who pay for this laptop and the kit.

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) Download the Extensions at http://msdn.microsoft.com/en-us/data/ (just google Reactive Extensions) or via Nuget See the 8/9 part introduction to Rx at LeeCampbell.Blogspot.com James Miles has some good content at EnumerateThis.com and is also prominent on the Rx Forums which are very active