{"id":1621,"date":"2012-02-23T08:36:55","date_gmt":"2012-02-23T07:36:55","guid":{"rendered":"http:\/\/www.pleus.net\/blog\/?p=1621"},"modified":"2012-02-23T08:40:16","modified_gmt":"2012-02-23T07:40:16","slug":"ria-goes-to-hollywood","status":"publish","type":"post","link":"https:\/\/www.pleus.net\/blog\/?p=1621","title":{"rendered":"RIA goes to Hollywood"},"content":{"rendered":"<p>Most RIA technologies today use an asynchronous model to communicate with the server, primarily to keep the UI responsive no matter what the server is doing. This principle is also known as the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hollywood_principle_(computer_programming)\" targeet=\"_blank\">Hollywood principle<\/a>. <em>Don&#8217;t call us, we call you<\/em> means that instead of continuously polling, the server calls back when the operation finished. In order to be notified when the server finished its job, callbacks are used. <\/p>\n<p>For instance in Silverlight this leads to code like this<\/p>\n<pre class=\"brush:csharp\">\r\nvoid ClickLoad()\r\n{\r\n    LoadCustomer(1,\r\n    (result) =>\r\n    {\r\n        \/\/ Process result\r\n    },\r\n    (error) =>\r\n    {\r\n        \/\/ Process error\r\n    });\r\n}\r\n\r\nvoid LoadCustomer(int id, Action&lt;Customer&gt; success, Action&lt;Exception&gt; error)\r\n{\r\n    LoadFromServer(\"select * from customers where id = \" + id,\r\n    (result) =>\r\n    {\r\n        success(result[0] as Customer);\r\n    },\r\n    (exp) =>\r\n    {\r\n         error(exp);\r\n    });\r\n}\r\n\r\nvoid LoadFromServer(string query, Action&lt;List&lt;object&gt;&gt; success, Action&lt;Exception&gt; error)\r\n{\r\n    server.LoadCompleted += (s, e) =>\r\n    {\r\n        if (e.Error != null)\r\n            error(e.Error);\r\n        else\r\n            success(e.Data);\r\n    };\r\n\r\n    server.LoadAsync(query);\r\n}\r\n<\/pre>\n<p>Not very nice, is it? In jquery we find a similar pattern.<\/p>\n<pre class=\"brush:js\">\r\n$.ajax({\r\n  url: 'ajax\/load.html',\r\n  data: \"query=1\"\r\n  success: function(result) {\r\n    \/\/ Process result\r\n  }\r\n  error: function(XMLHttpRequest, textStatus, errorThrown) {\r\n    \/\/ Process error\r\n  }\r\n});\r\n<\/pre>\n<p>Usually an application consists of several layers. In that case the callbacks have to be routed back to the original caller, leading to code that is neither easy to read nor easy to maintain.<\/p>\n<p><a href=\"http:\/\/programmerpayback.com\/2011\/11\/17\/the-winrt-genome-project\/\" target=\"_blank\">From what is already visible<\/a>, it is very likely that Silverlight and WinRT are going to have great overlap. The combination of XAML, C# and WinRT plus the tooling will be very familiar to all Silverlight users.<br \/>\nThat means, it is likely that the asynchronous programming model will be the predominant model for client\/server communication for Windows Metro style apps. In order to simplify this, C#5.0 is going to include the <strong>await\/async<\/strong> keywords, that will make callback chaining obsolete.<\/p>\n<p>Example:<\/p>\n<pre class=\"brush:csharp\">\r\n\r\nvoid ClickLoad()\r\n{\r\n    try\r\n    {\r\n        Customer result = await LoadCustomer(1);\r\n        \/\/ Process result\r\n    }\r\n    catch(Exception e)\r\n    {\r\n        \/\/ Process error\r\n    }\r\n}\r\n\r\nasync Task&lt;Customer&gt; LoadCustomer(int id)\r\n{\r\n    List&lt;object&gt; result = await LoadFromServer(\"select * from customers where id = \" + id);\r\n    return result[0] as Customer;\r\n}\r\n\r\nasync Task&lt;List&lt;object&gt;&gt; LoadFromServer(string query)\r\n{\r\n    return await server.LoadAsync(query);\r\n}\r\n<\/pre>\n<p>This is much cleaner than the examples above. Actually it is a synchronous programming model supporting an asynchronous runtime model. As asynchronous calls are very common in RIA applications the <strong>async\/await<\/strong> keywords are going to change the way we program async calls in the near future, leading to code that is easier to develop and maintain.<\/p>\n<p>I can&#8217;t await async in C# 5.0 \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most RIA technologies today use an asynchronous model to communicate with the server, primarily to keep the UI responsive no matter what the server is doing. This principle is also known as the Hollywood principle. Don&#8217;t call us, we call you means that instead of continuously polling, the server calls back when the operation finished. &hellip; <a href=\"https:\/\/www.pleus.net\/blog\/?p=1621\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RIA goes to Hollywood<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12],"tags":[35,81],"class_list":["post-1621","post","type-post","status-publish","format-standard","hentry","category-ria","category-silverlight","tag-csharp","tag-ria"],"_links":{"self":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1621"}],"version-history":[{"count":69,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1621\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1621\/revisions\/1691"}],"wp:attachment":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}