Implementing The GeoCoordinateWatcher As A Reactive Service
With Rx, events are first class citizens that can be passed around and composed as needed in a very simple way.
“The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developers represent asynchronous data streams with Observables, query asynchronous data streams using LINQ operators, and parameterize the concurrency in the asynchronous data streams using Schedulers. Simply put, Rx = Observables + LINQ + Schedulers.” – from the MSDN page.
The library also provides a considerable amount of helpers that make it easy to warp events into observables.
Wrapping the GeoCoordinateWatcher as a reactive service is quite simple. All it takes is creating observables and exposing the events as observables:
public class GeoCoordinateReactiveService : IGeoCoordinateReactiveService, IDisposable { private readonly GeoCoordinateWatcher geoCoordinateWatcher = new GeoCoordinateWatcher();</span><span style="background: white; color: blue">public </span><span style="background: white; color: black">GeoCoordinateReactiveService() { </span><span style="background: white; color: blue">this</span><span style="background: white; color: black">.StatusObservable = </span><span style="background: white; color: #2b91af">Observable </span><span style="background: white; color: black">.FromEventPattern<</span><span style="background: white; color: #2b91af">GeoPositionStatusChangedEventArgs</span><span style="background: white; color: black">>( handler => geoCoordinateWatcher.StatusChanged += handler, handler => geoCoordinateWatcher.StatusChanged -= handler); </span><span style="background: white; color: blue">this</span><span style="background: white; color: black">.PositionObservable = </span><span style="background: white; color: #2b91af">Observable </span><span style="background: white; color: black">.FromEventPattern<</span><span style="background: white; color: #2b91af">GeoPositionChangedEventArgs</span><span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">GeoCoordinate</span><span style="background: white; color: black">>>( handler => geoCoordinateWatcher.PositionChanged += handler, handler => geoCoordinateWatcher.PositionChanged -= handler); } </span><span style="background: white; color: blue">public </span><span style="background: white; color: #2b91af">IObservable<span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">EventPattern</span></span><span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">GeoPositionStatus</span><span style="background: white; color: black">> StatusObservable { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">private set</span><span style="background: white; color: black">; } </span><span style="background: white; color: blue">public </span><span style="background: white; color: #2b91af">IObservable<span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">EventPattern</span></span><span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">GeoPosition</span><span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">GeoCoordinate</span><span style="background: white; color: black">>> PositionObservable { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">private set</span><span style="background: white; color: black">; }
}
And now, instead of the StatusChanged and PositionChanged events we have respectively the StatusObservable and PositionObservable as a stream of EventPattern<TEventArgs> instances.
But the EventPattern<TEventArgs> class includes the source of the event and an the event arguments in properties which is far more than we need in this case. With normal LINQ operators we can convert thes streams of EventPattern<TEventArgs> instances in streams of the desired values.
public class GeoCoordinateReactiveService : IGeoCoordinateReactiveService, IDisposable { private readonly GeoCoordinateWatcher geoCoordinateWatcher = new GeoCoordinateWatcher();</span><span style="background: white; color: blue">public </span><span style="background: white; color: black">GeoCoordinateReactiveService() { </span><span style="background: white; color: blue">this</span><span style="background: white; color: black">.StatusObservable = </span><span style="background: white; color: #2b91af">Observable </span><span style="background: white; color: black">.FromEventPattern<</span><span style="background: white; color: #2b91af">GeoPositionStatusChangedEventArgs</span><span style="background: white; color: black">>( handler => geoCoordinateWatcher.StatusChanged += handler, handler => geoCoordinateWatcher.StatusChanged -= handler) <strong>.Select(ep => ep.EventArgs.Status);</strong> </span><span style="background: white; color: blue">this</span><span style="background: white; color: black">.PositionObservable = </span><span style="background: white; color: #2b91af">Observable </span><span style="background: white; color: black">.FromEventPattern<</span><span style="background: white; color: #2b91af">GeoPositionChangedEventArgs</span><span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">GeoCoordinate</span><span style="background: white; color: black">>>( handler => geoCoordinateWatcher.PositionChanged += handler, handler => geoCoordinateWatcher.PositionChanged -= handler) <strong>.Select(ep => ep.EventArgs.Position);</strong> } </span><span style="background: white; color: blue">public </span><span style="background: white; color: #2b91af">IObservable<span style="background: white; color: black"><</span></span><span style="background: white; color: #2b91af"><strong>GeoPositionStatus</strong></span><span style="background: white; color: black">> StatusObservable { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">private set</span><span style="background: white; color: black">; } </span><span style="background: white; color: blue">public </span><span style="background: white; color: #2b91af">IObservable<span style="background: white; color: black"><</span></span><strong><span style="background: white; color: #2b91af">GeoPosition</span><span style="background: white; color: black"><</span><span style="background: white; color: #2b91af">GeoCoordinate</span></strong><span style="background: white; color: black"><strong>></strong>> PositionObservable { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">private set</span><span style="background: white; color: black">; }
}
And to use these observables all it is needed is to subscribe to them:
geoCoordinateWatcherService.StatusObservable .Subscribe(this.OnStatusChanged); geoCoordinateWatcherService.PositionObservable .Subscribe(this.OnPositionChanged);
But, usually, we want to use these values in view model to bind to the UI and, consequently, we want this to happen in the UI thread:
geoCoordinateWatcherService.StatusObservable .ObserveOnDispatcher() .Subscribe(this.OnStatusChanged); geoCoordinateWatcherService.PositionObservable .ObserveOnDispatcher() .Subscribe(this.OnPositionChanged);
It’s as simple as that!
Resources: