reactive extensions

13
REACTIVE EXTENSIONS

Upload: rtigger

Post on 10-May-2015

1.045 views

Category:

Technology


2 download

DESCRIPTION

The Reactive Extension library in .NET

TRANSCRIPT

Page 1: Reactive Extensions

REACTIVE EXTENSIONS

Page 2: Reactive Extensions

PULL…

• Most programming today is based on a “pull” model

• Ask a data source for an item, wait for the item to be returned, and then process it

• Our app is active the entire time

• makes the request,

• waits for the result (usually a blocking operation),

• controls the speed at which we process the result

Page 3: Reactive Extensions

…VS PUSH

• The “push” model aims to make our applications less concerned with the data flow

• Our app asks a data source to notify us if it has data we can use

• The data source is responsible for the notification and sending of data

• Our app is free to continue on its way, and is simply notified if there’s new data

• AKA Observer Pattern

Page 4: Reactive Extensions

REACTIVE EXTENSIONS (RX)

• Some neat baked-in interfaces, extension methods, and static classes for implementing Pull-based (observable pattern) systems

• Originally announced on Nov 17, 2009

• Included in .NET v4.0

Page 5: Reactive Extensions

IOBSERVABLE<T>

• Interface representing a collection that you can “observe”

• IDisposable Subscribe(IObserver<Type> observer) only interface method

Page 6: Reactive Extensions

IOBSERVER<T>

• Interface representing something that can observe an observable

• void OnNext(T data) – called by observable when the next piece of data is ready

• void OnError(Exception ex) – called by observable when an error occurs

• void OnCompleted() – called by observable when the pull operation has completed

Page 7: Reactive Extensions

EXAMPLE!

• Twitter Firehose API Example

Page 8: Reactive Extensions

OBSERVABLE STATIC CLASS

• IObserable/IObserver are all well and good, but we really could’ve done that without Rx

• Observable/Observer provide static methods to create Observables/Observers without class definitions

• Provides a whole bunch of options

• Separate install (can be installed from NuGet)

Page 9: Reactive Extensions

OBSERVABLE

• Example Code!

Page 10: Reactive Extensions

SCHEDULER

• By default, Observable will automatically schedule itself on a certain thread, by default it uses TaskPool

• You can control this by passing in a scheduler

• ImmediateScheduler, CurrentThreadScheduler, DispatcherScheduler, NewThreadScheduler, TaskPoolScheduler, ThreadPoolScheduler

• Example Code!

Page 11: Reactive Extensions

RX AND LINQ

• Rx adds a few new LINQ extension methods, as well as uses the existing ones

• Example Code!

Page 12: Reactive Extensions

OBSERVABLE SUBJECT

• Subject is a proxy class you can use to wrap a non-pull system and create an observable

• Implements both IObservable<T> and IObserver<T>

• Example Code!

Page 13: Reactive Extensions

IQBSERVABLE

• Combines LINQ’s Queryable and Rx’s Observable functionality

• Queryable – allows you to create a query client side using LINQ, and pass that query to a data source (server, database, web service, etc)

• Observable – instead of blocking until the data comes back, will just let you know when it gets the data