<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>CodeBetter.Com - Stuff you need to Code Better!</title><link>http://codebetter.com/blogs/</link><description>Stuff you need to code better!</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/CodeBetter" type="application/rss+xml" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><title>MSpec latest build download now available</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/IX1bdA_iqvA/mspec-latest-build-download-now-available.aspx</link><pubDate>Thu, 19 Nov 2009 18:30:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:439212</guid><dc:creator>aaronjensen</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Many thanks again to &lt;a href="http://twitter.com/agross"&gt;Alexander Gro&amp;#223;&lt;/a&gt; for adding packaging to MSpec. You can now download builds from &lt;a href="http://teamcity.codebetter.com/viewType.html?tab=buildTypeStatusDiv&amp;amp;buildTypeId=bt44"&gt;here&lt;/a&gt;without having to build it yourself! Just grab the Artifact from the latest build.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=439212" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/IX1bdA_iqvA" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/frameworks/default.aspx">frameworks</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/machine/default.aspx">machine</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/mspec/default.aspx">mspec</category><feedburner:origLink>http://codebetter.com/blogs/aaron.jensen/archive/2009/11/19/mspec-latest-build-download-now-available.aspx</feedburner:origLink></item><item><title>Call for help with MSpec</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/VMPHwkCtYiw/call-for-help-with-mspec.aspx</link><pubDate>Thu, 19 Nov 2009 06:51:25 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:437877</guid><dc:creator>aaronjensen</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/aaron.jensen/20091118_5F00_22401_5F00_65EC1067.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;margin-left:0px;border-top:0px;margin-right:0px;border-right:0px;" title="2009-11-18_2240[1]" border="0" alt="2009-11-18_2240[1]" align="right" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/aaron.jensen/20091118_5F00_22401_5F00_thumb_5F00_4C17DA38.png" width="309" height="89" /&gt;&lt;/a&gt;Thankfully, the community has created a number of very helpful blog posts about MSpec. Unfortunately, I’ve been quite bad at aggregating them into a single authoritative source and documenting the few but somewhat obscure APIs and tools that make up MSpec. It is on my list of things to do but time has been rather… tight. I’d love to get it up sooner than later, as MSpec is getting more and more eyes on it and I want to make things as smooth as possible for new users.&lt;/p&gt;  &lt;p&gt;That’s where this plea comes in, I’m looking for someone to help put together some documentation in one place, probably the &lt;a href="http://github.com/machine/machine.specifications/blob/master/README.markdown"&gt;github README.markdown&lt;/a&gt;. I obviously don’t have anything to offer other than fame and fortune (minus the fortune and probably the fame). Rest assured, though, that myself and the rest of the community (like &lt;a href="http://twitter.com/kreiggers"&gt;kreiggers&lt;/a&gt;) would be forever grateful.&lt;/p&gt;  &lt;p&gt;I’d be happy to edit and keep up to date the documentation once created so this wouldn’t be an ongoing effort, but I could use some help getting started. Ping &lt;a href="http://twitter.com/aaronjensen"&gt;me on twitter&lt;/a&gt; if you’re interested and we can brainstorm a bit. Thanks in advance and sorry for the spam.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=437877" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/VMPHwkCtYiw" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/frameworks/default.aspx">frameworks</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/machine/default.aspx">machine</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/mspec/default.aspx">mspec</category><feedburner:origLink>http://codebetter.com/blogs/aaron.jensen/archive/2009/11/18/call-for-help-with-mspec.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part V</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/H6aMOub4Yo8/introduction-to-the-reactive-framework-part-v.aspx</link><pubDate>Wed, 18 Nov 2009 22:48:20 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:436562</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;In the previous post of the Introduction to the Reactive Framework series, we covered how to create new IObservable&amp;lt;T&amp;gt; instances, either from scratch or from existing sequences.&amp;#160; What this allowed us to do was turn an operation which was previously interactive, such as iterating over a collection, to a reactive, where I could have multiple listeners reacting to the same collection concurrently.&amp;#160; This has nice implications for allowing us to scale our application in interesting ways.&amp;#160; This time, let’s take another angle on making asynchronous programming easier.&lt;/p&gt;  &lt;p&gt;Let’s get caught up to where we are today:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;Part I – Introduction&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx"&gt;Part II – Duality of Enumerable/Observable&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx"&gt;Part III – From Events to Observables&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/15/introduction-to-the-reactive-framework-part-iv.aspx"&gt;Part IV – From Enumerables to Observables&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;From Asynchronous To Observables&lt;/h2&gt;  &lt;p&gt;In the first post in the series, we talked about some of the problems we face with asynchronous programming today.&amp;#160; For example, for any given operation, how do we handle exceptions, or handle cancellation, or even manage the lifetime of resources?&amp;#160; Let’s take a look at the story of asynchronous programming today.&amp;#160; For example, let’s take the fairly straight forward approach of sending a tweet in Twitter, but with the added pressure of non-blocking calls for all IO operations.&amp;#160; Now we took an example which should have been straight forward, and now it looks something like this anti-pattern:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d00f6bb-d89f-4d21-87e9-284327b63e6b" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; request &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebRequest.Create(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;http://twitter.com/statuses/update.xml&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
request.Credentials &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
request.Method &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;POST&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;

request.BeginGetRequestStream(rsAr &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; requestStream &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.EndGetRequestStream(rsAr);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; tweet &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; HttpUtility.HtmlEncode(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;status=Hello from an async method #bacon&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; bytes &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Encoding.UTF8.GetBytes(tweet);
    requestStream.BeginWrite(bytes, &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, bytes.Length, beginWriteAr &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    {
        requestStream.EndWrite(beginWriteAr);
        request.BeginGetResponse(responseAr &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        {
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; response &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.EndGetResponse(responseAr);
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; responseStream &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; response.GetResponseStream();

            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; readBuffer &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color:#000000;"&gt;[response.ContentLength];
            responseStream.BeginRead(readBuffer, &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, readBuffer.Length, readAr &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            {
                &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; read &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; responseStream.EndRead(readAr);
                &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; readText &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Encoding.UTF8.GetString(readBuffer);
                ResultsText.Text &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; readText;
            }, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
        }, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
    }, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
}, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Not only is this code an absolute mess, but it has a number of bad practices, including not handling cleanup of our resources, as well as not handling exceptions, cancellation checking, or even checking if our buffer was completely read.&amp;#160; Adding those constraints to the above code would make it a bit more unreadable.&amp;#160; The situation we run into is that programming in a non-blocking fashion has caused all sorts of heartache where we become so wrapped up in the non-blocking code that we lose sight easily of the domain logic we should have been interested in, in the first place.&lt;/p&gt;

&lt;p&gt;I’ve covered in the past that with F# async workflows, solving this is a rather easy task and that kind of rich functionality has been unavailable to C# developers directly.&amp;#160; But, with the Reactive Extensions for .NET, we have the ability to also take advantage of composable asynchronous actions as well.&amp;#160; So, where do we get started?&lt;/p&gt;

&lt;p&gt;Let’s take a simple example of creating a function and then invoking it asynchronously.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:742903f2-e391-48c3-86d0-554ce2d7c2c3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; add &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (x, y) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; x &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; y;

add.BeginInvoke(&lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;4&lt;/span&gt;&lt;span style="color:#000000;"&gt;, ar &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; result &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; add.EndInvoke(ar);
    Console.WriteLine(result);
}, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Many asynchronous methods in .NET are written in this way that their signature has a BeginXXX and EndXXX where the XXX is the method name that is being executed asynchronously.&amp;#160; The Begin takes the arguments to execute the method, an AsyncCallback which is an action that takes an IAsyncResult and returns nothing, and finally object state.&amp;#160; The End method takes the IAsyncResult which is passed in from the AsyncCallback in order to retrieve the value of the asynchronous call.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7b4bfa8d-cb53-4313-8e25-6ab5c1244331" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; BeginXXX(args, AsyncCallback, state);

Value EndXXX(IAsyncResult);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Luckily, the Reactive Extensions give us a couple of ways to make this happen.&amp;#160; First, let’s take an existing method such as the System.IO.Stream.WriteAsync.&amp;#160; In this example, we’ll create an extension method which allows us to turn this asynchronous method into a IObservable&amp;lt;T&amp;gt;.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:66b21f24-0090-4362-a498-9b8fd0e8eddb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; WriteAsync(
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt; Stream stream, 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color:#000000;"&gt;[] buffer, 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; offset, 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; count)
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.FromAsyncPattern&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color:#000000;"&gt;[], &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(
        stream.BeginWrite,
        stream.EndWrite)(buffer, offset, count);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because the Write method returns void, we have issues in C# as we cannot have a generic type as being void.&amp;#160; To get around this, much like F#, the Reactive Extensions introduced the Unit type to express that we have no value.&amp;#160; Quite frankly this is the way I think C# should have been to allow for this and not need these unnecessary hacks.&amp;#160; Anyways, the Observable class has a method called FromAsyncPattern which takes in both the BeginXXX and EndXXX and returns a Func delegate that takes the arguments required to execute it.&amp;#160; In this case, the Observable.FromAsyncPattern returns a Func which takes the buffer, offset and count and returns an IObservable&amp;lt;Unit&amp;gt;.&amp;#160; &lt;/p&gt;

&lt;p&gt;There is also another way in that we can take our existing add function from above and using an extension method called ToAsync, can also work the IObservable magic.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5840af4f-e707-4492-88b1-bbb3f46abb9c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; add &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (x, y) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; x &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; y;
Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;, IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; obvervableAdd &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; add.ToAsync();

IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; result &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; added &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; obvervableAdd(&lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;4&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
                          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; added;
IDisposable sub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; result
    .SubscribeOnDispatcher()
    .Subscribe(added &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Results.Text &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; added.ToString());&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this instance, we took our existing add method and then turned it into an asynchronous method by using the ToAsync extension method.&amp;#160; That returns a Func delegate which takes our two arguments and returns an IObservable&amp;lt;int&amp;gt; which contains our asynchronous result.&amp;#160; We can then take our new observableAdd and execute it inside of a LINQ statement, then subscribe to it on the Dispatcher thread in order to rid ourselves of synchronization context issues in WPF.&lt;/p&gt;

&lt;p&gt;Going back to our original example here, let’s see how we might follow some of the ideas presented here into creating a way to send a tweet completely in a LINQ statement.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e04b7ec5-7fd2-435f-b65a-526ac5711c24" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; tweetObservable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; request &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
        CreateWebRequest(
            &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;http://twitter.com/statuses/update.xml&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, 
            userName, 
            password, 
            &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;POST&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; tweet &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; HttpUtility.HtmlEncode(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;status= Hi! #bacon&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; buffer &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Encoding.UTF8.GetBytes(tweet)
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.SetContentLength(buffer.Length)
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; requestStream &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.GetRequestStreamAsync()
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; __ &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; requestStream.WriteAsync(buffer, &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, buffer.Length)
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; response &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.GetResponseAsync()
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; responseStream &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; response.GetResponseStream()
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; StreamReader(responseStream)
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader.ReadToEnd();
    
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; tweetsObservable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; tweetObservable
    .Throttle(TimeSpan.FromMinutes(&lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;))
    .Repeat(&lt;/span&gt;&lt;span style="color:#800080;"&gt;25&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can create extension methods to the WebRequest for GetRequestStream and GetResponse to enable observable behavior via the Begin/End methods that the WebRequest exposes.&amp;#160; As well, we can extend the System.IO.Stream as well to both write and read streams asynchronously.&amp;#160; We have other more trivial methods such as setting the content length and creating a web request as an observable as well, and in order to do that was covered in the previous post using the Observable.Create method.&amp;#160; After we create this IObservable value, we can then throttle it to every three minutes and to repeat 25 times.&amp;#160; One concern we might have is around the resource usage and ensuring the cleanup, and we’ll cover that next time.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As I’ve stated before, the LiveLabs Reactive Framework gives us the ability to harness reactive programming and treat events as the first class citizens they should have been using LINQ expressions and other standard LINQ combinators.&amp;#160; Still, there is much to cover in this series with the standard LINQ combinators, the monadic heritage of this solution as well as managing resources, and turning Tasks Observables.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=436562" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/H6aMOub4Yo8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/18/introduction-to-the-reactive-framework-part-v.aspx</feedburner:origLink></item><item><title>Astoria, SSIS Adapters and BDD – Projecting Projections</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/zRiCtMYWljs/astoria-ssis-adapters-and-bdd-projecting-projections.aspx</link><pubDate>Wed, 18 Nov 2009 22:19:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:436470</guid><dc:creator>hdierking</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;The big idea behind ADO.NET Data Services is that it enables a data source (generally a relational data source) to be exposed via a RESTful service.&amp;#160; In the context of fetching data – which is the principle concern of our SSIS data source component, data is returned as either an ATOM feed or as a JSON list.&amp;#160; Despite the particular data format, the general principles are the same – you’re taking a relational model and projecting it in a more serial structure.&amp;#160; Now, if all you’re querying is a single table, it doesn’t matter all that much – you’re simply taking a tabular data set and serializing each row as an entry in the ATOM feed.&amp;#160; However, ADO.NET Data Services provides an expansion feature that traverses a relationship and inserts the child data inline in the resulting feed.&amp;#160; Keep in mind that the ADO.NET Data Services API is based on HTTP – and the expansion feature is exposed simply as a querystring parameter.&lt;/p&gt;  &lt;h3&gt;Looking At the Data&lt;/h3&gt;  &lt;p&gt;Consider the following query: &lt;span style="font-family:courier new;"&gt;http://localhost:7558/northwind.svc/Products&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;This will return the following ATOM feed (fragment):&lt;/p&gt;  &lt;pre class="xml" name="code"&gt;&amp;lt;feed xml:base=&amp;quot;http://localhost:7558/Northwind.svc/&amp;quot; 
   xmlns:d=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices&amp;quot; 
   xmlns:m=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&amp;quot; 
   xmlns=&amp;quot;http://www.w3.org/2005/Atom&amp;quot;&amp;gt;
   &amp;lt;title type=&amp;quot;text&amp;quot;&amp;gt;Products&amp;lt;/title&amp;gt; 
   &amp;lt;id&amp;gt;http://localhost:7558/northwind.svc/Products&amp;lt;/id&amp;gt; 
   &amp;lt;updated&amp;gt;2009-11-17T22:45:22Z&amp;lt;/updated&amp;gt; 
   &amp;lt;link rel=&amp;quot;self&amp;quot; title=&amp;quot;Products&amp;quot; href=&amp;quot;Products&amp;quot; /&amp;gt; 
   &amp;lt;entry&amp;gt;
      &amp;lt;id&amp;gt;http://localhost:7558/Northwind.svc/Products(1)&amp;lt;/id&amp;gt; 
      &amp;lt;title type=&amp;quot;text&amp;quot; /&amp;gt; 
      &amp;lt;updated&amp;gt;2009-11-17T22:45:22Z&amp;lt;/updated&amp;gt; 
      &amp;lt;author&amp;gt;
         &amp;lt;name /&amp;gt; 
      &amp;lt;/author&amp;gt;
      &amp;lt;link rel=&amp;quot;edit&amp;quot; title=&amp;quot;Products&amp;quot; href=&amp;quot;Products(1)&amp;quot; /&amp;gt; 
      &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories&amp;quot; 
         type=&amp;quot;application/atom+xml;type=entry&amp;quot; 
         title=&amp;quot;Categories&amp;quot; 
         href=&amp;quot;Products(1)/Categories&amp;quot; /&amp;gt; 
      &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details&amp;quot; 
         type=&amp;quot;application/atom+xml;type=feed&amp;quot; 
         title=&amp;quot;Order_Details&amp;quot; 
         href=&amp;quot;Products(1)/Order_Details&amp;quot; /&amp;gt; 
      &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers&amp;quot; 
         type=&amp;quot;application/atom+xml;type=entry&amp;quot; 
         title=&amp;quot;Suppliers&amp;quot; 
         href=&amp;quot;Products(1)/Suppliers&amp;quot; /&amp;gt; 
      &amp;lt;category term=&amp;quot;NorthwindModel.Products&amp;quot; 
         scheme=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/scheme&amp;quot; /&amp;gt; 
      &amp;lt;content type=&amp;quot;application/xml&amp;quot;&amp;gt;
         &amp;lt;m:properties&amp;gt;
            &amp;lt;d:Discontinued m:type=&amp;quot;Edm.Boolean&amp;quot;&amp;gt;false&amp;lt;/d:Discontinued&amp;gt; 
            &amp;lt;d:ProductID m:type=&amp;quot;Edm.Int32&amp;quot;&amp;gt;1&amp;lt;/d:ProductID&amp;gt; 
            &amp;lt;d:ProductName&amp;gt;Chai&amp;lt;/d:ProductName&amp;gt; 
            &amp;lt;d:QuantityPerUnit&amp;gt;10 boxes x 20 bags&amp;lt;/d:QuantityPerUnit&amp;gt; 
            &amp;lt;d:ReorderLevel m:type=&amp;quot;Edm.Int16&amp;quot;&amp;gt;10&amp;lt;/d:ReorderLevel&amp;gt; 
            &amp;lt;d:UnitPrice m:type=&amp;quot;Edm.Decimal&amp;quot;&amp;gt;18.0000&amp;lt;/d:UnitPrice&amp;gt; 
            &amp;lt;d:UnitsInStock m:type=&amp;quot;Edm.Int16&amp;quot;&amp;gt;39&amp;lt;/d:UnitsInStock&amp;gt; 
            &amp;lt;d:UnitsOnOrder m:type=&amp;quot;Edm.Int16&amp;quot;&amp;gt;0&amp;lt;/d:UnitsOnOrder&amp;gt; 
         &amp;lt;/m:properties&amp;gt;
      &amp;lt;/content&amp;gt;
   &amp;lt;/entry&amp;gt;
   ...
&amp;lt;/feed&amp;gt;&lt;/pre&gt;

&lt;p&gt;As you can see, for a single resource query, the resulting feed is pretty simple with a feed consisting of multiple entries.&amp;#160; Each entry (in addition to the basic metadata) contains properties for the actual row values and links for relationships.&amp;#160; To leverage the expansion functionality – in this case, we’ll expand Suppliers, we would modify the query to look like the following: &lt;span style="font-family:courier new;"&gt;http://localhost:7558/northwind.svc/Products?$expand=Suppliers&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;From this query, we would get the following, expanded result:&lt;/p&gt;

&lt;pre class="xml" name="code"&gt;&amp;lt;entry&amp;gt;
   &amp;lt;id&amp;gt;http://localhost:7558/Northwind.svc/Products(1)&amp;lt;/id&amp;gt; 
   &amp;lt;title type=&amp;quot;text&amp;quot; /&amp;gt; 
   &amp;lt;updated&amp;gt;2009-11-17T23:04:46Z&amp;lt;/updated&amp;gt; 
   &amp;lt;author&amp;gt;
      &amp;lt;name /&amp;gt; 
   &amp;lt;/author&amp;gt;
   &amp;lt;link rel=&amp;quot;edit&amp;quot; title=&amp;quot;Products&amp;quot; href=&amp;quot;Products(1)&amp;quot; /&amp;gt; 
   &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories&amp;quot; 
      type=&amp;quot;application/atom+xml;type=entry&amp;quot; title=&amp;quot;Categories&amp;quot; href=&amp;quot;Products(1)/Categories&amp;quot; /&amp;gt; 
   &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details&amp;quot; 
      type=&amp;quot;application/atom+xml;type=feed&amp;quot; title=&amp;quot;Order_Details&amp;quot; href=&amp;quot;Products(1)/Order_Details&amp;quot; /&amp;gt; 
   &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers&amp;quot; 
      type=&amp;quot;application/atom+xml;type=entry&amp;quot; title=&amp;quot;Suppliers&amp;quot; href=&amp;quot;Products(1)/Suppliers&amp;quot;&amp;gt;
      &amp;lt;m:inline&amp;gt;
         &amp;lt;entry m:etag=&amp;quot;W/&amp;quot;&amp;#39;49%20Gilbert%20St.&amp;#39;,&amp;#39;London&amp;#39;,&amp;#39;EC1%204SD&amp;#39;&amp;quot;&amp;quot;&amp;gt;
            &amp;lt;id&amp;gt;http://localhost:7558/Northwind.svc/Suppliers(1)&amp;lt;/id&amp;gt; 
            &amp;lt;title type=&amp;quot;text&amp;quot; /&amp;gt; 
            &amp;lt;updated&amp;gt;2009-11-17T23:04:46Z&amp;lt;/updated&amp;gt; 
            &amp;lt;author&amp;gt;
               &amp;lt;name /&amp;gt; 
            &amp;lt;/author&amp;gt;
            &amp;lt;link rel=&amp;quot;edit&amp;quot; title=&amp;quot;Suppliers&amp;quot; href=&amp;quot;Suppliers(1)&amp;quot; /&amp;gt; 
            &amp;lt;link rel=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products&amp;quot; 
               type=&amp;quot;application/atom+xml;type=feed&amp;quot; title=&amp;quot;Products&amp;quot; href=&amp;quot;Suppliers(1)/Products&amp;quot; /&amp;gt; 
            &amp;lt;category term=&amp;quot;NorthwindModel.Suppliers&amp;quot; 
               scheme=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/scheme&amp;quot; /&amp;gt; 
            &amp;lt;content type=&amp;quot;application/xml&amp;quot;&amp;gt;
               &amp;lt;m:properties&amp;gt;
                  &amp;lt;d:Address&amp;gt;49 Gilbert St.&amp;lt;/d:Address&amp;gt; 
                  &amp;lt;d:City&amp;gt;London&amp;lt;/d:City&amp;gt; 
                  &amp;lt;d:CompanyName&amp;gt;Exotic Liquids&amp;lt;/d:CompanyName&amp;gt; 
                  &amp;lt;d:ContactName&amp;gt;Charlotte Cooper&amp;lt;/d:ContactName&amp;gt; 
                  &amp;lt;d:ContactTitle&amp;gt;Purchasing Manager&amp;lt;/d:ContactTitle&amp;gt; 
                  &amp;lt;d:Country&amp;gt;UK&amp;lt;/d:Country&amp;gt; 
                  &amp;lt;d:Fax m:null=&amp;quot;true&amp;quot; /&amp;gt; 
                  &amp;lt;d:HomePage m:null=&amp;quot;true&amp;quot; /&amp;gt; 
                  &amp;lt;d:Phone&amp;gt;(171) 555-2222&amp;lt;/d:Phone&amp;gt; 
                  &amp;lt;d:PostalCode&amp;gt;EC1 4SD&amp;lt;/d:PostalCode&amp;gt; 
                  &amp;lt;d:Region m:null=&amp;quot;true&amp;quot; /&amp;gt; 
                  &amp;lt;d:SupplierID m:type=&amp;quot;Edm.Int32&amp;quot;&amp;gt;1&amp;lt;/d:SupplierID&amp;gt; 
               &amp;lt;/m:properties&amp;gt;
            &amp;lt;/content&amp;gt;
         &amp;lt;/entry&amp;gt;
      &amp;lt;/m:inline&amp;gt;
   &amp;lt;/link&amp;gt;
   &amp;lt;category term=&amp;quot;NorthwindModel.Products&amp;quot; 
      scheme=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/scheme&amp;quot; /&amp;gt; 
   &amp;lt;content type=&amp;quot;application/xml&amp;quot;&amp;gt;
      &amp;lt;m:properties&amp;gt;
         &amp;lt;d:Discontinued m:type=&amp;quot;Edm.Boolean&amp;quot;&amp;gt;false&amp;lt;/d:Discontinued&amp;gt; 
         &amp;lt;d:ProductID m:type=&amp;quot;Edm.Int32&amp;quot;&amp;gt;1&amp;lt;/d:ProductID&amp;gt; 
         &amp;lt;d:ProductName&amp;gt;Chai&amp;lt;/d:ProductName&amp;gt; 
         &amp;lt;d:QuantityPerUnit&amp;gt;10 boxes x 20 bags&amp;lt;/d:QuantityPerUnit&amp;gt; 
         &amp;lt;d:ReorderLevel m:type=&amp;quot;Edm.Int16&amp;quot;&amp;gt;10&amp;lt;/d:ReorderLevel&amp;gt; 
         &amp;lt;d:UnitPrice m:type=&amp;quot;Edm.Decimal&amp;quot;&amp;gt;18.0000&amp;lt;/d:UnitPrice&amp;gt; 
         &amp;lt;d:UnitsInStock m:type=&amp;quot;Edm.Int16&amp;quot;&amp;gt;39&amp;lt;/d:UnitsInStock&amp;gt; 
         &amp;lt;d:UnitsOnOrder m:type=&amp;quot;Edm.Int16&amp;quot;&amp;gt;0&amp;lt;/d:UnitsOnOrder&amp;gt; 
      &amp;lt;/m:properties&amp;gt;
   &amp;lt;/content&amp;gt;
&amp;lt;/entry&amp;gt;&lt;/pre&gt;

&lt;p&gt;While this example demonstrates expansion of a single link resource, ADO.NET Data Services allows you to expand a single link, multiple links, or multiple nested links.&amp;#160; The funny thing about this is that the SSIS data flow engine takes much more of a relational view of the world with its stream architecture.&amp;#160; This means that we’re starting with a relational model, projecting into a hierarchical model, and then projecting back to a relational model.&amp;#160; The question, then, is how we go about designing (and testing) this projection.&lt;/p&gt;

&lt;p&gt;To start, here’s a few high level design principles:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Every resource usage (primary and all expanded) should yield a unique output stream in an SSIS data flow which is visible in the data flow designer &lt;/li&gt;

  &lt;li&gt;Composition of output streams (e.g. joining) should be done via data flow designer transform components &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;A Unique Stream Per Resource&lt;/h3&gt;

&lt;p&gt;Every resource that is returned as part of an ADO.NET Data Services query, whether it is the primary resource (e.g. Products) or an expanded resource, should have a unique output in the data flow designer.&amp;#160; Moreover, if a resource is referenced in more than 1 role (e.g. http://localhost:7558/northwind.svc/Products?$expand=Suppliers/Products), an output stream should be created for each role.&amp;#160; The goal of having multiple output streams is that each one can then by manipulated independently using the data flow designer.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/multioutputdesigner_5F00_52919AA5.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="multi-output-designer" border="0" alt="multi-output-designer" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/multioutputdesigner_5F00_thumb_5F00_348366E4.png" width="598" height="344" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;Enabling Composition&lt;/h3&gt;

&lt;p&gt;One of the primary reasons behind creating unique streams per resource *role* is to enable granular composition/branching inside the data flow designer (as shown above).&amp;#160; The service metadata provided by a data service provides a type definition for each resource managed by the service.&amp;#160; This makes it initially straightforward for determining data shape from a data services query.&amp;#160; However, in order to effectively project the parent/child relationship specified with the expansion feature, we need to make sure we include the identifiers of the parent record in context of the child record.&amp;#160; I started with the following specifications:&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class when_employees_resource_query_expands_territories : with_northwind_CSDL
{
    Establish context = () =&amp;gt; {
        queryBuilder.ResourcePath = &amp;quot;Employees&amp;quot;;
        queryBuilder.OperationsString = &amp;quot;$expand=Territories&amp;quot;;
    };

    Because of = () =&amp;gt; query = queryBuilder.GetQuery();

    It should_declare_2_resources = () =&amp;gt; query.DataShape.Count().ShouldEqual(2);

    It should_have_1_reference_property_for_territories =
        () =&amp;gt; query.DataShape.ElementAt(1).Properties
            .Where(p =&amp;gt; p.GetType() == typeof (RefProperty)).Count()
            .ShouldEqual(1);

    It should_have_2_properties_for_territories =
        () =&amp;gt; query.DataShape.ElementAt(1).Properties
            .Where(p =&amp;gt; p.GetType() == typeof (Property)).Count()
            .ShouldEqual(2);

    It should_have_correct_query_string = 
        () =&amp;gt; query.ToString().ShouldEqual(
            string.Format(&amp;quot;{0}/Employees?$expand=Territories&amp;quot;, Constants.SERVICE_URI));

    It should_have_employees_as_first_resource = 
        () =&amp;gt; query.DataShape.ElementAt(0).Name.ShouldEqual(&amp;quot;Employees&amp;quot;);

    It should_have_employees_as_the_first_resource =
        () =&amp;gt; query.DataShape.ElementAt(0).Name.ShouldEqual(&amp;quot;Employees&amp;quot;);

    It should_have_Territories_as_second_resource =
        () =&amp;gt; query.DataShape.ElementAt(1).Name.ShouldEqual(&amp;quot;Territories&amp;quot;);

    It should_have_territories_as_the_second_resource =
        () =&amp;gt; query.DataShape.ElementAt(1).Name.ShouldEqual(&amp;quot;Territories&amp;quot;);
}&lt;/pre&gt;

&lt;p&gt;First, note the with_northwind_CSDL base class.&amp;#160; Aaron brought up in an earlier post that this “with…” convention was one that he was actually moving further away from, so in this point, I want to call out more specifically what is happening here.&amp;#160; The basic point of this shared context is to setup a mock reader to get the XML metadata from a file rather than from the $metadata clause, which is what is used at runtime.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class with_northwind_CSDL
{
    const string NORTHWIND_METADATA_PATH = &amp;quot;svc_metadata.xml&amp;quot;;
    protected static DataServicesQuery query;
    protected static IDataServicesQueryBuilder queryBuilder;

    Establish context = () =&amp;gt; {
        var mockMetadataReader = new Mock&amp;lt;IReader&amp;gt;();
        mockMetadataReader.Setup(r =&amp;gt; r.GetData(Constants.SERVICE_URI + &amp;quot;/$metadata&amp;quot;))
            .Returns(new StreamReader(NORTHWIND_METADATA_PATH));

        queryBuilder = new DataServicesQueryBuilder(new CSDLResourceBuilder(), 
            mockMetadataReader.Object) {ServiceURI = Constants.SERVICE_URI};
    };
}&lt;/pre&gt;

&lt;p&gt;Setting this up in a base class rather than in every spec class saved me a fair amount of repetitive typing – especially since my specifications here are pivoting on how the query shapes the data shape rather than on how the metadata is acquired.&lt;/p&gt;

&lt;p&gt;So once I had my basic specs setup, all that remained was the implementation.&amp;#160; As you can see from the following, the basic task of creating the data shape is effectively divided among 3 types:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/core_5F00_39F1D788.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="core" border="0" alt="core" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/core_5F00_thumb_5F00_513CDBF9.png" width="549" height="312" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the data reader is responsible for getting the metadata from some source (in the running service, this is done using a HTTP GET request – for my specs, this was mocked with an XML file reader) &lt;/li&gt;

  &lt;li&gt;the resource builder is responsible for producing resources (the main elements of the data shape) from the metadata provided by the data reader &lt;/li&gt;

  &lt;li&gt;the query builder is responsible for knowing how to interpret the query URI and ensuring that parent/child projections are correctly constructed. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on this separation of roles, you can see that the bulk of the projection logic resides in the query builder class – specifically in the GetResources method.&amp;#160; This logic looks like the following:&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;IEnumerable&amp;lt;Resource&amp;gt; GetResources() {
    var metadata = _metadataReader.GetData(string.Format(&amp;quot;{0}/$metadata&amp;quot;, ServiceURI));
    _resourceBuilder.Initialize(metadata);

    // initialize resourcs collection with primary resource
    var resources = new List&amp;lt;Resource&amp;gt; {
                                           _resourceBuilder.GetResource(ResourcePath.Split(new[] {&amp;#39;/&amp;#39;}).Last())
                                       };

    //add resources specified by the presence of an $expand query operation
    if (!string.IsNullOrEmpty(OperationsString)) {
        var expandOperation = QueryOperation.ParseQueryOperations(OperationsString).Expand();

        foreach (var expandedPath in expandOperation.ResourcePaths)
            if (expandedPath.Contains(&amp;quot;/&amp;quot;)) {
                var pathAxes = expandedPath.Split(new[] {&amp;#39;/&amp;#39;});
                for (var i = 0; i &amp;lt; pathAxes.Length; i++)
                    resources.Add(_resourceBuilder.GetResource(pathAxes[ i ], resources.Last()));
            }
            else
                resources.Add(_resourceBuilder.GetResource(expandedPath, resources.Last()));
    }

    return resources;
}&lt;/pre&gt;

&lt;p&gt;As you can see, this method is basically getting resource names from the data services query string and then asking the resource builder to build those resources.&amp;#160; For expanded resources, the builder is asked to produce the resource incorporating the keys of the parent resource.&lt;/p&gt;

&lt;p&gt;In sum, I think that dealing with $expand is one of the more interesting aspect of this component, so I wanted to spend a bit more time going through it.&amp;#160; To see all of the components working together, you can download the entire source code at &lt;a title="http://code.msdn.microsoft.com/ssisastoria" href="http://code.msdn.microsoft.com/ssisastoria"&gt;http://code.msdn.microsoft.com/ssisastoria&lt;/a&gt;.&amp;#160; But in sum, the problem looks like: DB (relational) –&amp;gt; Data Services (hierarchical) –&amp;gt; SSIS (relational).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=436470" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/zRiCtMYWljs" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/BDD/default.aspx">BDD</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/MSpec/default.aspx">MSpec</category><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/11/18/astoria-ssis-adapters-and-bdd-projecting-projections.aspx</feedburner:origLink></item><item><title>Astoria, SSIS Adapters and BDD – Class Design</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/h_4BuFlV-D8/astoria-ssis-adapters-and-bdd-class-design.aspx</link><pubDate>Wed, 18 Nov 2009 20:11:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:436155</guid><dc:creator>hdierking</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;In the process of writing a post about one specific feature of the design, I realized that I hadn’t actually described the moving parts in the solution.&amp;#160; There are 3 projects in the VS solution: the SSIS-specific code, the core application logic, and the application logic tests (specifications).&amp;#160; I’ll talk more about the SSIS part later and focus here on the core application logic and tests.&lt;/p&gt;  &lt;p&gt;At a high level, there are 2 major logical components that interact to support SSIS at design time and runtime – those are DataServicesQuery and DataServicesQueryBuilder.&amp;#160; &lt;/p&gt;  &lt;h3&gt;The DataServicesQueryBuilder Object&lt;/h3&gt;  &lt;p&gt;The query builder is responsible for getting the metadata from ADO.NET Data Services and constructing the resources that are used by SSIS during the design time experience (those resources are also passed to the query to be used at runtime for making sure that a data element gets put in the right place in the buffer).&amp;#160; The basic elements of the query builder are shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/core_5F00_50D0F3F6.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="core" border="0" alt="core" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/core_5F00_thumb_5F00_6CB20321.png" width="559" height="422" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you can see, responsibilities are further decomposed as follows:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The DataServicesQueryBuilder class is responsible for understanding an ADO.NET Data Services query and producing a query that can be consumed by the SSIS runtime &lt;/li&gt;    &lt;li&gt;The IReader interface is responsible for getting stream data from some data source – the concrete implementation used by the query builder is the HttpRequestReader.&amp;#160; This implementation is used by the query builder to get service metadata as follows: &lt;/li&gt; &lt;/ul&gt;  &lt;pre class="c-sharp" name="code"&gt;var metadata = _metadataReader.GetData(
    string.Format(&amp;quot;{0}/$metadata&amp;quot;, ServiceURI));&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;The IResourceBuilder interface is responsible for producing Resource instances from whatever stream data is handed to it.&amp;#160; Since ADO.NET Data Services currently has a dependency on EDM (more specifically, CSDL), the only relevant concrete class at the moment is CSDLResourceBuilder. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only other thing I want to point out from the query builder classes is that we have a class for both Property and for RefProperty.&amp;#160; Resource properties are the equivalent of columns (if looking at the data from a tabular perspective) and RefProperties are the equivalent of links.&amp;#160; This will become more obvious when I discuss use of the $expand keyword in a future post, but the difference between parent/child (and subsequently, the driver behind inheritance) is based on the following runtime behavior (when extracting data from results data):&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class Property
{
    ...

    public virtual object GetValue(XElement element) {
        var propertyElement = element.Element(XName.Get(&amp;quot;content&amp;quot;, XmlNamespaces.AtomXmlNamespace))
            .Element(XName.Get(&amp;quot;properties&amp;quot;, XmlNamespaces.DataServicesMetadataXmlNamespace))
            .Element(XName.Get(Name, XmlNamespaces.DataServicesXmlNamespace));

        var propertyVal = propertyElement.Value;

        var val = Convert.ChangeType(propertyVal, PropertyType);

        return val;
    }
}

public class RefProperty : Property
{
    public RefProperty(string name, Type type, int? maxLength, bool? unicode)
        : base(name, type, maxLength, unicode) { }

    public override object GetValue(XElement element) {
        return element.Parent.Name.LocalName==&amp;quot;feed&amp;quot; 
            ? base.GetValue(element.Parent.Parent.Parent.Parent) 
            : base.GetValue(element.Parent.Parent.Parent);
    }
}&lt;/pre&gt;

&lt;h3&gt;The DataServicesQuery object&lt;/h3&gt;

&lt;p&gt;Ultimately, the SSIS code simply uses the query builder in order to get a DataServicesQuery object.&amp;#160; The query builder configures the query with the collection of resources that it created (called the data shape).&amp;#160; The query is responsible for executing the actual query and extracting the results from the returned XML (ATOM feed) into a tabular format which can be used to populate SSIS output buffers.&amp;#160; As you can see below, the query class layout is much simpler than the builder:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/QueryClasses_5F00_156D686B.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Query Classes" border="0" alt="Query Classes" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/QueryClasses_5F00_thumb_5F00_79E86667.png" width="511" height="255" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, the reader is again used to pull down stream data.&amp;#160; The query then uses it’s data shape (resources) to take the XML data stream and create a ResultSet for each resource.&amp;#160; 1..n ResultSets are then handed back as the result of executing the query – this is what gets handed back to SSIS and allows the SSIS adapter code to populate the SSIS buffers.&lt;/p&gt;

&lt;p&gt;You can download and look at the complete source at &lt;a title="http://code.msdn.microsoft.com/ssisastoria" href="http://code.msdn.microsoft.com/ssisastoria"&gt;http://code.msdn.microsoft.com/ssisastoria&lt;/a&gt;.&amp;#160; As always, please let me know if there’s stuff that doesn’t make sense or that could be refactored to be improved.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=436155" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/h_4BuFlV-D8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/11/18/astoria-ssis-adapters-and-bdd-class-design.aspx</feedburner:origLink></item><item><title>MSpec v0.3</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/xDyGW22HoQk/mspec-v0-3.aspx</link><pubDate>Wed, 18 Nov 2009 18:20:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:435888</guid><dc:creator>aaronjensen</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;&lt;strong&gt;IMPORTANT NOTE: &lt;/strong&gt;This is beta (still). The assertion library is new and may have bugs. I’d appreciate you testing it on your specs, but don’t fight specs breaking too hard if they do, &lt;a href="http://twitter.com/aaronjensen"&gt;let me know&lt;/a&gt; what happened and revert, I wouldn’t want to cost you your day chasing down a bug in MSpec. Thanks!&lt;/p&gt;  &lt;p&gt;It’s been &lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/09/02/mspec-v0-2.aspx"&gt;more or less forever&lt;/a&gt; since I’ve incremented the version number of MSpec so I went ahead and did it today. This is just a brief post to let you know what’s new and what I broke—maybe I’ll post some more detail later.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MOST IMPORTANT NOTE:&lt;/strong&gt; v0.3 removes the dependency on NUnit/XUnit. If you’re upgrading, delete your Machine.Specifications.NUnit.dll and Machine.Specifications.XUnit.dll and remove their references from your projects. MSpec has an all new assertion library (with a bit of borrowed code from XUnit, turns out equality is hard). &lt;/p&gt;  &lt;p&gt;I’ve attempted to format the new error messages to minimize debugging, so they’re rather verbose. Please let me know if they are too verbose or if there’s anything else you’d like to see.&lt;/p&gt;  &lt;p&gt;You may need to tweak a few specs (ex. ShouldEquals is now generic, so you have to pass it the same type as the thing you’re observing) but most things should “just work”. Let me know if any of the new assertions give pain.&lt;/p&gt;  &lt;p&gt;I also added a &lt;a href="http://weblogs.asp.net/nunitaddin/archive/2009/11/05/testdriven-net-2-24-xcopy-deployable-test-runners.aspx"&gt;XCopy Deployable Test Runner manifest&lt;/a&gt; for TD.NET so no more fiddling with the registry or dealing with version issues. Pretty slick really. Thanks &lt;a href="http://twitter.com/jcansdale"&gt;Jamie&lt;/a&gt; for pointing this out to me.&lt;/p&gt;  &lt;p&gt;Of course there are plenty of other changes from 0.2 to 0.3 since it’s been like, a year. Here’s some others off the top of my head:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;R# runners for 4.0, 4.1, 4.5 and 5.0 and they’re better than ever thanks to &lt;a href="http://therightstuff.de/"&gt;Alexander&lt;/a&gt; &lt;a href="http://twitter.com/agross"&gt;Groß&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Xml output from the command line runner with --xml for CC.NET/Hudson thanks to Barry Woods. &lt;/li&gt;    &lt;li&gt;Concern is now Subject &lt;/li&gt;    &lt;li&gt;There is no longer a difference between “Establish context” and “Establish context_once”, the context is always established only once. The idea is that observations are observations, they should not mutate state so there is no reason to execute the context more than once. Same for cleanup. If you *really* want to do execute the context for each specification, first try to change your mind, then if you still want it use: [SetupForEachSpecification] on your context class. &lt;/li&gt;    &lt;li&gt;You can implement an &lt;strong&gt;IAssemblyContext&lt;/strong&gt; in your spec assembly to set things up before or clean things up after all of your specs in that assembly have run. &lt;/li&gt;    &lt;li&gt;You can implement a &lt;strong&gt;ICleanupAfterEveryContextInAssembly&lt;/strong&gt; to perform cleanup after every context (think cleaning up static state, resetting your ServerClock/DateTime replacement for example. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2009/10/19/advanced-selenium-logging-with-mspec.aspx"&gt;Selenium RC support&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Many thanks again to &lt;a href="http://twitter.com/agross"&gt;Alexander Gro&amp;#223;&lt;/a&gt; for adding packaging to MSpec. You can now download builds from &lt;a href="http://teamcity.codebetter.com/viewType.html?tab=buildTypeStatusDiv&amp;amp;buildTypeId=bt44"&gt;here&lt;/a&gt;without having to build it yourself! Just grab the Artifact from the latest build.&lt;/p&gt;  &lt;p&gt;And, as always, the best way to get me is on &lt;a href="http://twitter.com/aaronjensen"&gt;twitter&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=435888" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/xDyGW22HoQk" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/bdd/default.aspx">bdd</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/machine/default.aspx">machine</category><category domain="http://codebetter.com/blogs/aaron.jensen/archive/tags/mspec/default.aspx">mspec</category><feedburner:origLink>http://codebetter.com/blogs/aaron.jensen/archive/2009/11/18/mspec-v0-3.aspx</feedburner:origLink></item><item><title>Reactive Extensions for .NET (Rx) released on DevLabs</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/bR67D1jCqeo/reactive-extensions-for-net-rx-released-on-devlabs.aspx</link><pubDate>Tue, 17 Nov 2009 22:50:12 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:434948</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;As you may have noticed, I’ve been blogging a bit about the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx"&gt;Reactive Framework&lt;/a&gt;, now the Reactive Extensions for .NET (Rx) and how it will change the way we approach asynchronous and event-based programming.&amp;#160; Just as LINQ to Objects changed the way we deal with collections, The Reactive Extensions will change the way we deal with asynchronous and event-based programming.&amp;#160; Fast forward to today, &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Rx has been released on Microsoft DevLabs&lt;/a&gt; for not only .NET 4 Beta 2, but also .NET 3.5 SP1 and Silverlight 3 as well.&amp;#160; &lt;/p&gt;  &lt;p&gt;If you’re not familiar with Rx, it is a library for composing asynchronous and event based programs together using observable collections (IObservable&amp;lt;T&amp;gt;).&amp;#160; This is especially important in the development of Silverlight, which disallows blocking networking and threading calls from happening and instead forces you into an asynchronous model, but just as well, it’s quite useful for other applications such as traditional desktop applications and web applications.&amp;#160; With that comes its own set of challenges.&amp;#160; How do I compose these asynchronous events together?&amp;#160; How do I handle exceptions?&amp;#160; How about cancellation?&lt;/p&gt;  &lt;p&gt;The Reactive Extensions, the contain a superset of the standard LINQ operators we’ve come to know and use quite frequently in our applications which are based not off the IEnumerable&amp;lt;T&amp;gt;, but instead IObservable&amp;lt;T&amp;gt;.&amp;#160; The IObservable&amp;lt;T&amp;gt;/IObserver&amp;lt;T&amp;gt;, being the mathematical dual of IEnumerable&amp;lt;T&amp;gt;/IEnumerator&amp;lt;T&amp;gt; allows us to go from the pull model to the push model with ease.&amp;#160; With that, we can create abstractions over asynchronous methods, events, Parallel Extensions for .NET Tasks, StreamInsight, F# First Class Events and more.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Download it today&lt;/a&gt; and give the team feedback.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=434948" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/bR67D1jCqeo" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/17/reactive-extensions-for-net-rx-released-on-devlabs.aspx</feedburner:origLink></item><item><title>The High Cost of Losing a Developer</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/N9FlS__-DUQ/the-high-cost-of-losing-a-developer.aspx</link><pubDate>Tue, 17 Nov 2009 06:18:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:434130</guid><dc:creator>Dave Laribee</dc:creator><slash:comments>23</slash:comments><description>&lt;p&gt;You&amp;#39;ve probably heard statistics about the &amp;quot;high cost of losing a customer.&amp;quot; The theory is simple and true (fact?): keeping existing customers and repeat business is much more profitable than attracting new customers. Here are a few facts from &lt;a href="http://www.businesscoach.com/go/bc/handouts/the_high_cost_of_losing_a_customer/index.cfm"&gt;BusinessCoach.com&lt;/a&gt;:
&lt;/p&gt;
&lt;blockquote&gt;
- For every customer who bothers to complain, there are 26 others who remain silent.&lt;br /&gt;
- The average &amp;ldquo;wronged&amp;rdquo; customer will tell 8 to 16 people.&lt;br /&gt;
- 91% of unhappy customers will never purchase services from you again.&lt;br /&gt;
- It costs about five times as much to attract a new customer as it costs to keep an old one.&lt;br /&gt;
- Each one of your customers has a circle of influence of 250 people or potential customers who hear bad things about you!&lt;br /&gt;
&lt;/blockquote&gt;
&lt;p&gt;The point of this post is simple: &lt;b&gt;RETAIN YOUR GOOD DEVELOPERS.&lt;/b&gt; Getting someone up to speed on a legacy codebase takes a long time and is an expensive undertaking. A large part of the software coach&amp;#39;s or manager&amp;#39;s job is attracting, developing and &lt;i&gt;keeping&lt;/i&gt; talent. And that talent becomes more valuable over time. &lt;/p&gt;
&lt;p&gt;Yes, you might be able to replace someone at a lower annual salary, but you have to take into account the complexity of your code portfolio in how long it&amp;#39;ll take to make that person productive. A $60K/annum employee may very well take $120K before reaching the productivity level and contribution of the developer who left the team.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=434130" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/N9FlS__-DUQ" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/teams/default.aspx">teams</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/management/default.aspx">management</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/systems+thinking/default.aspx">systems thinking</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/business/default.aspx">business</category><feedburner:origLink>http://codebetter.com/blogs/david_laribee/archive/2009/11/17/the-high-cost-of-losing-a-developer.aspx</feedburner:origLink></item><item><title>Pablo's Fiesta:  Open Spaces in Austin</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/YOVbbH5mt-0/pablo-s-fiesta-open-spaces-in-austin.aspx</link><pubDate>Mon, 16 Nov 2009 22:25:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:432979</guid><dc:creator>Jeremy D. Miller</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;Looks like all my buddies at Los Techies are putting on their first Open Spaces event in Austin during February.&amp;nbsp; I&amp;#39;ll see you there.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;More info &lt;a href="http://www.lostechies.com/blogs/johnteague/archive/2009/11/16/pablo-s-fiesta-open-space-conference-in-austin-tx.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=432979" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/YOVbbH5mt-0" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/jeremy.miller/archive/2009/11/16/pablo-s-fiesta-open-spaces-in-austin.aspx</feedburner:origLink></item><item><title>Building extensible RIAs at PDC</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/vnYArHm5noY/building-extensible-rias-at-pdc.aspx</link><pubDate>Mon, 16 Nov 2009 18:23:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:432368</guid><dc:creator>Glenn Block</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&lt;a href="http://microsoftpdc.com/"&gt;&lt;img height="88" width="325" src="http://blogs.msdn.com/blogfiles/brada/WindowsLiveWriter/PDC2009Icantwait_88A8/image_3.png" align="left" alt="image" border="0" title="image" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;This week I&amp;rsquo;ll be attending PDC in Los Angeles speaking about using MEF in Silverlight.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://microsoftpdc.com/Sessions/FT24"&gt;Building Extensible Rich Internet Applications with the Managed Extensibility Framework&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In Hall F on Thursday at 11:30 AM&lt;/p&gt;
&lt;p&gt;Learn how the Microsoft .NET Framework 4 introduces the Managed Extensibility Framework (MEF) for building customizable applications that can easily be extended by third parties. Whether you are building an extensible data grid, a custom rules engine, a pluggable editor, or a composite application such as a pluggable CRM system, you want to learn about MEF. Hear how to use MEF to decouple your applications into more maintainable and testable pieces, and partition your application into dynamically deployable chunks that download on-demand.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you haven&amp;rsquo;t heard, &lt;a href="http://mef.codeplex.com"&gt;MEF&lt;/a&gt; is available in Silverlight similar to it&amp;rsquo;s availability on the desktop.&amp;nbsp; But what does it mean to build an extensible app in Silverlight? Yes it&amp;rsquo;s technically possible, but why should you care? In this talk we&amp;rsquo;ll explore different application scenarios for when MEF is a good fit. We&amp;rsquo;ll also explore some of the SL specific features we&amp;rsquo;ve introduced in order to enrich the MEF experience.&lt;/p&gt;
&lt;p&gt;Hope to see you there!&lt;/p&gt;
&lt;p&gt;Glenn &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=432368" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/vnYArHm5noY" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/silverlight/default.aspx">silverlight</category><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/MEF/default.aspx">MEF</category><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/RIA/default.aspx">RIA</category><feedburner:origLink>http://codebetter.com/blogs/glenn.block/archive/2009/11/16/building-extensible-rias-at-pdc.aspx</feedburner:origLink></item><item><title>Back to Basics: Usage of Static</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/5KC2WAr-S_g/back-to-basics-usage-of-static.aspx</link><pubDate>Mon, 16 Nov 2009 13:06:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:431581</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>22</slash:comments><description>&lt;p&gt;





&lt;/p&gt;

 
  
  
 

&lt;p&gt;


&lt;/p&gt;

 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 

&lt;p&gt;

&lt;/p&gt;


&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;The keyword
&lt;b&gt;static&lt;/b&gt; is somewhat awkward in a pure
Oriented-Object world. I would like to expose here the usages of &lt;b&gt;static&lt;/b&gt; I came up after 15
years of OO Programming.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;The 2 rules
of thumb are:&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;" class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;&lt;b&gt;A&lt;/b&gt;: Static fields that are not pure value constants should be prohibited.&lt;br /&gt;&lt;b&gt;B&lt;/b&gt;:
Static classes, that contain only pure functions, that compute outputs
from inputs parameters, are allowed. Personally, I am used to suffix
such class with XXX&lt;b&gt;Helper&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;You might
not agree with these 2 simple rules so let me justify them.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;What&amp;rsquo;s
wrong with static fields that are not pure value constant? The underlying
motivation for creating a static field comes from the need for an object that
lives during all the program execution lifetime. Generally this is a central
object, shared by many methods. So instead of passing the object as a parameter
to all these methods, making it global makes it more convenient, easy or
readable. I see 3 main problems in this reasoning:&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;First, everytime
I stumbled on a &lt;i&gt;need for an object that
lives during all the program execution lifetime&lt;/i&gt; I found out sooner or later (generally
later) that I was wrong. There is no such thing. For example imagine that your
program become hosted as an addin (in VisualStudio for example). Addin can be
unload and reloaded. Thus the &lt;i&gt;program execution
lifetime&lt;/i&gt; become &lt;i&gt;session&lt;/i&gt; lifetime.
And you certainly don&amp;rsquo;t want that the global object lives between sessions. The
cornerstone here, to get my point, is the concept of &lt;b&gt;session&lt;/b&gt;. It is virtually impossible to forecast up-front what will be sessions in which our
code will live in the future. But there is a very common example of
session: Automatic tests. An automatic test (or a set of automatic tests) is a
mini session in itself. Having static mutable fields makes the code much less
testable because all these mini-sessions must shared the same mutable state,
with the need to initialize/clean up it.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Tests are
generally ran sequentially. But what if several sessions are ran in parallel and
they all share the same mutable objects. Then you need to synchronize access to
the global object. And trust me, synchronizing accesses will be a major pain,
not even taken account that this will slow down significantly your program. I
wrote an article on &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/05/05/manage-states-in-a-multi-threaded-environment-without-the-synchronization-pain.aspx"&gt;&lt;b&gt;Managing states in a
multi-threaded environment without the synchronization pain&lt;/b&gt;&lt;/a&gt;. One of my preferred tip in the article is &lt;b&gt;Thread / Resource affinity&lt;/b&gt;: &lt;i&gt;The
idea is as simple as it is efficient here also: making sure that an object is
always accessed by the same thread&lt;/i&gt;. Basically each thread hold its own
instance. If N threads holds N states then states must be attached to N objects
and states cannot be static. To do so, using the &lt;a target="_blank" href="http://codebetter.com/controlpanel/blogs/posteditor.aspx/ThreadStaticAttribute"&gt;ThreadStaticAttribute &lt;/a&gt;on
your static field can do the job. But it is much more elegant to create
a dedicated object for each thread that holds the complete &lt;i&gt;thread-bounded context&lt;/i&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Finally,
the third problem with static fields comes from the fact that this makes the
program much less maintainable. Static fields represent global state. A global
state can be accessed from anywhere in source code. Thus from anywhere in the source
code, in any scope, you can potential create a dependency to the global state. The
dependency is generally non-obvious because it is generally indirect (because
the static field is generally encapsulated by a static static property). But
the result is that accesses to the global state become anarchical. It is
virtually impossible to enforce accesses to a global state and you will end up with some sort of &lt;i&gt;side-effect nightmare&lt;/i&gt;. Sooner or later,
the need to refactor the global state in order to control its usage will urge (&lt;i&gt;this is another empirical fact I learnt the hard way&lt;/i&gt;).&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;A
consequence of all this is that &lt;b&gt;&lt;i&gt;singleton&lt;/i&gt; class must be prohibited&lt;/b&gt;. From
what we just saw, the single static field of singleton makes program harder to test,
harder to maintain, and singletons foster concurrency problems. In my opinion,
the problem with singleton is that it is the easiest &lt;i&gt;OOP design pattern&lt;/i&gt; to learn. As a
consequence, every OOP beginner knows the singleton and has a tendency to use
it. Instead of using singleton, I use what I call &lt;b&gt;Managers&lt;/b&gt;. A manager is a purely non-static class especially created
in the spirit that only one instance will live at a particular point in time. A
manager doesn&amp;rsquo;t have the dirty static instance field trick. Instead managers
are generally referenced by other more high level managers. &lt;b&gt;All this grape of managers represent the context in which the code is running&lt;/b&gt;. Manager can also be
easily passed as a parameter to methods that needs locally to access the context. Also, manager are generally free to
initialize and clean up, this make them convenient for testing.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Concerning
the second rule B, yes I love purely static helper classes, that don&amp;rsquo;t have state.
The simple &lt;a target="_blank" href="http://www.ndepend.com/Features.aspx#CQL"&gt;CQL query&lt;/a&gt;&amp;hellip;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;"&gt;SELECT&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;"&gt;METHODS&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;"&gt;
&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;"&gt;WHERE&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#000064;"&gt;IsStatic&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;&amp;hellip;just
showed that 36% of my code resides in static methods. Object-Oriented is a
great mean to model a problematic and creates proper abstraction. However, too
often classes contain functions that computes well-defined output
from well-defined input. These functions have a tendency to be &lt;i&gt;potentially &lt;/i&gt;static and to be implemented with
a lot of lines of code. Doing so result in fat classes hard to understand and
maintain. This is why I prefer to nest this code inside dedicated helper
classes. Not only this makes my instance classes smaller and clearer, but also automatic
tests for helper classes are easy to write: tests just need to create input,
call a static method, and check output done by computation. There is no hidden
state to take care of initialize. Ironically, a helper class might encapsulate
its own private mini-object model that models its own mini-concern.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;I know that
some others might see these helper classes like trash code that result from lack
of design. I agree, such helper classes could be avoided but for what? For more
instance classes whose purpose is not clear and obvious. Indeed, if their
underlying purpose was obvious in the first place, then the idea to put the
code in a static helper wouldn&amp;rsquo;t&amp;rsquo; have even come to the mind. &lt;b&gt;K&lt;/b&gt;eep &lt;b&gt;I&lt;/b&gt;t &lt;b&gt;S&lt;/b&gt;imple
&lt;b&gt;S&lt;/b&gt;tupid!&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=431581" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/5KC2WAr-S_g" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/11/16/back-to-basics-usage-of-static.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part IV</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/Oq2bAliK1-o/introduction-to-the-reactive-framework-part-iv.aspx</link><pubDate>Sun, 15 Nov 2009 23:54:11 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:429716</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>7</slash:comments><description>&lt;p&gt;In the previous post in this series, we covered how to turn .NET events into first class values through IObservable instances.&amp;#160; By doing so, we were able to do much more interesting things than just subscribe and unsubscribe, instead we were able to create a mouse drag event with little effort through composition.&amp;#160; In this post, let’s look at going from push to pull by turning collections into IObservable&amp;lt;T&amp;gt; instances.&lt;/p&gt;  &lt;p&gt;Let’s get caught up to where we are today:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;Part I – Introduction&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx"&gt;Part II – Duality of Enumerable/Observable&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx"&gt;Part III – From Events to Observables&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;From IEnumerables To Observables&lt;/h2&gt;  &lt;p&gt;As you may remember from the first post and second post in this series, we covered going from IEnumerable&amp;lt;T&amp;gt; to IObservable&amp;lt;T&amp;gt; and the duality between the two.&amp;#160; But, how can we switch back and forth between them?&amp;#160; Remember that we can create an IObservable from a couple of angles.&amp;#160; Let’s first create one by using the chaining together Empty and StartsWith combinators on the Observable class to create an IObservable&amp;lt;T&amp;gt;.&amp;#160; Note that this is not the publicly available release of the Reactive Framework, so bear with any name changes.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:30e46f2e-546c-4f63-8d74-f1342c3681a5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.Empty&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
    .StartWith(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    .StartWith(&lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    .StartWith(&lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows me to start out with an empty IObservable&amp;lt;int&amp;gt; and then pre-append the value to the front of the instance, so in this case it would render 3, 2, 1.&amp;#160; Next, let’s look at how we might generate a sequence IObservable from 1 to 10.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c0eff709-5d87-4b5d-93ae-8fbd7c8f23b4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.Generate(
    &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;,                        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Initial value&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; value &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt;,    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Predicate&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; value,            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Selector&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; value &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;);    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Iterator&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;observable.Subscribe(Console.WriteLine);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this instance, we supply an initial value, then a predicate to determine when to stop, a result selector, and then finally an iterator.&amp;#160; There are plenty of overloads as well on the Generate to suit your needs when creating IObservable&amp;lt;T&amp;gt; values.&amp;#160; But, what about taking existing IEnumerable&amp;lt;T&amp;gt; values and converting them to IObservable&amp;lt;T&amp;gt; values?&amp;#160; One could use the the Create function which allows us to create arbitrary IObservable&amp;lt;T&amp;gt; instances.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c928ec0b-99da-4182-b765-9f79f6dd8748" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.Create&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(observer &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;try&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; item &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerable)
            observer.OnNext(item);
        observer.OnCompleted();
    }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;catch&lt;/span&gt;&lt;span style="color:#000000;"&gt; (Exception exception)
    {
        observer.OnError(exception);
    }

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; () &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; { };
});
observable.Subscribe(
    value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Console.WriteLine(value),
    exn   &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Console.WriteLine(exn.ToString()));&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing in the above code is using the Create method which gives us an IObserver&amp;lt;T&amp;gt; that we can post values to.&amp;#160; In this case, we’re iterating through a collection and for each item, we’re calling OnNext.&amp;#160; At the end of the iteration, we call the OnCompleted method which indicates that there are no more values.&amp;#160; If there were some sort of exception when iterating the values, it would be caught and the exception posted to the observer.&amp;#160; We can then decide how to handle it during our Subscribe method call, if at all.&amp;#160; Finally, we return an Action which is to be called once we unsubscribe from the given IObserver&amp;lt;T&amp;gt;, which in this instance, we do nothing.&amp;#160; That’s all fine and good, but there is an easier way of course, by using the ToObservable extension method.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0c2f0204-ae18-45bc-b489-c828f699bf53" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerable.ToObservable();
observable.Subscribe(Console.WriteLine);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Going from IEnumerable&amp;lt;T&amp;gt; to IObservable&amp;lt;T&amp;gt; as you an see is quite easy.&amp;#160; Just as well, going back is easy as well through either of two methods, one being calling the ToEnumerable extension method, and the other being the GetEnumerator method.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7c1f7a89-5a72-4827-9af4-819f44b622d9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; To Observable&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerable.ToObservable();

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; And back again&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerableAgain &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable.ToEnumerable();
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; enumerator &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; observable.GetEnumerator();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This ultimately leaves us in control of when we want to switch between the pull and push model, depending on our circumstances.&amp;#160; It’s sort of like a buy one get one free in that respect.&amp;#160; What makes this interesting is to take a collection and spread it out to multiple instances, for example reading in a file and then you could have multiple subscriptions each process the data in turn.&amp;#160; Those possibilities are endless if we want to do certain actions concurrently.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a8e645ee-5a69-423c-971b-4e0dceb0113d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; text &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; File.ReadAllLines(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;DocumentToProcess.txt&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; textObservable &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; text.AsObservable();

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Process concurrently&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; textLetterSub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; text.Subscribe(CountLetters);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; textWordSub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; text.Subscribe(CountWords);
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; textVowelSub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; text.Subscribe(CountVowels);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are also more things to come when dealing with collections that we’ll get into in later posts in this series, but this is enough for now.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As I’ve stated before, the LiveLabs Reactive Framework gives us the ability to harness reactive programming and treat events as the first class citizens they should have been using LINQ expressions and other standard LINQ combinators.&amp;#160; Still, there is much to cover in this series with the standard LINQ combinators, the monadic heritage of this solution as well as turning Tasks and Asynchronous Methods into Observables.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=429716" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/Oq2bAliK1-o" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/15/introduction-to-the-reactive-framework-part-iv.aspx</feedburner:origLink></item><item><title>Code Coverage: What Is It Good For?</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/i3Y43_GrcsM/code-coverage-what-is-it-good-for.aspx</link><pubDate>Fri, 13 Nov 2009 20:06:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:427043</guid><dc:creator>Dave Laribee</dc:creator><slash:comments>13</slash:comments><description>&lt;p&gt;Absolutely nothing. Or so the story goes...&lt;/p&gt;
&lt;p&gt;I had a mini-exchange with my pal &lt;a href="http://codebetter.com/blogs/glenn.block/Default.aspx"&gt;Glenn Block&lt;/a&gt; last night on Twitter. He was recycling the somewhat old meme that code coverage is a cockamamie metric that, as he puts it, &amp;quot;[code coverage is] a false security blanket on quality&amp;quot;. &lt;/p&gt;
&lt;p&gt;Measurements (other than net profit, of course) are merely indicators. They give us insight into what we&amp;#39;re doing, red flags that help us know when to follow up on and drill into with more qualitative and hands on tools.This is, more-or-less, the idea behind &lt;a href="http://en.wikipedia.org/wiki/Autonomation"&gt;Autonomation or &amp;quot;automation with a human touch.&amp;quot;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For example: while code coverage isn&amp;#39;t proof that our testing efforts will yield higher maintainability, it does tell us a teams commitment to a test practice.&lt;/p&gt;
&lt;p&gt;I tend to believe the most interesting indicators are composite metrics. What happens when we start thinking about combining code coverage with cyclomatic complexity? I&amp;#39;d like to read a (hopefully) inverse relationship over time, coverage rising while complexity falls, as a good indicator that we&amp;#39;re doing simple design.&lt;/p&gt;
&lt;p&gt;The important thing to remember about metrics is they are only single purpose tools which, correctly applied, gift us with little tidbits of insight. It&amp;#39;s up to you to find meaningful and creative uses for the various metrics and their various combinations. Once you start doing that, measurements like code coverage can certainly help you achieve your immediate goals.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=427043" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/i3Y43_GrcsM" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/Metrics/default.aspx">Metrics</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/TDD/default.aspx">TDD</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/teams/default.aspx">teams</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/testing/default.aspx">testing</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/practices/default.aspx">practices</category><feedburner:origLink>http://codebetter.com/blogs/david_laribee/archive/2009/11/13/code-coverage-what-is-it-good-for.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part III</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/4jLu_zo0Jf4/introduction-to-the-reactive-framework-part-iii.aspx</link><pubDate>Fri, 13 Nov 2009 04:06:55 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:424920</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>11</slash:comments><description>&lt;p&gt;In my previous post in the Introduction to the Reactive Framework series, I discussed the duality between the IEnumerable/IEnumerator and IObservable/IObserver and how that enables us to generalize the ideas from LINQ to Objects to LINQ to Observables/Events.&amp;#160; After the last post, I was asked how we go from events to Observables, so let’s tackle that in this post.&lt;/p&gt;  &lt;p&gt;Before we get started, let’s get caught up to where we are today:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;Part I – Introduction&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx"&gt;Part II – Duality of Enumerable/Observable&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;From Events...&lt;/h2&gt;  &lt;p&gt;Let’s start out by looking at the way we’d usually do event-based programming in .NET.&amp;#160; First, if we have the need to pass data as arguments to our event, we’ll need to create a class which inherits from EventArgs, and in this case we have a class called SomeEventArgs.&amp;#160; This class holds nothing more than just a simple object value.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e12e2e80-0bb7-4138-b3f9-cce8a4073979" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeEventArgs : EventArgs {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; Value { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;set&lt;/span&gt;&lt;span style="color:#000000;"&gt;; }

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeEventArgs(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; value) {
        Value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; value
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Then we’d have a class which exposes an event with an EventHandler of our SomeEventArgs.&amp;#160; At some point along the way we’d have a method which invokes our event, keeping in mind that we need to check for nulls before invoking.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cad38561-d515-4291-bbcc-6a8e1fd0f73e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeClass
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;event&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventHandler&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeEvent;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;event&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventHandler&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; OtherEvent;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; InvokeEvent(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; value) {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; handler &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeEvent;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;(handler &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
            handler(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeEventArgs(value));
    }
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Other event invoking code here&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now to do something useful with it, you can subscribe and unsubscribe by using the += and –= operators.&amp;#160; In this case, I actually declared a method called SomeHandler which will react to the event arguments that I give it.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:965935b3-ce54-4d98-a324-12e3dbf0b444" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; someClass &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeClass();

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Add handler&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;someClass.SomeEvent &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeHandler;

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Remove handler&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;someClass.SomeEvent &lt;/span&gt;&lt;span style="color:#000000;"&gt;-=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeHandler;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Our SomeHandler method must follow the signature with the sender as an object and the EventArgs derived class, whatever it might be.&amp;#160; At this point we react to anything that we’re subscribed to in this method.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:52b5075c-37dc-486a-a84c-92e9c4962726" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeHandler(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; sender, SomeEventArgs e) {
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some handler code&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem with this solution is that it isn’t quite compositional.&amp;#160; What do I mean by that?&amp;#160; What if I want to combine this SomeEvent firing with another event?&amp;#160; Or if I want to ensure that an event gets posted on a certain synchronization context?&amp;#160; Or if I want to throttle the firing of an event to a certain threshold?&amp;#160; All of those are a tad more difficult using standard out of the box .NET events, but, we can with Observables.&amp;#160; So, how do we get there?&lt;/p&gt;

&lt;h2&gt;To Observables&lt;/h2&gt;

&lt;p&gt;Unlike F#, we don’t get Observables for free when we use events, so we have a little work ahead of us.&amp;#160; In order for us to go from events to Observables , we simply need to use the Observable.FromEvent method.&amp;#160; There are several flavors that we can use.&amp;#160; The first option is to use a little “magic string” juice to wrap our event in an IObservable.&amp;#160; This method takes the object to bind for the event and the event name as a string.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c9edcb3b-eae2-4bac-9444-35daecc57ee1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventExtensions {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Event&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetSomeEvent(
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeClass someClass) {
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.FromEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(
        someClass, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;SomeEvent&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This method then returns to us an IObservable&amp;lt;Event&amp;lt;SomeEventArgs&amp;gt;&amp;gt; which allows us to get access to the EventArgs and the Sender through their respective properties on the Event class.&amp;#160; Personally, I’m not a fan of magic strings at all and if I can get away with not using them, all the better.&amp;#160; Luckily, there are other options.&amp;#160; The next option is to use Action&amp;lt;EventHandler&amp;lt;EventArgs&amp;gt;&amp;gt; arguments for both the subscribe and unsubscribe.&amp;#160; Keep in mind that this only works for those events which are declared to use EventHandler&amp;lt;TEventArgs&amp;gt;.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2385afb4-95b9-4a14-99ae-4a68ce60f160" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventExtensions {
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Event&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetSomeEvent(
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt; SomeClass someClass) {
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.FromEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(
        h &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; someClass.SomeEvent &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; h,
        h &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; someClass.SomeEvent &lt;/span&gt;&lt;span style="color:#000000;"&gt;-=&lt;/span&gt;&lt;span style="color:#000000;"&gt; h);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;If your given event doesn’t have the signature of EventHandler&amp;lt;TEventArgs&amp;gt;, you can still take advantage of the Reactive Framework as well.&amp;#160; In this instance, let’s extend the WPF IInputElement to wrap the MouseMove event as an IObservable&amp;lt;Event&amp;lt;MouseEventArgs&amp;gt;&amp;gt;.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8ba746cd-1754-49bc-956e-eda162de353a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Event&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;MouseEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetMouseMove(
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt; IInputElement inputElement)
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.FromEvent(
        (EventHandler&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;MouseEventArgs&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; h) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; MouseEventHandler(h),
        h &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; inputElement.MouseMove &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; h,
        h &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; inputElement.MouseMove &lt;/span&gt;&lt;span style="color:#000000;"&gt;-=&lt;/span&gt;&lt;span style="color:#000000;"&gt; h);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What this does for us behind the scenes is create an IObservable/IObserver to handle the events as they happen.&amp;#160; Each invocation of the event causes the OnNext to be fired with the Event&amp;lt;TEventArgs&amp;gt; argument.&amp;#160; In this case, the OnComplete and OnException are not implemented as part of the IObserver instance.&lt;/p&gt;

&lt;h2&gt;An Example&lt;/h2&gt;

&lt;p&gt;Let’s create a little example of drawing on a canvas in WPF.&amp;#160; In order to do that, we’ll need not only the MouseMove extension as we have above, but also MouseLeftButtonDown and MouseLeftButtonUp.&amp;#160; Each of those extension methods should follow the same pattern as above in terms of needing the extra parameter to convert the EventHandler&amp;lt;MouseButtonEventArgs&amp;gt; to a MouseButtonEventHandler.&amp;#160; &lt;/p&gt;

&lt;p&gt;First, we need to capture position of the mouse during our mouse moves.&amp;#160; After that, we’ll need to zip together the mouse move and another mouse move that is one ahead in order to capture a delta.&amp;#160; Finally, we’ll capture the mouse downs and the mouse deltas until the mouse ups happen.&amp;#160; Listed below is code to make that happen.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5147173a-2e9d-4704-9a65-68927643a5c8" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseMoves &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; mm &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; mainCanvas.GetMouseMove()
                 let location &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; mm.EventArgs.GetPosition(mainCanvas)
                 &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; { location.X, location.Y};
                 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDiffs &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseMoves
    .Skip(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    .Zip(mouseMoves, (l, r) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; {X1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; l.X, Y1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; l.Y, X2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; r.X, Y2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; r.Y});

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; _  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; mainCanvas.GetMouseLeftButtonDown()
                &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; md &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDiffs.Until(
                    mainCanvas.GetMouseLeftButtonUp())
                &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; md;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code should read rather straight forward as we attach to the mainCanvas object in order to draw.&amp;#160; The interesting functions are the mouseDiffs and the mouseDrag.&amp;#160; The mouseDiffs takes the mouseMoves and then skips 1 ahead and zips it with another mouseMoves, and projects to hold both left and right values.&amp;#160; The mouseDrag function takes events from left mouse button down and then from mouseDiffs until the left mouse button up Observable happens.&amp;#160; &lt;/p&gt;

&lt;p&gt;Now that we have our mouseDrag, what can we do with it?&amp;#160; Well, let’s go ahead and draw on our canvas:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f3e437d0-d390-4df0-81f8-44a6550498bb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseSub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDrag.Subscribe(item &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; line &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Line
                  {
                      Stroke &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Brushes.LightSteelBlue,
                      X1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; item.X1,
                      X2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; item.X2,
                      Y1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; item.Y1,
                      Y2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; item.Y2,
                      StrokeThickness &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;5&lt;/span&gt;&lt;span style="color:#000000;"&gt;
                  };
    mainCanvas.Children.Add(line);
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our mouseSub subscription creates a new line with our coordinates from zipped mouseDrag Observable and then adds it to our canvas.&amp;#160; Consider how little code was required to make this happen, and try doing that with standard .NET events.&amp;#160; Quite frankly, this kind of solution makes event-based programming using .NET events expressive enough to be usable.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As I’ve stated before, the LiveLabs Reactive Framework gives us the ability to harness reactive programming and treat events as the first class citizens they should have been using LINQ expressions and other standard LINQ combinators.&amp;#160; Still, there is much to cover in this series with the standard LINQ combinators, the monadic heritage of this solution and more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=424920" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/4jLu_zo0Jf4" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx</feedburner:origLink></item><item><title>Astoria, SSIS Adapters and BDD – On BDD and MSpec</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/7sgzxk3aaxE/astoria-ssis-adapters-and-bdd-on-bdd-and-mspec.aspx</link><pubDate>Fri, 13 Nov 2009 01:04:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:424518</guid><dc:creator>hdierking</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;Let me preface this post by saying that it is about my (early) experiences practicing &lt;a href="http://en.wikipedia.org/wiki/Behavior_driven_development"&gt;BDD&lt;/a&gt; in the context of a specific specification framework &amp;ndash; &lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx"&gt;MSpec&lt;/a&gt;.&amp;nbsp; There are plenty of folks out there who are much more authoritative on this subject than I.&amp;nbsp; And if you happen to be one of those people, I would love to get your feedback on what I&amp;rsquo;ve done here and how I could do it better.&lt;/p&gt;
&lt;p&gt;In developing a &lt;a href="http://codebetter.com/blogs/howard.dierking/archive/2009/10/28/astoria-ssis-adapters-and-bdd-take-1.aspx"&gt;SSIS data source to talk to an ADO.NET Data Services&lt;/a&gt; endpoint, shifting my approach from a basic &lt;a href="http://en.wikipedia.org/wiki/Test_Driven_Development"&gt;TDD&lt;/a&gt; style to specifications was not originally part of the plan.&amp;nbsp; However, as I got deeper into development, I felt as though I actually wanted a bit more rigor in terms of semantic restrictions (e.g. arrange/act/assert) than I was getting out of the box from my unit test framework.&amp;nbsp; MSpec had been on my radar for a little while (I was introduced to both this framework and BDD in general at &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc337902.aspx"&gt;ALT.NET&lt;/a&gt;), so I took a quick look - and after staring oddly for a moment at the =()=&amp;gt; syntax decided that it provided the kind of semantic consistency I was really looking for.&amp;nbsp; Additionally, the fact that it could plug into the &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; unit test runner (with underscores removed from boxcar casing no less!) made it quick and very easy to get up and going on the journey writing specifications rather than tests.&amp;nbsp; Going through the specification writing exercise on this project has me convinced at this point that as someone else once said, BDD is TDD done right.&amp;nbsp; I certainly plan to keep pursuing specifications over traditional unit tests in future efforts.&lt;/p&gt;
&lt;p&gt;So where to start?&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m not going to spend time in this post talking about how to use MSpec &amp;ndash; there&amp;rsquo;s already lots of good guidance out there.&amp;nbsp; That said, because the syntax initially looks a bit funky, I will say 2 quick things that are helpful to keep in mind when you first look at specifications built on top of MSpec.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Everything in your specification (test) class is a field.&amp;nbsp; And all of the work&amp;nbsp; gets down via delegate fields which are of types with names like &amp;ldquo;Establish&amp;rdquo;, &amp;ldquo;Because&amp;rdquo;, and &amp;ldquo;It&amp;rdquo; &lt;/li&gt;
&lt;li&gt;Regarding the =()=&amp;gt; syntax, you are probably already comfortable with lambda expressions, so you can ignore the =&amp;gt; on the right - and you can get the assignment operator on the left.&amp;nbsp; Therefore, that just leaves the empty parenthetical &amp;ndash; and this just means that your delegate (lambda) is for a function that has no arguments.&amp;nbsp; There, all better? &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I already had some unit tests written &amp;ndash; though some of them were more of integration tests and revealed some pretty serious problems in my design &amp;ndash; but we&amp;rsquo;ll deal more with those topics later.&amp;nbsp; So my starting point was to try and re-write some of my existing tests as specifications.&amp;nbsp; For example, take the following:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;[Test]
public void Given_Null_URI_Throw_Exception() {
    try {
        new DataServicesQuery(_dataServicesQueryBuilder, null, &amp;quot;Suppliers&amp;quot;);
    }
    catch(ArgumentNullException ex) {
        Assert.AreEqual(&amp;quot;serviceUri&amp;quot;, ex.ParamName);
    }
}&lt;/pre&gt;
&lt;p&gt;Re-writing this as a specification resulted in the following:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;public class when_service_uri_is_null : with_dummy_resource_builder_and_reader
{
    static NullReferenceException lastException;

    Because service_uri_is_null = () =&amp;gt; {
        try {
            queryBuilder.ServiceURI = null;
            queryBuilder.ResourcePath = &amp;quot;Products&amp;quot;;
            queryBuilder.GetQuery();
        }
        catch (NullReferenceException ex) {
            lastException = ex;
        }
    };

    It should_have_specific_error_message = 
        () =&amp;gt; lastException.Message.ShouldEqual(&amp;quot;Service URI must be provided&amp;quot;);

    It should_raise_an_exception = 
        () =&amp;gt; lastException.ShouldNotBeNull();
}&lt;/pre&gt;
&lt;p&gt;A couple things to point out here.&amp;nbsp; &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First and foremost, there is a more rigid semantic that is enforced by both convention and even by the type system.&amp;nbsp; The convention is that classes follow a &amp;ldquo;when X [with Y]&amp;rdquo; pattern (I found that this class name should map very tightly to the instructions contained in the Establish and/or Because expressions &amp;ndash; if it does not, you may have a problem in your design).&amp;nbsp; Also, assertion (&amp;ldquo;It&amp;rdquo; expressions) delegates should follow a naming convention whereby &amp;ldquo;should &amp;hellip;&amp;rdquo; is followed by a description of the assertion.&amp;nbsp; MSpec also uses the type system to enforces this convention in how the delegate types are named (e.g. &amp;ndash; &amp;ldquo;Because&amp;rdquo;, &amp;ldquo;It&amp;rdquo;). &lt;/li&gt;
&lt;li&gt;All of the NUnit attributes identifying test fixtures and tests are gone.&amp;nbsp; MSpec is smart enough to figure out what is a test vs. what is a regular old class based on the MSpec delegates that make up the class.&amp;nbsp; When looked at through the &lt;a href="http://www.nunit.org/index.php"&gt;NUnit&lt;/a&gt; lens, the specification class remains the test fixture, and each &amp;ldquo;It&amp;rdquo; delegate instance becomes an NUnit test. &lt;/li&gt;
&lt;li&gt;Notice the with_dummy_resource_builder_and_reader class.&amp;nbsp; As with unit tests, inheritance can be a great way to provide shared context to multiple test fixtures.&amp;nbsp; This base class is responsible for 2 things: 1) exposing a few data fields for its child classes, and 2) providing its own Establish delegate to setup those data fields. &lt;/li&gt;
&lt;li&gt;For me, this format of specifications seemed to yield another benefit &amp;ndash; without explicit effort.&amp;nbsp; That benefit was more tests with each test having a much better definition around its responsibility. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This particular type of test &amp;ndash; one where you&amp;rsquo;re expecting an exception &amp;ndash; was one where I wasn&amp;rsquo;t 100% confident in my approach.&amp;nbsp; However, I didn&amp;rsquo;t find any baked in support from MSpec &amp;ndash; and frankly, for the framework to provide a feature similar to NUnit&amp;rsquo;s ExpectedExceptionAttribute doesn&amp;rsquo;t really make a lot of sense to me for writing specifications anyway.&amp;nbsp; You can see above the approach that I took &amp;ndash; simply trap the exception and then run a couple tests over the exception to assert various characteristics of it.&amp;nbsp; If there&amp;rsquo;s a better way to go about this, I would love to hear it.&lt;/p&gt;
&lt;p&gt;After I started getting the hang of writing specifications by rewriting my tests, I began venturing into new parts of my application &amp;ndash; and this is where I really started realizing just how much ceremony exists in traditional unit testing frameworks compared to something like MSpec.&amp;nbsp; Because mostly everything in MSpec is a field, there&amp;rsquo;s no need to provide any kind of method body, etc. after declaring the title of the specification.&amp;nbsp; So, for example, when I wanted to write specifications around executing a query, I was able to quickly capture the specs as follows:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;public class when_query_is_executed_for_products_with_expanded_suppliers
     : with_query_shape_factory
{
    It should_have_2_result_sets;
    It should_have_a_result_set_named_products;
    It should_have_8_columns_in_products_result_set;
    It should_have_77_rows_in_products_result_set;
    It should_have_a_result_set_named_suppliers;
    It should_have_13_columns_in_suppliers_result_set;
    It should_have_77_rows_in_suppliers_result_set;
}&lt;/pre&gt;
&lt;p&gt;Remember, &amp;ldquo;It&amp;rdquo; is simply a type and &amp;ldquo;should_have_2_result_sets&amp;rdquo; is simply a field declaration &amp;ndash; no need to initialize it with anything just yet.&amp;nbsp; When the MSpec runner is executing, it checks to see whether the field has been initialized &amp;ndash; if it hasn&amp;rsquo;t, as in this case, it flags it as if you used NUnit&amp;rsquo;s IgnoreAttribute.&lt;/p&gt;
&lt;p&gt;Finally, I mentioned earlier in this post about using ReSharper to run MSpec specifications.&amp;nbsp; While I would probably still be interested in MSpec if this integration didn&amp;rsquo;t exist, the fact that it was there removed any question or hesitation.&amp;nbsp; We talk a lot about the readability of tests, and of tests being the spec &amp;ndash; and this is, based on what I&amp;rsquo;ve experienced so far, where specification frameworks like MSpec really shine.&amp;nbsp; Bringing that goodness directly into Visual Studio ala ReSharper mades the feedback loop even tighter, and really made for a happy development process.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/Untitled_5F00_06CF122B.png"&gt;&lt;img height="731" width="499" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/Untitled_5F00_thumb_5F00_48F27AAE.png" alt="Untitled" border="0" title="Untitled" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve read this far and you have no clue what this project is that I&amp;rsquo;m talking about, see the overview &lt;a href="http://codebetter.com/blogs/howard.dierking/archive/2009/10/28/astoria-ssis-adapters-and-bdd-take-1.aspx"&gt;here&lt;/a&gt;.&amp;nbsp; If you want to download the code and look at it yourself, check it out at &lt;a href="http://code.msdn.microsoft.com/ssisastoria" title="http://code.msdn.microsoft.com/ssisastoria"&gt;http://code.msdn.microsoft.com/ssisastoria&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=424518" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/7sgzxk3aaxE" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/R_2300_/default.aspx">R#</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/BDD/default.aspx">BDD</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/11/12/astoria-ssis-adapters-and-bdd-on-bdd-and-mspec.aspx</feedburner:origLink></item><item><title>Switching virtual networks the easy way</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/y2sk2SyujWI/switching-virtual-networks-the-easy-way.aspx</link><pubDate>Thu, 12 Nov 2009 10:40:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:423979</guid><dc:creator>pvanooijen</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;For my laptop I am using server 2008 as OS. The main reason is Hyper V, for every different project I can fire up one or more appropriate virtual servers. For the details on setting up such an environment see &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/09/15/nice-laptop-for-a-developer-running-windows-workstation-2008.aspx"&gt;this post&lt;/a&gt;. On the advantages of using Server 2008 &lt;strong&gt;R2 &lt;/strong&gt;see &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2009/09/22/windows-workstation-2008-r2-an-even-better-os-for-a-laptop.aspx"&gt;this post&lt;/a&gt;. Traveling around the laptop meets a lot of different networks, from the Office domain of which it is a member to some kind of wireless in the wild open. Sometimes I want my virtual servers to see and be seen by the outer world, sometimes I don&amp;rsquo;t. &lt;/p&gt;
&lt;p&gt;You can do a lot of network configuration in Hyper-V, in &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/10/28/hyper-v-virtual-networks-and-a-handy-usb-ethernet-gadget.aspx"&gt;this post&lt;/a&gt; I described a rather baroque configuration where all kinds of network connectivity can be switched on and off. Which worked well but is far more complicated than needed. By now I have found out that switching networks in Hyper &amp;ndash;V can be as simple as this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a virtual network for every adapter on the laptop. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Virtualnetworkmanager_5F00_2A3D2884.png"&gt;&lt;img height="653" width="693" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Virtualnetworkmanager_5F00_thumb_5F00_35F26FB6.png" alt="Virtual network manager" border="0" title="Virtual network manager" style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;My laptop has an internal (fixed IP address) adapter and an &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/10/28/hyper-v-virtual-networks-and-a-handy-usb-ethernet-gadget.aspx"&gt;external USB adapter&lt;/a&gt; (depending on DHCP). The Hyper-V virtual network manager does not allow you (yet ?) to pick Wireless or dial-up adapters for a virtual network. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Assign each virtual machine just one network adapter.&lt;/li&gt;
&lt;li&gt;Select the active adapter as needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Virtualmachinemanager_5F00_369B25E0.png"&gt;&lt;img height="663" width="705" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Virtualmachinemanager_5F00_thumb_5F00_21C92D60.png" alt="Virtual machine manager" border="0" title="Virtual machine manager" style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The good thing is that this can be done while the virtual machine is running. Changing the network is instantaneous. Just like pulling the cable and inserting another one. So much simpler than all the &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/10/28/hyper-v-virtual-networks-and-a-handy-usb-ethernet-gadget.aspx"&gt;previous multi network fiddling&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=423979" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/y2sk2SyujWI" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/peter.van.ooijen/archive/2009/11/12/switching-virtual-networks-the-easy-way.aspx</feedburner:origLink></item><item><title>[ANN] DC ALT.NET – 11/24/2009 “Stop Talking, Start Teaching” with Jeff Casimir</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/pi_ZJD08yxU/ann-dc-alt-net-11-24-2009-stop-talking-start-teaching-with-jeff-casimir.aspx</link><pubDate>Tue, 10 Nov 2009 19:48:14 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:420719</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;As a group, we’ve been focusing a lot on technology on such things as JavaScript, MongoDB and so on.&amp;#160; This month, I’d like to take a step back as a group and instead focus on some soft skills.&amp;#160; When I started this group back in 2007, I had the idea that this group would help others not only become better technologists in a wide array of technologies, but also better teachers as well.&amp;#160; On November 24th, we have Jeff Casimir to help us understand how to become a better teacher with his presentation “Stop Talking, Start Teaching: 5 Rules for Successful Presentations and Demos”.&amp;#160; Listed below are the details:&lt;/p&gt;  &lt;h2&gt;Stop Talking, Start Teaching: 5 Rules for Successful Presentations &amp;amp; Demos&lt;/h2&gt;  &lt;p&gt;Some of the most important work we do as programmers involves pitching ideas. Whether it&amp;#39;s inside the team, searching for funders, or running demos for potential clients, we all need to present ideas and cultivate an audience.&amp;#160; The past decade has seen the rise of the presentation as an important communication medium, but few people do it well. There are many resources on how to make good looking slides, but we need to focus on the method, structure, and content.&lt;/p&gt;  &lt;p&gt;We don&amp;#39;t need to be &amp;quot;speakers&amp;quot; – we need to be teachers. In this session we&amp;#39;ll explore the five most essential rules to successful teaching and how they apply to you.&lt;/p&gt;  &lt;p&gt;Attendees will, by the end of the session, have a framework for designing and critiquing their work and that of others. This new understanding will make them better teammates and more successful salespeople.&lt;/p&gt;  &lt;h2&gt;About Jeff Casimir&lt;/h2&gt;  &lt;p&gt;After majoring in Computer Systems Engineering I joined Teach for America and began a career in education. I taught high school Computer Science for four years before moving into school administration. As a Vice Principal I was responsible for evaluating and hiring new teachers, observing and coaching&amp;#160; existing teachers, and defining much of the school&amp;#39;s academic process. Most of all, I hate boring presentations. I want these smart, experienced people to Stop Talking, and Start Teaching.&lt;/p&gt;  &lt;p&gt;Hope to see you there!&lt;/p&gt; &lt;a href="http://www.eventbrite.com/event/485519201?ref=ebtn" target="_blank"&gt;&lt;img border="0" src="http://www.eventbrite.com/registerbutton?eid=485519201" alt="" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=420719" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/pi_ZJD08yxU" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/ALT.NET/default.aspx">ALT.NET</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/User+Groups/default.aspx">User Groups</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/10/ann-dc-alt-net-11-24-2009-stop-talking-start-teaching-with-jeff-casimir.aspx</feedburner:origLink></item><item><title>Design Conundrum for StoryTeller -- Opinions Wanted</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/HuWz_oo2-lE/design-conundrum-for-storyteller-opinions-wanted.aspx</link><pubDate>Mon, 09 Nov 2009 21:31:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:420125</guid><dc:creator>Jeremy D. Miller</dc:creator><slash:comments>35</slash:comments><description>&lt;p&gt;I made the &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2009/08/24/how-about-a-storyteller-preview-release.aspx"&gt;preview release of StoryTeller&lt;/a&gt; a couple months ago as soon as I got some sort of &amp;quot;intelligent editor&amp;quot; functionality going in the WPF client so you very rarely ever had to expose your tender eyes to the horrors of the underlying Xml storage.&amp;nbsp; Fine, except for one little problem:&amp;nbsp; the UX and usability suck (and I know because my team uses it and I&amp;#39;m irritated with it when I use it.&amp;nbsp; Hooray for dogfooding).&amp;nbsp; It&amp;#39;s also butt ugly.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Flash forward to the past couple months.&amp;nbsp; My team at Dovetail is putting the finishing touches on our online rules engine editor feature in our product (I&amp;#39;m blogging about it soon).&amp;nbsp; We basically use StoryTeller as our &amp;quot;DSL&amp;quot; engine, but we built a completely web based editor for the StoryTeller grammars and integrated &lt;i&gt;that&lt;/i&gt; into our web application.&amp;nbsp; I had originally envisioned this module as being written in Silverlight because of the complexity of the dynamic layout and behavior.&amp;nbsp; Instead, we decided to build the UI out with client side JavaScript.&amp;nbsp; We do generate HTML templates on the server representing the DSL grammars that embed behavioral directives with class attributes and copious usage of the &lt;a href="http://plugins.jquery.com/project/metadata"&gt;jQuery Metadata plugin&lt;/a&gt;, but all the behavior of the UI is accomplished through client side scripting.&amp;nbsp; My team might not completely agree, but frankly I&amp;#39;m thrilled with how it&amp;#39;s gone.&amp;nbsp; The UI looks pretty good and the screen behavior was relatively easy to create with the HTML/JavaScript/CSS/jQuery combination. We were even able to TDD quite a bit of the JavaScript development using QUnit.&lt;/p&gt;
&lt;p&gt;So here&amp;#39;s my dilemma, I&amp;#39;m committed to making StoryTeller rock and I absolutely need to replace the existing test editor functionality.&amp;nbsp; At this point I have two options -- either write a new version using WPF that closely mimics the improved usability that we built at Dovetail or stick with JavaScript/jQuery/HTML and embed a WebBrowser control into StoryTeller&amp;#39;s WPF client (which I already do).&amp;nbsp; Here are the facts as I see them and I&amp;#39;m obviously biased at the moment toward the HTML/JavaScript model -- but I&amp;#39;m looking for other people&amp;#39;s opinions, especially about WPF:&lt;/p&gt;
&lt;h3&gt;&lt;b&gt;Pro-WPF&lt;/b&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;It&amp;#39;s an excuse/reason to learn a lot more about WPF for me.&amp;nbsp; I think I&amp;#39;d finally have to dig into data templates, styling, and attached behaviors to do this well.&lt;/li&gt;
&lt;li&gt;Embedding a WebBrowser control into a WPF app and letting more COM stuff in the door is kloogey and occasionally error prone.&amp;nbsp; The integration is odd&lt;/li&gt;
&lt;li&gt;Might run faster&lt;/li&gt;
&lt;li&gt;Richer editing controls&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;&lt;b&gt;Pro-HTML/JavaScript/jQuery&lt;/b&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;The amazing jQuery ecosystem of plugins and support.&amp;nbsp; At a minimum, I&amp;#39;d be using the jQuery UI sortable/draggable stuff, jQuery Validation, and jQuery Metadata.&amp;nbsp; I went looking today and I just don&amp;#39;t see any substantial OSS community around WPF other than more copycat MVVM frameworks than you can shake a stick at.&lt;/li&gt;
&lt;li&gt;I&amp;#39;m much more familiar with the jQuery way of doing things and HTML&lt;/li&gt;
&lt;li&gt;I&amp;#39;ve got a successful working prototype to start from&lt;/li&gt;
&lt;li&gt;Many of the mechanics are much simpler with jQuery than with WPF.&amp;nbsp; For example, I want users to be able to reorder parts of the StoryTeller specification by dragging elements around.&amp;nbsp; This is just a matter of some declarative class names in HTML with jQuery, but the&lt;a href="http://blogs.msdn.com/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx"&gt; WPF equivalent looks exceptionally nasty&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;I actually like coding with JavaScript and the dynamic features make it relatively easy to extend the model with less ceremony than a static typed language&lt;/li&gt;
&lt;li&gt;The feedback cycle is faster with JavaScript than C#&lt;/li&gt;
&lt;li&gt;I&amp;#39;d really love to be able to allow users to embed the StoryTeller editor in their own applications, and the HTML/JavaScript model has much more reach than a WPF or even a Silverlight model would.&lt;/li&gt;
&lt;li&gt;I think this model is actually much easier to test than the WPF model.&amp;nbsp; It&amp;#39;s hard to write automated tests against WPF without something special like TypeMock that can get around how the WPF team sealed so many of the routed event classes or an expensive 3rd party tool that screen scrapes.&lt;/li&gt;
&lt;li&gt;Truth be told, I don&amp;#39;t really like WPF.&amp;nbsp; It&amp;#39;s weird and has more &amp;quot;oddball-ness&amp;quot; to it than any other technology I&amp;#39;ve ever used.&amp;nbsp; There are parts of it that I love (the layout system comes to mind), and others that drive me batty (XAML&amp;#39;s runaway verbosity, binding expressions, routed events).&amp;nbsp; I don&amp;#39;t hate it on first sight the way I did WebForms back in the day, but something about it just doesn&amp;#39;t agree with me.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;&lt;b&gt;Call it a Wash&lt;/b&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;I&amp;#39;ve got JetBrains RubyMine for the JavaScript editing, so the tooling support is not really an advantage for the WPF/C# solution anymore&lt;/li&gt;
&lt;li&gt;qUnit isn&amp;#39;t that hard to use, so I&amp;#39;m not sure that the C# code is any easier to test and I have a recipe for integrating qUnit tests into an automated build anyway.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Anyway, it&amp;#39;s been interesting.&amp;nbsp; Now, what do you think?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=420125" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/HuWz_oo2-lE" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/jeremy.miller/archive/tags/StoryTeller/default.aspx">StoryTeller</category><feedburner:origLink>http://codebetter.com/blogs/jeremy.miller/archive/2009/11/09/design-conundrum-for-storyteller-opinions-wanted.aspx</feedburner:origLink></item><item><title>On being Culture Aware</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/LJAVp1_mWpk/on-being-culture-aware.aspx</link><pubDate>Mon, 09 Nov 2009 14:26:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:417654</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>9</slash:comments><description>&lt;p&gt;






 
  
  
 




 
  Normal
  0
  
  
  21
  
  
  false
  false
  false
  
  FR
  X-NONE
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 





&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;A typical development pitfall comes
from forgetting about the culture setup of the machine on which the code is
executed in production (typical &lt;b&gt;it works on my machine
&lt;/b&gt;mistake). Imagine that a program running on a French machine stores a float as
the string &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;3,14&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;&amp;quot;&lt;/span&gt;&lt;span lang="EN-US"&gt;. The float value cannot be restored from the same program running
on a US machine. It would expect the string to be &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;3.14&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;&amp;quot;&lt;/span&gt;&lt;span lang="EN-US"&gt; with a dot, not a coma.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;To prevent such mistake in the
&lt;a target="_blank" href="http://www.NDepend.com"&gt;&lt;b&gt;NDepend&lt;/b&gt; &lt;/a&gt;code I
created the following &lt;b&gt;&lt;a target="_self" href="http://www.ndepend.com/Features.aspx#CQL"&gt;CQL
rule&lt;/a&gt;&lt;/b&gt;. Basically, it forces the code base to use only &lt;/span&gt;&lt;span style="font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;Parse()&lt;/span&gt;&lt;span lang="EN-US"&gt; and &lt;/span&gt;&lt;span style="font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;ToString()&lt;/span&gt;&lt;span lang="EN-US"&gt; methods that accept a parameter of
type &lt;/span&gt;&lt;span style="font-size:10pt;font-family:Consolas;" lang="EN-US"&gt;IFormatProvider&lt;/span&gt;&lt;span lang="EN-US"&gt;.&lt;span&gt;&amp;nbsp; &lt;/span&gt;As far
as I know, the problem occurs on DateTime, float (Single), double and
decimal.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:green;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:green;" lang="EN-US"&gt;// &amp;lt;Name&amp;gt;&lt;/span&gt;&lt;span style="background:lime none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:green;" lang="EN-US"&gt;Float and Date Parsing must be
culture aware&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:green;" lang="EN-US"&gt;&amp;lt;/Name&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;WARN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;IF&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#000064;" lang="EN-US"&gt;Count&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &amp;gt; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;0&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;IN&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;SELECT&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;METHODS&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;FROM&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;TYPES&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;quot;System.DateTime&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;quot;System.Single&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;quot;System.Double&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;,&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;quot;System.Decimal&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:green;" lang="EN-US"&gt;// The NameLike CQL clause operates on the signature&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:green;" lang="EN-US"&gt;// &amp;quot;methodName(type1,type2...typeN)&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;WHERE&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; (&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#000064;" lang="EN-US"&gt;NameLike&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;quot;Parse\(&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;OR&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#000064;" lang="EN-US"&gt;NameLike&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;quot;ToString\(&amp;quot;&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;) &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:blue;" lang="EN-US"&gt;AND&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;!&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#000064;" lang="EN-US"&gt;NameLike&lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:black;" lang="EN-US"&gt; &lt;/span&gt;&lt;span style="background:white none repeat scroll 0% 0%;font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;quot;IFormatProvider&amp;quot;&lt;/span&gt;&lt;span style="font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:1pt 0cm;"&gt;&lt;span style="font-size:9pt;font-family:Consolas;color:#a31515;" lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;With this rule set up, in the future
we won&amp;rsquo;t forget about caring for culture when converting from/to string.
Previously, I already wrote a post concerning this &lt;b&gt;&lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2009/06/21/agile-behavior-nurture-knowledge-database.aspx"&gt;Nurture Knowledge Database&lt;/a&gt;&lt;/b&gt; agile state
in mind.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;FxCop has a similar rule named
&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms182190%28VS.80%29.aspx"&gt;Specify IFormatProvider&lt;/a&gt;. We are inclined to append this CQL rule to the set of default CQL rules, what is your opinion?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=417654" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/LJAVp1_mWpk" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/11/09/on-being-culture-aware.aspx</feedburner:origLink></item><item><title>Using Latest NUnit Version With MSpec</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/63qD6S3UuT8/using-latest-nunit-version-with-mspec.aspx</link><pubDate>Sun, 08 Nov 2009 20:11:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:416195</guid><dc:creator>hdierking</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;As I&amp;rsquo;ve been working another blog post about my adventures in learning BDD ala &lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx"&gt;MSpec&lt;/a&gt;, I ran into something that I wanted to share as the problem wasn&amp;rsquo;t immediately obvious (to me anyway).&lt;/p&gt;
&lt;p&gt;The problem is that MSpec has a dependency on version 2.4.6.0 of the nunit.framework assembly &amp;ndash; and frankly, it&amp;rsquo;s not really a very big problem.&amp;nbsp; In fact, I didn&amp;rsquo;t even notice it until now because I had been &lt;a href="http://www.lostechies.com/blogs/seanbiefeld/archive/2009/08/25/step-by-step-to-using-machine-specifications-with-resharper.aspx"&gt;running all of my specs exclusively through the Resharper runner (enabled by the MSpec plugin&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;** As a side note, using R# with the MSpec plugin absolutely rocks, and while the runner is needed for integrating MSpec with your automated builds, I would totally recommend R# for your regular dev rhythm.&lt;/p&gt;
&lt;p&gt;In my project, I originally had some integration tests in the same assembly as my specifications.&amp;nbsp; As a result, I had a direct reference to NUnit (version 2.5.2.9222) in addition to the reference to Machine.Specifications and Machine.Specifications.NUnit (which has a reference to version 2.4.6.0).&amp;nbsp; As a result, the later version of NUnit was being copied to my bin folder and throwing a file load exception for every specification when run using the console runner.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/Untitled_5F00_549F853E.png"&gt;&lt;img height="224" width="668" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/Untitled_5F00_thumb_5F00_31FB46C3.png" alt="Untitled" border="0" title="Untitled" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;My initial reaction was to simply move all my integration tests to a different assembly and remove the direct NUnit reference in my specifications assembly (remember that referencing Machine.Specifications.NUnit will copy the version of NUnit to the bin folder that MSpec expects.&amp;nbsp; No problem &amp;ndash; everything works as you would expect.&lt;/p&gt;
&lt;p&gt;However, it seems reasonable to me that, at least in larger projects that are wanting to incorporate specs and not have to re-write all existing tests, that it would be good to simply reconfigure the MSpec console runner to use the latest version of NUnit.&amp;nbsp; This can be done easily using the standard .NET assembly binding redirection feature.&amp;nbsp; Simply create a file named Machine.Specifications.ConsoleRunner.exe.config and drop it into the MSpec folder that contains your console runner (Machine.Specifications.ConsoleRunner.exe).&amp;nbsp; The contents of the file should look like this.&lt;/p&gt;
&lt;pre name="code" class="xml"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;
&amp;lt;configuration&amp;gt;
    &amp;lt;runtime&amp;gt;
      &amp;lt;assemblyBinding xmlns=&amp;quot;urn:schemas-microsoft-com:asm.v1&amp;quot;&amp;gt;
        &amp;lt;dependentAssembly&amp;gt;
          &amp;lt;assemblyIdentity name=&amp;quot;nunit.framework&amp;quot;
                            publicKeyToken=&amp;quot;96d09a1eb7f44a77&amp;quot;
                            culture=&amp;quot;neutral&amp;quot; /&amp;gt;
          &amp;lt;bindingRedirect oldVersion=&amp;quot;2.4.6.0&amp;quot;
                           newVersion=&amp;quot;2.5.2.9222&amp;quot;/&amp;gt;
        &amp;lt;/dependentAssembly&amp;gt;
      &amp;lt;/assemblyBinding&amp;gt;
    &amp;lt;/runtime&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;
&lt;p&gt;Run the MSpec console again, and voila! Works like a charm.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/Untitled2_5F00_5EA4009C.png"&gt;&lt;img height="199" width="529" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/Untitled2_5F00_thumb_5F00_6B9DE0AD.png" alt="Untitled2" border="0" title="Untitled2" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=416195" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/63qD6S3UuT8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/R_2300_/default.aspx">R#</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/BDD/default.aspx">BDD</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/MSpec/default.aspx">MSpec</category><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/11/08/using-latest-nunit-version-with-mspec.aspx</feedburner:origLink></item><item><title>Astoria, SSIS Adapters and BDD – Code</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/PSIi7C0B3xI/astoria-ssis-adapters-and-bdd-code.aspx</link><pubDate>Fri, 06 Nov 2009 19:57:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:413671</guid><dc:creator>hdierking</dc:creator><slash:comments>4</slash:comments><description>&lt;p&gt;I&amp;rsquo;m currently working on the post talking about how, in the course of writing my SSIS data source for ADO.NET Data Services, I stumbled into replacing my unit tests with specifications.&amp;nbsp; However, in advance of that, I wanted to go ahead and provide the link to the source code.&amp;nbsp; I would love to get feedback on what&amp;rsquo;s working and what&amp;rsquo;s not so that I can get better &amp;ndash; and so that ideally, those the random stranger who downloads and looks at it on some idle Tuesday gets better as well.&lt;/p&gt;
&lt;p&gt;The code can be found at &lt;a href="http://code.msdn.microsoft.com/ssisastoria" title="http://code.msdn.microsoft.com/ssisastoria"&gt;http://code.msdn.microsoft.com/ssisastoria&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=413671" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/PSIi7C0B3xI" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/11/06/astoria-ssis-adapters-and-bdd-code.aspx</feedburner:origLink></item><item><title>December 2-3 London Workshop Update</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/3xBeH-OvrRs/december-2-3-london-workshop-update.aspx</link><pubDate>Thu, 05 Nov 2009 15:29:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:411776</guid><dc:creator>Dave Laribee</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;Forgive the self-promotional interruption, but quick housekeeping note:&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve asked the company hosting my workshop in London to reduce the price of my 2-day workshop on December 2-3 aimed at &lt;a href="http://codebetter.com/blogs/david_laribee/archive/2009/09/30/london-workshop-december-1-2.aspx"&gt;tools and techniques technical leads, folks interested in lean, standard work, etc.&lt;/a&gt; to try to get as many people there (it&amp;#39;s a group collaboration focused thing). I also want to get as much feedback as I can. &lt;b&gt;If you book before November 15th, the price is &amp;pound;550&lt;/b&gt;.

					&lt;/p&gt;
&lt;p&gt;Register &lt;a href="http://skillsmatter.com/course/design-architecture/towards-a-new-architect"&gt;at the SkillsMatter site&lt;/a&gt;. &lt;i&gt;Also feel free to contact me with any questions laribee/AT/gmail/DOT/com&lt;/i&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=411776" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/3xBeH-OvrRs" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/housekeeping/default.aspx">housekeeping</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/announcements/default.aspx">announcements</category><feedburner:origLink>http://codebetter.com/blogs/david_laribee/archive/2009/11/05/december-2-3-london-workshop-update.aspx</feedburner:origLink></item><item><title>The code is the truth, but it is not the whole truth</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/Dg151FqDah0/the-code-is-the-truth-but-it-is-not-the-whole-truth.aspx</link><pubDate>Wed, 04 Nov 2009 13:55:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:409582</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>11</slash:comments><description>&lt;p&gt;In a recent &lt;a target="_blank" href="http://www.informit.com/articles/article.aspx?p=1405569"&gt;interview from Grady Booch&lt;/a&gt;, co-creator of UML, Grady said:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;When Jim, Ivar, and I began our journey that became manifest in the UML, we never intended it to become a programming language. I think that there&amp;#39;s a fairly narrow domain for which model-driven development makes sense but that we should return to the roots of the UML, which was to be a language for visualizing, specifying, constructing, and documenting the artifacts of a software-intensive system&amp;mdash;in short, a graphical language to help reason about the design of a system as it unfolds. Most diagrams should be thrown away, but there are a few that should be preserved, and in all, one should only use a graphical notation for those things that cannot easily be reasoned about in code. As I&amp;#39;ve also often said, the code is the truth, but it is not the whole truth, and there are things such as rationale, cross-cutting concerns, and patterns that cannot easily be recovered or seen from code.... These are the things for which a graphical notation adds value, and any such notation should be used only if it has predictive power or reasoning power (meaning, you can ask questions about it).&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;I especially cannot agree more with:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;As I&amp;#39;ve also often said, the code is the truth, but it is not the whole truth, and there are things such as rationale, cross-cutting concerns, and patterns that cannot easily be recovered or seen from code....&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;The code is the truth, the code represents the whole asset of a software shop. But code base is a complex asset. Call them &lt;i&gt;patterns&lt;/i&gt;, &lt;i&gt;rationales&lt;/i&gt;, &lt;i&gt;intentions &lt;/i&gt;or &lt;i&gt;decisions&lt;/i&gt;, there are many valuable intellectual artifacts buried in a code base that cannot easily be recovered or seen from code.&lt;br /&gt;&lt;br /&gt;This is something I realized in 1997 while trying for the first time to demystify a C++ legacy code base. I was angry! What, in 1997, we, the professional developers, haven&amp;rsquo;t any tooling to visualize code? I remember also, in the architecture department next door, the architects were spending days doing diagrams that had nothing to do with what actually the code was. Putting hands in the code was something considered with low value. It was let for fresh graduated like me.&lt;br /&gt;&lt;br /&gt;Hopefully, the result of this period was the seed from where the idea of a tool such as &lt;a target="_blank" href="http://www.NDepend.com"&gt;NDepend &lt;/a&gt;came to me. Code Metrics, Treemap, Dependency Graph, Dependency Matrix, Instability vs Abstractness diagrams&amp;hellip; are natural artifacts to shed light on all these precious &lt;i&gt;intentions &lt;/i&gt;buried in the code. I especially like the word &lt;i&gt;intentions &lt;/i&gt;that was often used by &lt;a target="_blank" href="http://proagora.com/fr/experts/profiles/brunoboucard"&gt;Bruno Boucard&lt;/a&gt;, a former colleague I have great respect for. The idea of a language such as &lt;a target="_blank" href="http://www.ndepend.com/Features.aspx#CQL"&gt;Code Query Language&lt;/a&gt; came to me when &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/05/11/write-active-conventions-on-your-code-base.aspx"&gt;the need to make all these intentions concrete and verifiable&lt;/a&gt;, was clear.&lt;br /&gt;&lt;br /&gt;What we do is the result of what we did and who we met, no matter whether it was good or bad experiences. There is no such thing as: today, I gonna find an idea to make my own business. Good ideas come naturally along the way, through what, &lt;a target="_self" href="http://bookreviews.nabou.com/reviews/thealchemist.html"&gt;Paulo Coehlo&lt;/a&gt;, would call &lt;i&gt;signs&lt;/i&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=409582" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/Dg151FqDah0" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/11/04/the-code-is-the-truth-but-it-is-not-the-whole-truth.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part II</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/LTkhG4JcCgE/introduction-to-the-reactive-framework-part-ii.aspx</link><pubDate>Tue, 03 Nov 2009 05:28:37 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:409480</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>16</slash:comments><description>&lt;p&gt;In my &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;previous post&lt;/a&gt;, I talked a little bit about the parts of the Reactive Framework that are coming to the .NET 4 BCL as well as their implementation in F# as part of F# First Class Events.&amp;nbsp; This time, let’s come back to the Reactive Framework itself from which the IObservable&amp;lt;T&amp;gt; and IObserver&amp;lt;T&amp;gt; originated.&amp;nbsp; As you may remember, you can play with the bits of the Reactive Framework from the &lt;a href="http://www.codeplex.com/Silverlight"&gt;Silverlight 3 Toolkit&lt;/a&gt;.&amp;nbsp; &lt;/p&gt; &lt;p&gt;In the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/14/introducing-the-reactive-framework-part-i.aspx"&gt;first post in the series&lt;/a&gt;, I gave a basic introduction to the basic problems we have with asynchronous and reactive programming.&amp;nbsp; We covered some of the evolution of the .NET framework with asynchronous programming and where the Reactive Framework fits.&amp;nbsp; This time, I’ll pick up where we left off to talk about one of the important ideas behind the Reactive Framework, the duality of Enumerable and Observable.&lt;/p&gt; &lt;h2&gt;The Duality of Enumerable&amp;nbsp; &amp;amp; Observable&lt;/h2&gt; &lt;p&gt;During &lt;a href="http://channel9.msdn.com/shows/Going+Deep/E2E-Erik-Meijer-and-Wes-Dyer-Reactive-Framework-Rx-Under-the-Hood-1-of-2/"&gt;Erik’s appearances on Channel 9&lt;/a&gt;, an important, yet recurring theme was the &lt;a href="http://en.wikipedia.org/wiki/Duality_theory"&gt;mathematical duality&lt;/a&gt; of the Enumerable to the Observable.&amp;nbsp; What does that mean exactly though?&amp;nbsp; &lt;p&gt;We remember from our first post in the series where we talked about the pull (interactive) versus the push (reactive) model.&amp;nbsp; The pull model, represented by the iterator pattern of IEnumerable&amp;lt;T&amp;gt;/IEnumerator&amp;lt;T&amp;gt; states that we must explicitly call a method in order to get each item from our abstracted collection.&amp;nbsp; On the other hand, our push model, represented by the observable pattern of IObservable&amp;lt;T&amp;gt;/IObserver&amp;lt;T&amp;gt; states that we register an interest through a subscription and then items are subsequently handed to us from some abstracted collection.  &lt;p&gt;First, let’s look at the iterator pattern, which is a common interactive programming pattern that’s used quite frequently inside .NET.&amp;nbsp; This consists of two interfaces, the IEnumerable&amp;lt;T&amp;gt; and the IEnumerator&amp;lt;T&amp;gt;.&amp;nbsp; Listed below are some of the highlights of these two interfaces.&amp;nbsp; I cut down what they are to their essential bits. &lt;/p&gt; &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:78c27d02-2831-4956-a74e-25506fe8b415" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetEnumerator();
} 

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; : IDisposable
{
    T Current { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The IEnumerable&amp;lt;T&amp;gt; interface exposes a single method which returns an IEnumerator&amp;lt;T&amp;gt; to iterate some object.&amp;nbsp; The IEnumerator&amp;lt;T&amp;gt; gives us the ability to get the current item and determine whether there are more items to iterate.&amp;nbsp; &lt;p&gt;To think about basic asynchronous programming, we must consider a basic Gang of Four Pattern, the Observer Pattern.&amp;nbsp; This pattern describes where an object called an Observable, maintains a list of its dependent Observer classes and notifies them automatically of any state changes.&amp;nbsp; In Java, this should be rather familiar territory with the java.util.Observer class and the java.util.Observable interfaces.&amp;nbsp; In the Reactive Framework, this approach is no different, as we have an IObservable&amp;lt;T&amp;gt; interface which maintains a list of dependent IObserver&amp;lt;T&amp;gt; interfaces and notifies them automatically of any state changes.&amp;nbsp; Let’s look at the signatures below: &lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a22fc1cf-6ce7-41d9-b977-1c914f6dea63" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnCompleted();
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnError(Exception exception);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnNext(T value);
} 

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;What you may notice, and what you may have seen on some of Erik Meijer’s Channel 9 videos is that there is a duality between the Enumerable and Observable, given the signature of these interfaces.&amp;nbsp; To explain what I mean, let’s first look at the flow of the Enumerator: 
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_02D24D1F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_494F3D27.png" width="244" height="83" /&gt;&lt;/a&gt; 
&lt;p&gt;The GetEnumerator function takes no arguments and returns the IEnumerator&amp;lt;T&amp;gt; which has a no-argument function and returns the next item in the list through MoveNext and Current.&amp;nbsp; Now, let’s look at the inverse of it.&amp;nbsp; &lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_2FE739ED.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_21A8C0FD.png" width="244" height="123" /&gt;&lt;/a&gt; 
&lt;p&gt;The duality of the Enumerable solution is quite simple.&amp;nbsp; We have our observer on the left hand side which has an action function which takes a T argument and returns void.&amp;nbsp; The right hand side is the notification that is sent when something happens.&amp;nbsp; This is all managed by the Observable as it maintains a list of all observers so that it can notify them.&amp;nbsp; &lt;p&gt;To make this a little more concrete, let mash these two sets of interfaces together in order to dualize.&amp;nbsp; First, let’s start with the IEnumerator&amp;lt;T&amp;gt; interface.&amp;nbsp; &lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:31648a45-a756-4dad-876e-eb6beff8ac03" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; : IDisposable
{
    T Current { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; } &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; throws exception&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What we’ll notice is that we have two items the Current property with a get accessor and the MoveNext method which takes nothing and returns a boolean that indicates if it can move next.&amp;nbsp; You’ll also notice that our Current property also could throw an exception.&amp;nbsp; If we’re in the Java world, we’d explicitly mark this interface with throws Exception, but as C# doesn’t have that, this comment will do.&amp;nbsp; Now, let’s look to dualize this interface.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d43100c1-d9db-443e-a5e1-bbac9d2b880f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; SetCurrent(T value &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Exception ex);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; canMove);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At first glance, all we have to do is flip our Current property to have a setter.&amp;nbsp; Because the Current yielded a value or threw an exception, that’s the way we’ll model it for now.&amp;nbsp; Secondly, our MoveNext will now take a boolean to indicate whether we’re done.&amp;nbsp; Let’s refactor this a little bit more and break the SetCurrent into two distinct operations, one for yielding the next value, and one for throwing an exception.&amp;nbsp; &lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:644732de-0d0e-424d-8a79-f2e41f88677f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Yield(T value);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Throw(Exception ex);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; canMove);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now that we have that under control, let’s then move onto our MoveNext method.&amp;nbsp; As you’ll notice, we’ll continue to send in a true until we’re finished pushing values.&amp;nbsp; Instead of calling MoveNext repeatedly, let’s instead call this only once when we’re finished.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:67e3bcd0-71a6-4f8a-b2a7-38b4e6cfe217" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Yield(T value);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Throw(Exception ex);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Break();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What we then end up with is pretty much the signature of our IObserver&amp;lt;T&amp;gt;.&amp;nbsp; By renaming our Yield to OnNext, our Throw to OnException and Break to OnCompleted, we’re left with our original IObserver&amp;lt;T&amp;gt; interface.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64b72f33-4825-40a2-bbd3-9dc86c9d9445" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{   
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnNext(T value);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnError(Exception exception);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnCompleted();
} 
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What about the dual of IEnumerable&amp;lt;T&amp;gt;?&amp;nbsp; Let’s first look at what that interface is.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:de589912-3310-4d59-8687-6f512c6b547a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetEnumerator();
} &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This interface has a single method called GetEnumerator which returns an IEnumerator&amp;lt;T&amp;gt;.&amp;nbsp; How might we dualize this interface?&amp;nbsp; Well, we already know that the dual of the IEnumerator&amp;lt;T&amp;gt; is IObservable&amp;lt;T&amp;gt;, just as well, we need a way to attach interest in a given dualized Enumerator, so we’ll create a method called Attach.&amp;nbsp; Just as well, when we attach interest, we also need a way to detach our interest for a given observer.&amp;nbsp; That might look like the following.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b4d287df-8902-4317-bcd8-02d136ddbfa1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Attach(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Detach(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we look at the Java interfaces for Observer and Observable, these are pretty close in signature.&amp;nbsp; There ultimately is a problem with this scenario in terms of composition.&amp;nbsp; At each level, it needs to remember which things maps to which so that you could undo any of the observers if need be.&amp;nbsp; Not doing so could lead to space leaks and is generally a bad idea.&amp;nbsp; How could we solve this then?&amp;nbsp; Ultimately, we need a way to track our attachment once we call the Attach through some form of a return value.&amp;nbsp; At this point, the tracking is nothing more than needing the ability to detach our given Observer.&amp;nbsp; If you’ll notice in our IEnumerator&amp;lt;T&amp;gt; interface, it inherits the IDisposable interface which gets one thinking.&amp;nbsp; Could we instead, change our Attach method so that when we call it, we return an IDisposable instead which gives us the ability to track our subscription and be able to clean up after we’re done?&amp;nbsp; That’s exactly the approach that the IObservable&amp;lt;T&amp;gt; takes in the final version of the interface.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3753e03-8bdf-460e-a35d-0b9c26aef767" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now that we’ve proven the duality, why does it matter?&amp;nbsp; Because of this duality, the laws that apply to LINQ to Objects now can apply to LINQ to Observables (or Events).&amp;nbsp; And in fact, we can see that through the Observable class which has many of our standard LINQ combinators such as: 
&lt;ul&gt;
&lt;li&gt;Aggregate 
&lt;li&gt;First/FirstOrDefault 
&lt;li&gt;GroupBy 
&lt;li&gt;Join 
&lt;li&gt;Last/LastOrDefault 
&lt;li&gt;Select 
&lt;li&gt;SelectMany 
&lt;li&gt;Single/SingleOrDefault 
&lt;li&gt;Skip/SkipWhile 
&lt;li&gt;Take/TakeWhile 
&lt;li&gt;Where 
&lt;li&gt;Zip&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Because of this duality, we can do such simple things as fire the ProgressChanged event until the RunWorkerCompleted event happens.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b6433cf-f1f2-4c82-9d79-72eb72d9fbed" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; BackgroundWorker();

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; percentage &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; progress &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker.ProgressChangedEvent()
                           .Until(worker.RunWorkerCompletedEvent())
                 &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; progress.EventArgs.ProgressPercentage;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; subscription &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; percentage.Subscribe(
    p &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Console.WriteLine(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Percentage Complete: {0}&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, p));&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:38465fdf-0cb4-41e9-afd5-0468654547a4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s only scratching the surface of the power of the Reactive Framework.&amp;nbsp; We’ll go in depth in the next part of the series on the combinators.&amp;nbsp; We could go into the fact that the Enumerable solution is really just the lazy list monad and the Observable solution is really just the continuation monad, but that’s for another time as well.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As I’ve stated before, the LiveLabs Reactive Framework gives us the ability to harness reactive programming and treat events as the first class citizens they should have been using LINQ expressions and other standard LINQ combinators.&amp;nbsp; Still, there is much to cover in this series with the standard LINQ combinators, the monadic heritage of this solution and more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=409480" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/LTkhG4JcCgE" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx</feedburner:origLink></item><item><title>Should I use MEF with an IoC container? - Part 1</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/pBxpc37LwH8/should-i-use-mef-with-an-ioc-container.aspx</link><pubDate>Sun, 01 Nov 2009 06:36:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:408260</guid><dc:creator>Glenn Block</dc:creator><slash:comments>7</slash:comments><description>&lt;p&gt;In my post &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/08/16/should-i-use-mef-for-my-general-ioc-needs.aspx"&gt;&amp;quot;Should I use MEF for my IoC needs&amp;quot;&lt;/a&gt;&amp;nbsp;we took a look at the question of whether or not it is appropriate to use MEF instead of a traditional&amp;nbsp;IoC container. In this post we&amp;#39;ll discuss the question of whether or not you should use both an IoC container and MEF in the same application. &lt;/p&gt;
&lt;h2&gt;MEF and IoC container concerns&lt;/h2&gt;
&lt;p&gt;As we&amp;#39;ve mentioned in the past, MEF&amp;#39;s primary design in V1&amp;nbsp;is&amp;nbsp;around open-systems whose components (parts) are dynamically discovered at runtime. IoC containers on the other hand are primarily&amp;nbsp;designed for&amp;nbsp;decoupling&amp;nbsp;a closed&amp;nbsp;(fixed) set of application components in order to&amp;nbsp;improve the maintainability of the system by the engineering team. In other words each is addressing a specific set of concerns. So if that is the case then is it plausable to use both in an application? The answer is &lt;strong&gt;Yes&lt;/strong&gt;. Not only that, but we specifically designed MEF with the idea of integration with other containers in mind. Below will explore one of the key-scenarios for doing this.&lt;/p&gt;
&lt;h2&gt;Enabling third-party extensibility in an IoC container-dependent application.&amp;nbsp;&lt;/h2&gt;
&lt;p&gt;You have an existing customer management system that uses an IoC container such as Castle Windsor which you use in order to promote separation-of-concens and testability. The application leverages advanced Windsor facilities for integrating with NHibernate and for applying cross-cutting-concerns (AOP) such as logging, etc. The loosely coupled design of the system has proven to be greatly beneficial and has minimized the friction for developing and deploying the system through several releases. &lt;/p&gt;
&lt;p&gt;At some point requests start pouring in&amp;nbsp;from customers want to create their own extensions in order to customize the application in their specific environments. Additionally several ISVs express interest in providing third-party&amp;nbsp;value-adds such as telephony integration, and scheduling.&amp;nbsp;&amp;nbsp;You determine that adding a third-party&amp;nbsp;extensibility model to the application makes sense in order to address this opportunity. Through your&amp;nbsp;research you come across MEF and realize that it provides the infrastructure you need for extensibility.&amp;nbsp; However&amp;nbsp;it does so&amp;nbsp;in a manner that&amp;nbsp;overlaps with&amp;nbsp;many of the&amp;nbsp;functions that Windsor is providing.&amp;nbsp;What to do? Let&amp;#39;s explore various options.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;a. Migrate the existing code to use MEF and remove&amp;nbsp;the IoC container&amp;nbsp;completely.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One option is to consider ripping out Castle and just using MEF. Technically this&amp;nbsp;sounds reasonable, however after investigation&amp;nbsp;it is not feasible. There are literally thousands&amp;nbsp;of components that are currently registered in the Windsor container.&amp;nbsp;Moving these to MEF&amp;nbsp;involves modfying all of these existing classes&amp;nbsp;in order to make them discoverable by MEF, not to mention refactoring all the places in the application where the container is accessed.&amp;nbsp; Because the changes are so widespread, the likelihood of introducing&amp;nbsp; breakages / bugs in the system is very high. Additionally there are the various Castle facilities such as Dynamic Proxy, and NHibernate integration&amp;nbsp;which MEF does not yet support, which will have to authored by the team. The cost / risk of such changes is too prohibitive, not to mention that it seems&amp;nbsp;like it should be unnecessary.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;b. Implement a custom&amp;nbsp;extensibility mechanism&amp;nbsp;built on top of the&amp;nbsp;IoC container?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Another option is to author a home-grown solution. This can be a viable option depending on the needs, and the willingness to author your own solution. For simple cases you can easily facade an extensibility mechanism on top of an IoC. Once you start getting into more advanced features like lazy instantiation, metadata, recomposition and stable composition, you are in for quite a bit of work. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;c. Have&amp;nbsp;both&amp;nbsp;the IoC&amp;nbsp;and MEF both present in the same application?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The final option I want to discuss (and the one I recommend for these situations) is to use both. Use the IoC container for addressing internal application decoupling needs, and use MEF for third-party extensibiltiy needs, that is the IoC container for IoCish things and the MEF container for MEFfish things :-)&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This path is appealing for several reasons. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It works with existing codebases whether they be applications or frameworks. it does not require any significant rewrite.&lt;/li&gt;
&lt;li&gt;It allows leveraging the strengths of each container for the things it does best. For example leveraging MEF&amp;#39;s metadata model for discriminating between different extensions vs the dynamic proxy support in Castle. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;However, there are several caveats of this approach.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It&amp;nbsp;raises questions such as: 
&lt;ul&gt;
&lt;li&gt;Which component (part) should live in which container? &lt;/li&gt;
&lt;li&gt;Who owns the lifetime?&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Can a MEF part access services coming from the IoC?&amp;nbsp; How?&lt;/li&gt;
&lt;li&gt;Can the IoC access exports coming from MEF?&amp;nbsp;&amp;nbsp;How?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;It increases complexity in that now there are components living in mulitple places. &lt;/li&gt;
&lt;li&gt;It increases the potential points of failuire within the application. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of these issues are managable. In my next post we&amp;#39;ll see how.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=408260" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/pBxpc37LwH8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/IOC/default.aspx">IOC</category><category domain="http://codebetter.com/blogs/glenn.block/archive/tags/MEF/default.aspx">MEF</category><feedburner:origLink>http://codebetter.com/blogs/glenn.block/archive/2009/10/31/should-i-use-mef-with-an-ioc-container.aspx</feedburner:origLink></item></channel></rss>
