{"id":501,"date":"2009-10-15T13:41:20","date_gmt":"2009-10-15T12:41:20","guid":{"rendered":"http:\/\/www.leading-edge-dev.de\/?p=501"},"modified":"2010-07-10T10:26:45","modified_gmt":"2010-07-10T09:26:45","slug":"reactive-framework-rx-first-look","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/reactive-framework-rx-first-look","title":{"rendered":"Reactive Framework (Rx) &#8211; first look"},"content":{"rendered":"<p>During the last weeks I took some time to have a first look at the Reactive Framework. Here I want to share some\u00a0impressions and information with you.<\/p>\n<h3>Rx: What is it about?<\/h3>\n<p>The <strong>Reactive Framework (Rx)<\/strong> has been developed at the Microsoft <a href=\"http:\/\/livelabs.com\/\">Live Labs<\/a> and is another creation by <a href=\"http:\/\/research.microsoft.com\/en-us\/um\/people\/emeijer\/\">Erik Meijer<\/a>, the father of <a href=\"http:\/\/en.wikipedia.org\/wiki\/LINQ\">LINQ<\/a>. Rx will be part of the upcoming <strong>.NET Framework 4.0<\/strong>, but is already included as preview DLL (<code>System.Reactive.dll<\/code>) in the sources of the current <a href=\"http:\/\/www.codeplex.com\/Silverlight\">Silverlight 3 Toolkit<\/a>. It&#8217;s used there to get some smoother Unit Tests to work.<\/p>\n<p>The aim of Rx is to simplify\/unify complex event-based and asynchronous programming by providing a new programming model for those scenarios. That&#8217;s very important in a world which becomes more and more async (Web, AJAX, Silverlight, Azure\/Cloud, concurrency, many-core, &#8230;) and where present approaches (event-based programming with .NET events, Begin\/Invoke) hit the wall. Program logic gets hidden in a sea of callback methods which leads to nasty <em><a href=\"http:\/\/en.wikipedia.org\/wiki\/Spaghetti_code\">spaghetti code<\/a><\/em>. Those of you who are creating larger async programs know what I mean \ud83d\ude09<\/p>\n<h3>Basic ideas<\/h3>\n<p>The main concept of Rx is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Reactive_programming\"><strong>reactive programming<\/strong><\/a> and that&#8217;s not new at all. It involves a distinction of data processing methods into <em>push<\/em> and <em>pull<\/em> scenarios:<\/p>\n<ul>\n<li>In a <strong><em>pull<\/em>-scenario<\/strong> your code\/program is <em>interactively<\/em> asking for new data from the environment. In this case the actor is your program while it pulls new data from the environment.<\/li>\n<li>In a <strong><em>push<\/em>-scenario<\/strong> your program is <em>reactive<\/em>. The environment in pushing data into your program which has to react on every new incoming data item (see illustration below). Reactive programs are ubiquitous: every event-handler (e.g. for UI events) is an example for a reaction in a push-scenario.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/10\/Reactive_Programming1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-504\" title=\"Reactive Programming\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/10\/Reactive_Programming1.png\" alt=\"Reactive Programming\" width=\"400\" height=\"206\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/10\/Reactive_Programming1.png 400w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/10\/Reactive_Programming1-300x154.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>The guys at Microsoft got a very important insight which builds the fundament of Rx: there&#8217;s a <strong><a href=\"http:\/\/en.wikipedia.org\/wiki\/Duality_%28mathematics%29\"><em>dualism<\/em><\/a> between the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Iterator_pattern\">Iterator<\/a> and the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Observer_pattern\">Observer<\/a> pattern<\/strong>! This means that both patterns are essentially the same, if you dismiss their practical intentions for a moment.<br \/>\nWith the <em>Iterator<\/em> pattern you get a sequence of data which is iterated in the <em>pull<\/em>-mode: the Iterator is the actor and asks the iterable object for data.<br \/>\nThe <em>Observer<\/em> pattern can be interpreted equivalently: the environment pushes data into your program (e.g. through events), which has to react. As result with repeatedly fired events you get a sequence of data, which makes the relationship to the Iterator pattern obvious.<\/p>\n<p>With this, an Observer in the push-scenario is equal to an Iterator in the pull-scenario and that&#8217;s the key insight, which has led to the development of the Reactive Framework. 14 years had to pass from the publication of the GoF <a href=\"http:\/\/www.amazon.com\/dp\/0201633612\">Design Patterns<\/a> book until now to figure out this intense relationship of the Iterator and the Observer pattern.<\/p>\n<h3>LINQ to Events<\/h3>\n<p>Rx realizes the Observer pattern with the two interfaces <code>IObserver<\/code> and <code>IObservable<\/code> which are the pendants for <code>IEnumerator<\/code>\/<code>IEnumerable<\/code> of the Iterator pattern. <code>IObservable<\/code> is a sort of &#8222;asynchronous collection&#8220;, which represents the occurrence of a certain event at different points in time. Thus it expresses a sequence of data, on which you can run arbitrary actions. In fact <code>IObservable<\/code> for <em>push<\/em>-scenarios is essentially the same as <code>IEnumerable<\/code> for <em>pull<\/em>-scenarios.<\/p>\n<p>The interplay of <code>IObservable<\/code> and <code>IObserver<\/code> is the following: on an <code>IObservable<\/code> you can register (<code>Subscribe()<\/code>) an <code>IObserver<\/code> (Note the dualism to <code>IEnumerable<\/code>\/<code>IEnumerator<\/code> here!). The <code>IObserver<\/code> has three methods <code>OnNext()<\/code>, <code>OnCompleted()<\/code> and <code>OnError()<\/code>. These methods are invoked when the <code>IObservable<\/code> gets new data, completes the data sequence or results in an error. Thus your <code>IObserver<\/code> implementations take care of the data processing which replaces your custom callback methods.<\/p>\n<p>The real impressing thing with Rx is that with events as data sequences you can use <strong>LINQ<\/strong> syntax to process and transform these data sequences (that&#8217;s why it&#8217;s sometimes called <em>LINQ to Events<\/em>). To create instances of <code>IObservable<\/code> and for providing LINQ methods, Rx comes with the class <code>Observable<\/code>. If you want for example take the first 10 click events on a button but skip the first click, you could easily write:<\/p>\n<pre class=\"brush:csharp\">\r\nObservable.FromEvent(button1, \"Click\")\r\n    .Skip(1)\r\n    .Take(9)\r\n    .Subscribe(ev =&gt; MyEventAction(ev));\r\n<\/pre>\n<p><code>Subscribe()<\/code> has some overloads. In this example I&#8217;ve directly specified an action which is executed when an event (= a piece of data) comes in. Instead you could give it e.g. your custom implementations of <code>IObserver<\/code> as well.<br \/>\nLINQ gives you real power in processing, composing and transforming your events as data stream. Even in the simple example above, if you would implement it by event-based programming without Rx, you would need some additional effort. You would have to use temporary variables to skip certain elements, take others and so forth. In general, more complex cases require complete <a href=\"http:\/\/en.wikipedia.org\/wiki\/Finite_state_machine\"><em>finite state machines<\/em><\/a> which can be circumvented with Rx.<\/p>\n<p>Composing events with LINQ is another typical and impressing example for Rx. Thus, a dragging event on a textblock can be implemented as:<\/p>\n<pre class=\"brush:csharp\">\r\nIObservable&lt;Event&lt;MouseEventArgs&gt;&gt; draggingEvent =\r\n    from mouseLeftDownEvent in Observable.FromEvent(textBlock1, \"MouseLeftButtonDown\")\r\n    from mouseMoveEvent in Observable.FromEvent(textBlock1, \"MouseMove\")\r\n        .Until(Observable.FromEvent(textBlock1, \"MouseLeftButtonUp\"))\r\n    select mouseMoveEvent;\r\ndraggingEvent.Subscribe(...);\r\n<\/pre>\n<p>In this example, with the <code>from<\/code> keyword the two events <code>\"MouseLeftButtonDown\"<\/code> and <code>\"MouseMove\"<\/code> are chained together. Everytime the left mouse button is pressed on the textbox, the &#8222;gate&#8220; opens to react on mouse move events. The &#8222;gate&#8220; closes again when the left mouse button is released. So the composed stream includes all events where the left mouse button is pressed and the mouse is moved &#8211; the typical dragging event!<\/p>\n<p>There are other possibilities to compose events, which for example consider the order of events differently. Those build the interesting parts of the Reactive Framework and clarify the use cases, where Rx has real advantages over present event-based .NET programming.<\/p>\n<h3>Bottom line<\/h3>\n<p>Personally I like the new way of thinking of events as <em>data sequence<\/em>! The <em>dualism<\/em> of the Iterator and the Observer pattern has nothing artificially constructed, but some sort of purity and mathematical <em>elegance<\/em> for me. But one big question remains: do we need this in our everyday&#8217;s developer life and which advantages come with it?<\/p>\n<p>Erik Meijer himself says about Rx:<\/p>\n<blockquote><p>Of all the work I&#8217;ve done in my career so far, this is the most exciting. [&#8230;] I know it&#8217;s a bold statement, but I really believe that the problem of asynchronous programming events has been solved.<\/p><\/blockquote>\n<p>That&#8217;s a really strong message and it has some important weight since Erik is the creator of LINQ and has a big expertise in language design. Does Rx hold what he promises?<\/p>\n<p>In my opinion Rx is really beautiful and valuable when you do <em>complex<\/em> asynchronous operations, where you have to <em>compose<\/em> and <em>orchestrate<\/em> events\/data from <em>several sources<\/em>. With Rx you&#8217;re able to chain and filter events to easily create the <em>concrete event<\/em>, to which you want to react. That&#8217;s the power of <em>LINQ<\/em>, which allows transforming the resulting data as well. LINQ gives you the whole flexibility of processing the data stream that you already know from LINQ to Objects (on <code>IEnumerable<\/code>) etc.<\/p>\n<p>Beyond that at the moment I don&#8217;t see a <em>real<\/em> big impact in many current development scenarios. If you have to handle <em>simple<\/em> events or do simple async computations, you are still fine with the <em>present<\/em> event-based programming model. Of course with Rx you can write your own observers to prevent a sea of callback methods, but <em>encapsulation<\/em> of handler logic can be done without that as well. Thus at the end I like the concepts of Rx (Observer, LINQ, &#8230;) and I&#8217;m curious to see how this whole thing develops after the release of .NET 4.0, but I&#8217;m not over-enthusiastic at the moment.<\/p>\n<h3>Links<\/h3>\n<p>Finally here is a list of links to some interesting websites regarding the Reactive Framework:<\/p>\n<ul>\n<li><a href=\"http:\/\/langnetsymposium.com\/2009\/talks\/23-ErikMeijer-LiveLabsReactiveFramework.html\">Lang.NET &#8211; Live Labs Reactive Framework<\/a><\/li>\n<li><a href=\"http:\/\/channel9.msdn.com\/shows\/Going+Deep\/Expert-to-Expert-Brian-Beckman-and-Erik-Meijer-Inside-the-NET-Reactive-Framework-Rx\/\">Channel9 &#8211; Inside the .NET Reactive Framework (Rx)<\/a><\/li>\n<li><a href=\"http:\/\/channel9.msdn.com\/shows\/Going+Deep\/Kim-Hamilton-and-Wes-Dyer-Inside-NET-Rx-and-IObservableIObserver-in-the-BCL-VS-2010\/\">Channel9- Inside .NET Rx and IObservable\/IObserver in the BCL (VS 2010)<\/a><\/li>\n<li><a href=\"http:\/\/channel9.msdn.com\/shows\/Going+Deep\/E2E-Erik-Meijer-and-Wes-Dyer-Reactive-Framework-Rx-Under-the-Hood-1-of-2\/\">Channel9 &#8211; Reactive Framework Under the Hood Part1<\/a><\/li>\n<li><a href=\"http:\/\/channel9.msdn.com\/shows\/Going+Deep\/E2E-Erik-Meijer-and-Wes-Dyer-Reactive-Framework-Rx-Under-the-Hood-2-of-2\/\">Channel9 &#8211; Reactive Framework Under the Hood Part2<\/a><\/li>\n<li><a href=\"http:\/\/themechanicalbride.blogspot.com\/2009\/07\/introducing-rx-linq-to-events.html\">unfold &#8211; Introducing Rx (Linq to Events)<\/a><\/li>\n<li><a href=\"http:\/\/themechanicalbride.blogspot.com\/2009\/07\/developing-with-rx-part-1-extension.html\">unfold &#8211; The Joy of Rx: Extension Events<\/a><\/li>\n<li><a href=\"http:\/\/themechanicalbride.blogspot.com\/2009\/07\/developing-with-rx-part-2-converting.html\">unfold &#8211; The Joy of Rx: The Event-Based Async Pattern vs. IObservable<\/a><\/li>\n<li><a href=\"http:\/\/themechanicalbride.blogspot.com\/2009\/08\/joy-of-rx-building-asynchronous-service.html\">unfold &#8211; The Joy of Rx: Building an Asynchronous API&#8230;<\/a><\/li>\n<li><a href=\"http:\/\/www.paulbatum.com\/search\/label\/Reactive%20Framework\">Paul Batum &#8211; Reacting to the Reactive Framework Series<\/a><\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d501\"><img decoding=\"async\" src=\"http:\/\/www.dotnetkicks.com\/Services\/Images\/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d501\" border=\"0\" alt=\"kick it on DotNetKicks.com\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the last weeks I took some time to have a first look at the Reactive Framework. Here I want to share some\u00a0impressions and information with you. Rx: What is it about? The Reactive Framework (Rx) has been developed at the Microsoft Live Labs and is another creation by Erik Meijer, the father of LINQ. &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/reactive-framework-rx-first-look\" class=\"more-link\"><span class=\"screen-reader-text\">Reactive Framework (Rx) &#8211; first look<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,12],"tags":[182,187,181,186,183,184,185,189,188,179,180,178],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-dot-net","category-design-patterns","tag-asynchronous","tag-dualism","tag-events","tag-iterator","tag-linq","tag-linq-to-events","tag-observer","tag-pull","tag-push","tag-reactive-framework","tag-reactive-programming","tag-rx"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":7,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/501\/revisions\/934"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}