53

Microsoft introduced the IObservable<T> interface to the BCL with .NET Framework 4, and I thought, "Great, finally, I must use it!" So I dug deep and read posts and documentation and even implemented the pattern.

After doing so I've realized that the basic implementation actually sends all the T events to all of its subscribers without any filtering on it; i.e. plain broadcast. I read somewhere that the Observable pattern is meant for plain broadcast. I feel that this is not true and that I am missing something.

My questions:

  1. If I add a filtering mechanism, what is the difference between using the Observable pattern and just using plain CLR events?

  2. When should one use this pattern, and when should one choose to use plain CLR events?

  3. What are the main advantages of the Observable pattern?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Adiel Yaacov
  • 1,401
  • 3
  • 14
  • 24

1 Answers1

32

Observable is the cornerstone of the Rx library. They provide pretty much all the implementations and operators you'll need. The idea behind IObservable<T> and Rx is not just the "handling" of events, but enabling "LINQ to Events." So you can easily compose "event streams," which gives you a lot of power compared to regular event handling.

Note that the sample MSDN implementation of IObservable<T> is incorrect; the doc team has been notified.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 2
    hi thanks for your reply, could you please elaborate more about " enabling "LINQ to Events." So you can easily compose "event streams" or give an example ? adiel. – Adiel Yaacov Jul 06 '10 at 14:54
  • 2
    The [Rx wiki page](http://rxwiki.wikidot.com/101samples#toc15) has several LINQ to Events kinds of examples. There's also the famous "drag and drop" [video](http://channel9.msdn.com/posts/J.Van.Gogh/Writing-your-first-Rx-Application/). – Stephen Cleary Jul 06 '10 at 15:00
  • 2
    +0 I bet you have plenty insight to #2 you have not supplied :P – Ruben Bartelink Jul 03 '14 at 14:19
  • @RubenBartelink: Did you follow the link? One specific problem was described there, and since then the Rx team has released [more details on the `IObservable` requirements](http://go.microsoft.com/fwlink/?LinkID=205219). – Stephen Cleary Jul 04 '14 at 08:57
  • Thanks for the response. I hadn't followed either to be honest. Now that I have (and I don't feel it addresses the aspect of the question I've asked), I've [asked a related question](http://stackoverflow.com/questions/24572366/should-iobservable-be-preferred-over-events-in-a-library-targetting-net-4). – Ruben Bartelink Jul 04 '14 at 10:37