Real world examples of Rx

theburningmonk picture theburningmonk · May 21, 2010 · Viewed 9.1k times · Source

Possible Duplicate:
Good example of Reactive Extensions Use

I've been playing around with the Reactive Extension for a little while now, but mostly limited to handling/composing user driven events within a WPF frontend.

It's such a powerful, new way of doing async programming, and I'm curious as to what other people are doing with it, and where do you think it might be able to improve the way we're currently doing things?

Answer

PL. picture PL. · May 21, 2010

We used RX with great success on two projects (Silverlight UI) already. At the beginning the intent was to simplify the WCF access layer) The rational was that in the worse case scenario we can always revert back to standard (callback) ways of doing things without affecting higher levels of the UI.

Little did we know that RX is like an addictive drug - once you start using it there's simply no coming back. Like a virus it quickly spread from this low-level communication layer all the way up to UI components:

  • we started with simple syntactic sugar to make accessing WCF services simpler.
  • from there it was a natural step to extend RX to server-to-client async messaging
  • after that to use RX to merge both of these ways for client to communicate with the server into one so viewmodels are agnostic about how they receive messages was a default option.

And then it was complete capitulation:

  • need to handle messages coming out of order?
  • need to flash a cell on the grid when price changes?
  • have a performance issue because client is bombarded by messages from the server?
  • have some rudimental CEP logic to handle?

Well, guess what, there's RX operator for that ;) (and if there's not - you can easily just write one)

The hardest part of it all was to overcome that "my-brain-hurts-so-bad" feeling that everyone on our team experienced at the beginning. Brain of a mere mortal conditioned by years of handle-my-event-by-this-callback coding is just not wired the way RX sees the world. As the result RX code (especially once it progressively gets more and more dense while handling more and more complicated scenarios) for an unprepared mind looks like complete abracadabra that amusingly does result in a rabbit pulled out of a seemingly empty hat. Unfortunately, the reality is there's no place for magic in production running code and thus whole team must be on board, which means everyone will have to go through this painful process of rewiring their brains in what seems at first like a very unnatural way.

I'd say that it's a human factor and not the RX API itself that is the biggest obstacle on effectively adopting RX. But boy is it worth it!