C# - Dabble with IObservable<T> and IObserver<T>
Delegate offers some nice features of observable pattern, however, there are some limiation on the delgate implemented observable patterns.
?
Why not Delegate?
For onething, there is no way to know when the subject no longer tick; the subject may become out of interest if it does not tick updates; But with the passive mode of delegate, you do not know that a subject is become inactive as the pattern itself is passive (there are some workaround , indeed by you provide another event to nofity that the subject is beome inactive);
?
For another, there is no way to descrimate against a valid udpate from error conditions, using some sentry value as the update event args could serve in some cases, but that is a botch/tinker/bumble/bungle rather than good solution, it is a smell design to intermingle the normal message channel with the error message channel;
?
for yet another, there is no way to know when/how to remove the IObserver from the notification list, a simple -= may unsusbscribe the event listener, but there would exist a huge amount of house keeping work that needs to be addresseed when an Observer go out of scope. Better, a Disposable is better in situation like this;?
?
?
IObservable<T> and IObserver<T> pattern provides the cure;
?
It has provide the following.
IObservable<T>.Subscribe(IObserver<T> observer): this notify the IObserver to add the observer to its notification list;the return value of IDisposable, which satisify the the question of "how to remove teh IObserver from the notification list";
IObserver<T>.OnComplete: This notifies that the provider has finished sending push-based notificationthis has the functionality of telling the observers when the subject (provider) will/has become inactive. and on receiving the information, teh IObserver can use the IDispose method that it receives in the Subscribe step to unsubscibe itself from the notification and do the proper uninitialization.
IObserver<T>.OnErro(Exception erorr): this satisfy the question of how to notify the client when some error condition has happens.?IObserver<T>.OnNext(): this satisfy the basic requirement of the Provider notifies the clien when there is update coming.?
Below shows the code an example IObservable<T> implementation, the T is of type Location, which represent some Location information.
?
?
?
namespace PushBasedNotificationTest{ class Program { static void Main(string[] args) { // Define a provider and two observers LocationTracker provider = new LocationTracker(); LocationReporter reporter1 = new LocationReporter("FixedGPS"); reporter1.Subscribe(provider); LocationReporter reporter2 = new LocationReporter("MobileGPS"); reporter2.Subscribe(provider); provider.TrackLocation(new Location(47.6456, -122.1312)); reporter1.Unsubscribe(); provider.TrackLocation(new Location(47.6677, -122.1199)); provider.TrackLocation(null); // trigger the error condition provider.EndTransmission(); } }}?
?
?
?