Reactive Extensions (Rx) Explained Presenter: Kevin Grossnicklaus August 5 th, 2011.

Slides:



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

Reactive Extension to .NET
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.
Het Asynchrone Microsoft Landschap Kees Dijk Senior Software Developer, Vivens
An Introduction to the Reactive Extensions Ivan Towlson Mindscape.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
Silverlight Presenter: Kevin Grossnicklaus February 25 th, 2010.
−Kevin Grossnicklaus −ArchitectNow- (2009-Present) −President −Washington.
Bing it on, Reactive Extensions Building a quick search app from scratch in WPF with Rx and Bing.com.
Silverlight 5: What’s new? Presenter: Kevin Grossnicklaus June 25 th, 2011.
How to be a C# ninja in 10 easy steps. Benjamin Day.
Demystifying the .NET Asynchronous Programming Landscape
Startup Tips and Tricks: Getting a Small IT Shop Off the Ground by Kevin Grossnicklaus.
.NET 2010 What can we expect for 2010? Kevin Grossnicklaus November 30 th, 2009.
Rx Framework Reactive Extensions for.Net and JavaScript Andrea Nasoni & Diego Guidi.
Principles of Object-Oriented Software Development The language Java.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Curing Your Event Processing Blues with Reactive Extensions (Rx)
Startup Tips and Tricks: Getting a Small IT Shop Off the Ground Presenter: Kevin Grossnicklaus June 25 th, 2011.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
 George Chrysanthakopoulos Software Architect Microsoft Corporation.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
RIA Services Master Class Presenter: Kevin Grossnicklaus November 30 th, 2010.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Eric Vogel Software Developer A.J. Boggs & Company.
Intro to Silverlight Kevin Grossnicklaus. Introductions Kevin Grossnicklaus – SSE - ( ) Chief Architect Software.
LINQ, Take Two Realizing the LINQ to Everything Dream Bart J.F. De Smet Senior Software Development Engineer Microsoft Corporation.
A Jumpstart to WPF by Kevin Grossnicklaus ArchitectNow.
Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
Monads Steve Goguen. About Me Web Developer for Allied Building Supply  ASP.NET, SQL Server, LINQ, etc. Website:
Jumpstart: Silverlight Presenter: Kevin Grossnicklaus November 6 th, 2010.
Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.
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.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Paul Using Rx; 8 November 2014 Raleigh Code Camp.
WHO WILL BENEFIT FROM THIS TALK TOPICS WHAT YOU’LL LEAVE WITH ASP.NET developers, including Web Forms & MVC History of async programming in.NET How async.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
1 Lecture 22 Reactive Programming with Rx Duality between Push and Pull models, Iterable vs. Observable. Composing two asynchronous services. Ras Bodik.
Kevin Grossnicklaus and a “Lot of Web Development Stuff” Presenter: Kevin Grossnicklaus.
LINQ & PLINQ (Parallel) Language Integrated Query.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design Software University
Introduction to All Things Azure Presenter: Kevin Grossnicklaus.
Mail Web Twitter TypeScript Rainer Stropek software architects gmbh JavaScript on Steroids.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
How to develop a VoIP softphone in C# that enables SIP Instant Messaging (IM) This presentation describes how to create a softphone in C# that allows you.
RxJava and SWT: Out with Events, in with FRP
Building Web Applications with Microsoft ASP
Reactive Android Development
12 Asynchronous Programming
Advanced Topics in Concurrency and Reactive Programming: ReactiveX
DEV422 Becoming a C# Time Lord Joe Albahari 2/5/ :46 AM
Mike Ammerlaan Program Manager Microsoft Corporation
Rx Java intro Vaidas Kriščeliūnas.
Server & Tools Business
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

Reactive Extensions (Rx) Explained Presenter: Kevin Grossnicklaus August 5 th, 2011

Agenda Introductions Talk Additional Resources Conclusion

Introductions Kevin Grossnicklaus – ArchitectNow- (2009-Present) President – Washington University - CAIT Program ( ) Instructor – SSE - ( ) Chief Architect Software Development Practice Twitter: blog.architectnow.net blog.architectnow.net Blog: blog.architectnow.netblog.architectnow.net

EXPECTATIONS

Expectations Experience with.NET development – Samples will be in C# 4.0 – VS 2010 will be used Basic familiarity with LINQ and some Async programming Lambda Expressions will be used

INTRODUCTION TO RX

IEnumerable/IEnumerator interface IEnumerable { IEnumerator GetEnumerator(); } interface IEnumerator : IDisposable { bool MoveNext(); T Current { get; } void Reset(); }

Iterating Collections var items = new string[] { “Hello”, “World” }; foreach (var x in items) { //interact with each piece of data } //we can now assume we are done //we must also handle exceptions

IObservable/IObserver interface IObservable { IDisposable Subscribe(IObserver observer); } interface IObserver { void OnNext(T value); void OnError(Exception ex); void OnCompleted(); }

IObservable/IObserver var _data = new string[] { "Hello", "World" }; var _observable = _data.ToObservable(); var _observer = _observable.Subscribe(x => Console.WriteLine(x)); var _observer2 = _observable.Subscribe( x => Console.WriteLine(x), () => Console.WriteLine("Completed")); var _observer3 = _observable.Subscribe( x => Console.WriteLine(x), ex => Console.WriteLine("Exception: " + ex.Message), () => Console.WriteLine("Completed")); _observer2.Dispose();

Getting Rx Installed NuGet – Easiest and quickest Rx Home Page: – Simply add a reference to: – System.Reactive.DLL Available for: – Full Framework (WPF, WinForms, server side ASP.NET, MVC, etc) – Silverlight 3 and 4 – JavaScript – Windows 7 Phone – Xbox/XNA

OBSERVABLE EXTENSIONS

Subjects using System.Reactive.Subjects; var _subject = new Subject (); var _observer = _subject.Subscribe(x => Console.WriteLine(x)); _subject.OnNext("Rx"); _subject.OnNext("will"); _subject.OnNext("save"); _subject.OnNext("me"); _subject.OnNext("some"); _subject.OnNext("headaches"); _subject.OnCompleted(); _observer.Dispose();

Subscribing public static class ObservableExtensions { public static IDisposable Subscribe (this IObservable source); public static IDisposable Subscribe (this IObservable source, Action onNext); public static IDisposable Subscribe (this IObservable source, Action onNext, Action onError); public static IDisposable Subscribe (this IObservable source, Action onNext, Action onCompleted); public static IDisposable Subscribe (this IObservable source, Action onNext, Action onError, Action onCompleted); }

Creation of Observables //simply call OnComplete var empty = Observable.Empty (); //Call OnNext(“Value”) and then call OnComplete var obReturn = Observable.Return("Value"); //Raise no events var never = Observable.Never (); //Call OnException with the specified expection var throws = Observable.Throw (new Exception()); //Specify a delegate to be called when anyone subscribes var createSample = Observable.Create ( observable => { observable.OnNext("a"); observable.OnNext("b"); observable.OnCompleted(); return () => Console.WriteLine("Observer has unsubscribed"); });

Creation of Observables (Cont…) //Create a range of numbers var range = Observable.Range(10, 15); //publish a count from 0 every specified time period var interval = Observable.Interval(TimeSpan.FromMilliseconds(250)); //Never call “Next” but call “Complete” when the long running operation is done var longOperation = Observable.Start( x =>..do something that takes awhile.. ); //Generate a collection much like for (i=5, i<15, i+3) return i.ToString(); var generated = Observable.Generate(5, i => i i.ToString(), i => i + 3); //simply convert an existing collection or array (IEnumerable) to IObservable var converted = MyCollection.ToObservable();

Rx LINQ Operators Where Select First FirstOrDefault Last LastOrDefault Single Count Min Max Sum Where GroupBy Take TakeUntil Skip DistinctUntilChanged Buffer Throttle Sample Delay Until TimeOut..etc…etc…etc…

RX AND.NET EVENTS

The “Old Way” txtSample.TextChanged += new TextChangedEventHandler(txtSample_TextChanged); //txtSample.TextChanged - = new TextChangedEventHandler(txtSample_TextChanged); private string _lastValue = string.Empty; void txtSample_TextChanged(object sender, TextChangedEventArgs e) { var _currentValue = ((TextBox)sender).Text; if (_currentValue.Length > 5 && _currentValue != _lastValue) { _lastValue = _currentValue; lstData.Items.Add(_currentValue); }

The “New Way” var _textChanged = Observable.FromEventPattern (txtSample, "TextChanged").Select(x => ((TextBox)x.Sender).Text); var _longText = _textChanged.Where(x => x.Length > 5).DistinctUntilChanged().Throttle(TimeSpan.FromSeconds(.5)); _longText.ObserveOnDispatcher().Subscribe(x => lstData.Items.Add(x));

ADDITIONAL TOPICS

Additional Topics Threading – Scheduler Async Pattern Attaching/Detaching When should I use Rx?

FINAL THOUGHTS

Additional Resources Rx Homepage – Rx Beginners Guide (Tutorials, Videos, etc) – Great Keynote Overview – programming-blues programming-blues Team Blog – Community Rx Wiki – Channel 9 Videos – RxSandbox – Great Blog Series by Lee Campbell –

Next Steps…