Thursday, December 31, 2009

IObservable (T) and IObserver(T) in .Net 4.0: Implemt push based notification using observer pattern

.Net framework 4.0 is going to come up with two new interfaces IObservable and IObserver. These interfaces is going to give a cleaner way to implement observer pattern in your code. In observer design pattern there are two parties involved. One is observer and another is subject.The subject maintains a list of observers and notifies them in case of change in its state.

In .net framework 4 the implementation of observer pattern can be achieved by implementing two interfaces IObservable and IObserver. The class implementing IObservable plays the role of subject and the class implemeniting IObserver acts as observer.

Let us assume that a 'Blog' has some subscribers who want to be notified after every article posted in the 'Blog'. So we can say that here the readers are subscribers to the 'Blog' and they are observer. On the other hand 'Blog' is the subject or observable. When a new article is posted in the blog subscriber will be notified about the article. The following block of codes will help you to understand you can implement this in .net framework 4.0 with two new interfaces.


namespace ObserverExample
{
class Program
{
static void Main(string[] args)
{
Blog blog = new Blog();
/***********Subscribe to the blog********/
blog.Subscribe(new Subscriber { Name = "Alex" , Email="alex@abc.com"});
blog.Subscribe(new Subscriber { Name = "Kevin", Email = "kevin@abc.com" });
blog.Subscribe(new Subscriber { Name = "Kelly", Email="kelly@mail.com"});

blog.PostArticle();
Console.ReadLine();
}
}

public class Article
{
public string Title
{
get;
set;
}
public string Content
{
get;
set;
}

public string Url
{
get;
set;
}
}

public class Blog : IObservable<Article>
{
List<IObserver<Article>> observers = new List<IObserver<Article>>();
Article _article;

#region IObservable<user> Members
public IDisposable Subscribe(IObserver<Article> observer)
{
observers.Add(observer);
return observer as IDisposable;
}
#endregion

public void PostArticle()
{
_article = new Article { Content="Cleaner way to implement observer", Title="Observer pattern in .net 4.0", Url="http://observer.blog.com"};

/*********Save article and then notify observers*****************/
foreach (IObserver<article> observer in observers)
{
observer.OnNext(_article);
}
}
}

public class Subscriber : IObserver<Article>
{
#region IObserver<Article> Members
public string Name
{
get;
set;
}
public string Email
{
get;
set;
}
public void OnCompleted()
{
Console.WriteLine("Finished sending notification.");
}

public void OnError(Exception error)
{
Console.WriteLine("Error sending notification");
}

/****************This is the function which will be called to notify observer***************/
public void OnNext(Article value)
{
/*************Writing to console****************/
/************You should write code here according to your notification mechanism to the subscriber/observer ************/
Console.WriteLine("Name:{0} Email:{1}", this.Name, this.Email);
Console.WriteLine("Title:{0}\n Content:{1}", value.Title,value.Content);
}
#endregion
}
}

Though the example is trivial I hope this one will help anyone to understand how to implement observer pattern in a cleaner way in framewok 4.0.