As few people told me to implement an observer in my last post where I just showed how to use it, here is the post where I am going to clear out confusions for you. If you have read my other post, you might already know what is an Observer and why it is required. Lets recap this a bit more.

An observer is a container which observes each element individually and notifies you when the object state is modified. The observer should contain methods that enables you to subscribe or unsubscribe individually so that when you subscribe for a notification, it will keep on creating notification until you explicitly unsubscribe the Observer.

In .NET base class library, there are two interfaces introduced viz, IObservable and IObserver. These interfaces gives you a standard to develop Observable pattern and also recommends you to use it rather than doing it of your own. Microsoft also builds forth its Reactive Framework (I will discuss about it later) based on Observer pattern and lets us use it when Observer is required.

In this post, I will discuss how you could use IObserver and IObservable to implement you own notifiers.

IObserver is actually the individual Observers that when you add to the system will eventually generate notification to the environment.

Clarification of Observable and Observer

For example, say you want to add a tracer to an environment, so that when each object changes its state, you want to get notified and hence write Trace elements based on those state changes. In this case Observer might come handy. The Observer is the individual objects that generate notifications. On the other hand, the entire environment is observed by the Observable.So Observable acts as the container for all the Observers. Say you want two observer, one for Tracing and another for Logging. In such a case, you need to implement two classes, one to trace the state changes and another to log the state changes. Here once you subscribe each Observer, the Observable will generate notification to the environment which lets you to write your custom code.

As I have already told you that IObserver is the unit of Push based observer and it is just a dual of IEnumerator, it has three methods in it.

OnNext acts when the object changes its state. In my own implementation, the object state is changed when new object is inserted.

OnError acts when observer cannot generate the notification. In my own implementation, it will not generate OnError, as the Observable is eventually a collection and I left out the possibility of generating the Error. I will modify the code later to give you chance to get OnError.

OnComplete will be called when observer completes the notification. In my own implementation OnComplete is called whenever the subscriber unsubscribes the Observer.

I have also added Subscribe and Unsubscribe method in the implementation of IObserver (which is not mandatory) which will let you to subscribe the object to the Observable and also Unsubscribe the Observer by calling its Dispose. You should note, the Subscribe method actually returns a Disposable object. We will use the object to Dispose the Observer. The disposal is done using a class called Unsubscriber. Lets implement the same :

As in my own implementation of Observer, I have used only a Collection to generate the notification, we just pass the Observer container and the Observer so that we could remove the object from the container whenever we require to Dispose.

IObservable Implementation

The implementation of IObservable is very simple. The IObservable has a single method Subscribe. This method lets one to subscribe the object. The method returns an IDisposable object which lets you to Unsubscribe the object.

In the sample implementation, you can see I have used a List of IObserver. The List will hold all the Observers that I subscribe and I call each observer whenever any object is added. You can see, I have used TrackObserver to invoke the OnNext notification myself. In your actual implementation, you could attach an event handler to invoke OnNext and OnError for a collection as I have showed in my previous post.

Here in the sample I have used a class which have two properties. The TraceProperties method will help me to get value of those properties. So we declare the SampleObservable. It is the container for all Observers. and added two Observers for the class MyDummyClass. The TrackObserver will help us to generate OnNext notifications and which will eventually write the Properties on the Console.

We added two Observer, sover1 and sovr2. The using block is useful in this case as it automatically dispose objects.

So from the above snap you can see, the observer generate two notifications for each objects Abhishek and Abhijit but for the third, it calls only once. The first Observer is disposed after Tracing the first two objects.

So I hope this clears the basic usage of Observable and Observer. I am implementing the same using Events to clear you more. Stay tune to see the updates . Also I would like to see your feedback and criticisms so that It could improve the post.

Thank you for your comment. Well, are you talking about Observable.FromEvent ? If so, it is not a part of Base class library as of now. Base class library only includes the two interfaces IObservable and IObserver. The Observable implementation is done from within Reactive Framework.

Anyways, there are lots of workaround to these, but I just showed how to build one.

Blog Subscription

Learn MVC 5 step by step

My friend Shivprasad Koirala who is also a Microsoft ASP.NET MVP has released Learn MVC 5 step by step video series. It starts right from basics of MVC and goes to a level until you become a professional.
You can start taking the course for free using the below youtube video.
Please try it, you will find it awesome.

My Awards

Hit Counter

Twitter

Best .NET 4.5 Expert CookBook

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!
Another book on .NET 4.5 has been released very recently. It covers Debugging, Testing, Extensibility, WCF, Windows Phone, Windows Azure and many more...
Grab it now !!!GET ANY BOOK AT $5 from PacktPub. Offer is limited