reactive extensions
DESCRIPTION
The Reactive Extension library in .NETTRANSCRIPT
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
…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
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
IOBSERVABLE<T>
• Interface representing a collection that you can “observe”
• IDisposable Subscribe(IObserver<Type> observer) only interface method
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
EXAMPLE!
• Twitter Firehose API Example
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)
OBSERVABLE
• Example Code!
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!
RX AND LINQ
• Rx adds a few new LINQ extension methods, as well as uses the existing ones
• Example Code!
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!
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