5

Attempting to understand why and when I would need to use Reactive Extension (Rx) for .NET, I came to question "C# 5.0 async/await feature and Rx - Reactive Extensions" the reading of which with its references resulted in more questions than answers.

The referenced in the latter article Task vs IObservable: when to use what? seems to refer to usage of IObservable synonymously (or interchangeably) to usage of Reactive (Rx) extensions for .NET

What does, for example, the phrase from the mentioned article:

Your code will require the reactive extensions if you chose to return IObservable

wanted to say?

Both Task<T> and IObservable<T> are part of .NET which I am using without any reference or setup of Rx.
Why do I require reactive extensions in order to retuen IObservable?

What does RX have to do in discussion of Task<T> vs. IObservable?
And why is their usage being juxtaposed?

Community
  • 1
  • 1

2 Answers2

13

It's actually due to ease of implementation. You should never attempt to implement IObservable<T> yourself; Rx has every implementation you could need done correctly and efficiently.

The Rx design guidelines specify the actual semantics of IObservable<T>. Note that the MSDN example implementation is completely and utterly wrong.

Update:

The MSDN example code fails in the following semantics (the reference numbers are the design guidelines from the document referenced above):

  • Does not enforce the Rx grammar (4.1).
  • Does not unsubscribe automatically after OnError (4.3).
  • May not properly handle corner cases (6.1, 6.2).
  • OnError does not have abort semantics (6.6).
  • If a subscription is disposed and the same observer is re-subscribed, then the original subscription is no longer idempotent (6.17).

And that's only if the custom implementation assumes that all calls to TrackLocation are serialized. In the Rx world, this is hardly ever the case.

Note that it is possible to fix all of these problems with the Rx Synchronize operator, though you still have to make the assumption about serialized calls to TrackLocation.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 2
    A few words about why the MSDN implementation is "completely and utterly wrong" would be nice :) – Eyvind Apr 08 '13 at 13:07
  • Technically, the implementation is "completely and utterly" out-of-date. :) It was written circa Rx 1.0. We are in the process of moving (and updating) all of the documentation over to rx.codeplex.com (where you can also find the entire open-source Rx codebase). Stephen, thanks for pointing out the specific issues! – lindydonna Apr 08 '13 at 22:12
  • @lindydonna, thanks for your feedback. I managed to use Reactive Extensions (Rx) [v1.0.2856.104](http://www.microsoft.com/en-us/download/details.aspx?id=24940) + [SP1](http://www.microsoft.com/en-us/download/details.aspx?id=28568) but failing any of [v.2.0](http://www.microsoft.com/en-us/download/details.aspx?id=30708)+ including [those from codeplex](https://rx.codeplex.com/SourceControl/list/changesets) on VS2010/.NET4.0 (Windows XP SP3). Do you have a different experience? – Gennady Vanin Геннадий Ванин Apr 09 '13 at 10:21
  • @Геннадий Ванин Новосибирск Try using the NuGet packages (http://blogs.msdn.com/b/rxteam/archive/2013/02/06/rx-2-1-is-here.aspx) instead. Also consider running VS2010 on Win7 or higher, though you could certainly target XP for your final application. – lindydonna Apr 11 '13 at 01:23
  • 2
    Whenever I tried Nuget, it btought me .NET 4.5 packages. So, I avoid using it – Gennady Vanin Геннадий Ванин Apr 12 '13 at 12:44
1

IObservable and IObserver were created by the Rx team for Rx ... they made it into the .NET Framework so that people could program against that interface and expose IObservables in their libraries without forcing everyone to automatically require all the Rx assemblies, but as soon as you want to actually do something with IObservable, you will either have to create Rx-like classes to consume them, or use Rx.

IObservable and IObserver are mathematically dual (the "arrows", the direction that parameters and return values flow, is reversed in a clever way) to IEnumerable and IEnumerator. Similar to how you cannot do much with IEnumerable besides MoveNext and Current and use a foreach statement which wraps those methods (method and property, actually), but LINQ provides all kinds of extension methods that work on IE, Rx provides extensions methods for IO. It would be possible to have Subscribe implemented in the BCL, and only that, and perhaps add a foreach like statement that does that for you, but then you'll quickly find that you need the utility of other things to really maximize the use of IO.

Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62