<?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>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>0</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>10</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>7</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>4</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>8</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>14</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><item><title>Getting Your Groove Off</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/LX6W-b-cZI8/getting-your-groove-off.aspx</link><pubDate>Thu, 29 Oct 2009 21:26:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:403450</guid><dc:creator>hdierking</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;Until recently, the only knowledge I had of Groove was that it was some kind of collaboration product that we got as a result of bringing in &lt;a href="http://www.microsoft.com/presspass/exec/ozzie/?tab=biography"&gt;Ray Ozzie&lt;/a&gt;.&amp;nbsp; Then I got a new manager who insisted that our team standardize on Groove for the team document repository - so I got setup on Groove and went on with my life.&amp;nbsp; In general, there&amp;rsquo;s nothing I found terribly impressive about it, but it wasn&amp;rsquo;t really getting in my way so no reason to get too upset&amp;hellip;&lt;/p&gt;
&lt;p&gt;Until my &lt;a href="http://tortoisesvn.net/"&gt;Tortoise SVN&lt;/a&gt; icon overlays stopped working.&lt;/p&gt;
&lt;p&gt;I ended up spending quite a bit of time this morning trying to figure out exactly why they weren&amp;rsquo;t working.&amp;nbsp; It was odd &amp;ndash; I had no problems on my home machine &amp;ndash; but on both of my work machines, same result &amp;ndash; no TSVN icons.&amp;nbsp; Finally, &lt;a href="http://twitter.com/pilotbob"&gt;@pilotbob&lt;/a&gt; pointed me to &lt;a href="http://msdn.microsoft.com/en-us/library/cc144123%28VS.85%29.aspx"&gt;this article&lt;/a&gt; which set off a chain of recollections &amp;ndash; most notably that the major difference between my work machines and my home machine had to do with the order that I installed stuff.&amp;nbsp; On my home machine, I installed Office after I got my dev environment setup.&amp;nbsp; However, on my work machine, Office was installed as a part of the default OS install image - and Groove was included in that installation.&lt;/p&gt;
&lt;p&gt;So why is this important?&amp;nbsp; Because if you remember from the article, Windows supports only 15 overlay handler slots &amp;ndash; and installing Groove puts 5 handlers right there on top of my TSVN handlers &amp;ndash; the net result being that my TSVN icon overlays don&amp;rsquo;t load.&lt;/p&gt;
&lt;p&gt;To fix, I simply went into the registry, deleted all of the Groove overlay handlers from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/reg_5F00_5DBA9B31.png"&gt;&lt;img height="464" width="644" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/reg_5F00_thumb_5F00_1F059DCB.png" alt="reg" border="0" title="reg" 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;Then rebooted.&amp;nbsp; Voila!&amp;nbsp; TSVN icon overlays come right back up!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=403450" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/LX6W-b-cZI8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/howard.dierking/archive/tags/TortoiseSVN/default.aspx">TortoiseSVN</category><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/10/29/getting-your-groove-off.aspx</feedburner:origLink></item><item><title>Low-Technology</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/QLCKhPrYHO8/low_2D00_technology.aspx</link><pubDate>Thu, 29 Oct 2009 18:55:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:403429</guid><dc:creator>Dave Laribee</dc:creator><slash:comments>13</slash:comments><description>&lt;p&gt;I&amp;#39;m completely obsessed with the idea of &lt;a href="http://en.wikipedia.org/wiki/Low-technology"&gt;low-technology&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;The term low-technology is a description of those crafts and tools whose inception (typically) predates the Industrial Revolution.&lt;/blockquote&gt;
&lt;blockquote&gt;A test for low-technology may be that it can be practiced or fabricated with a minimum of Capital investment by an individual or small group of individuals; and that the knowledge of the practice can be completely comprehended by a single individual, free from increasing specialization and compartmentalization.&lt;/blockquote&gt;
&lt;p&gt;An excellent overview of some of the cutting edge efforts in low-technologies can be seen in this &lt;a href="http://www.ted.com/talks/amy_smith_shares_simple_lifesaving_design.html"&gt;TED video with low-technologist Amy Smith&lt;/a&gt;. I completely agree with when says, &amp;quot;not all inventions need to be grandiose, complex things... sometimes they can be simple and smart ideas that just help a lot of people.&amp;quot;&lt;/p&gt;
&lt;p&gt;The second block quote is where I&amp;#39;m coming from. What are those tools in software development that add lots of value without minimal getting started overhead. Pair-programming seems like an excellent example of low technology. Emergent design, test-driven development and value-driven approaches like XP are yet more.&lt;/p&gt;
&lt;p&gt;It seems, to me anyway, that these are the things that&amp;#39;ll yield the most benefits. Both vendor-supplied tooling and trending topics we hold near and dear, like &lt;a href="http://domaindrivendesign.org/"&gt;Domain-Driven Design&lt;/a&gt; and &lt;a href="http://www.martinfowler.com/bliki/DomainSpecificLanguage.html"&gt;DSLs&lt;/a&gt; and this fluent madness in general, strike me as more &lt;i&gt;high-technology&lt;/i&gt; approaches.&lt;/p&gt;
&lt;p&gt;There&amp;#39;s nothing wrong with high-technology, of course, but my instinct is that the risk-reward equation for most teams usually favors low-technology. We have, after all, limited bandwidth and attention, so my vote will ever go to what I see as big and easy wins: stuff focused on beefing up the signal of our communication as a team and community against the noise we might get as a software fashion victim.&lt;/p&gt;
&lt;p&gt;Anyway, this is a key topic I&amp;#39;ll be centering on over the next little while and thought y&amp;#39;all might find this way of looking at our kit, our standard tradecraft at least a little bit interesting.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=403429" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/QLCKhPrYHO8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/XP/default.aspx">XP</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/opinion/default.aspx">opinion</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/coaching/default.aspx">coaching</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/practices/default.aspx">practices</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/low-technology/default.aspx">low-technology</category><feedburner:origLink>http://codebetter.com/blogs/david_laribee/archive/2009/10/29/low_2D00_technology.aspx</feedburner:origLink></item><item><title>Eschewing Date Types in our Database</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/apbWKuyKBm8/eschewing-date-types-in-our-database.aspx</link><pubDate>Thu, 29 Oct 2009 13:41:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:402818</guid><dc:creator>karl</dc:creator><slash:comments>17</slash:comments><description>&lt;p&gt;About a month ago, I found myself transforming data for the reporting portion of our application. Like many reporting systems, ours works off of date ranges, with statistics grouped together by the day (and a few by the hour). An early prototype made heavy use of TSQL Date functions (like DateAdd and DatePart). However, I wasn&amp;#39;t happy with this solution, my transformations were unnecessarily coupled to SQL Server. I mean, database date functions all do pretty primitive date arithmetic&amp;#39;s, surely we can come up with a way to do that ourselves?&lt;/p&gt;
&lt;p&gt;I decided to borrow the strategy long-used by the Unix and PHP community (or JavaScript or doubtless countless other platforms/languages) - to store dates as seconds relative to a starting point. Unix Epoch is January 1st, 1970. If you store seconds since then in an unsigned 32 bit integer, you can store up to 136 years. If you only need minute-resolution, you can store over 8 100 years. If you only need day-resolution, then you can store a bit under 12 million years.&lt;/p&gt;
&lt;p&gt;Best of all, you can do straight up arithmetic&amp;#39;s against these fields to manipulate the dates. If you want to group hits per user for a day, you can do:&lt;/p&gt;
&lt;pre class="sql" name="code"&gt;insert into DailyHits (UserId, DaysSince, Count)
select UserId, SecondsSince/86400, count(*) 
   from Hits 
   group by UserId, SecondsSince/86400
--86400 is the number of seconds in a day

insert into HourlyHits (UserId, HoursSince, Count)
select UserId, SecondsSince/3600, count(*) 
   from Hits 
   group by UserId, SecondsSince/3600 
--3600 is the number of seconds in a hour&lt;/pre&gt;
&lt;p&gt;You can also manipulate relative times in C#, to apply as parameters:&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;public static readonly DateTime Epoch = new DateTime(1970, 1, 1);
public IList GetDailyHits(DateTime from, DateTime to)
{

   command.CommandText = &amp;quot;select * from DailyHits where DaysSince &amp;gt; @From and DaysSince &amp;lt; @To&amp;quot;;
   command.Parameters.Add(&amp;quot;@From&amp;quot;, from.Substract(Epoch).TotalDays);
   command.Parameters.Add(&amp;quot;@To&amp;quot;, to.Substract(Epoch).TotalDays);
   ...
}&lt;/pre&gt;
&lt;p&gt;If you are using NHibernate, you can use the IUserType interface to help map DateTimes to their relative counterparts (I think).&lt;/p&gt;
&lt;p&gt;The examples above are simplistic, but they do work very well and have a number of benefits - the most significant being great independence from the underlying database.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=402818" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/apbWKuyKBm8" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/karlseguin/archive/2009/10/29/eschewing-date-types-in-our-database.aspx</feedburner:origLink></item><item><title>Astoria, SSIS Adapters and BDD – Take 1</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/mVYHfxnN23s/astoria-ssis-adapters-and-bdd-take-1.aspx</link><pubDate>Wed, 28 Oct 2009 16:00:17 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:400446</guid><dc:creator>hdierking</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;Yea I know – 3 things you never thought you would hear mentioned together.&amp;#160; And to be really honest, I didn’t start out trying to build this in a BDD fashion – and since I’m totally new to the disciplines of BDD, I may not have been all that successful.&amp;#160; But hey, it’s all about the journey, and so I wanted to at least go ahead and start posting about the work I’ve been doing here, partly to share and partly to get your input ways to improve my design/specs.&lt;/p&gt;  &lt;p&gt;So here was the initial goal – I was studying ADO.NET Data Services (previously known as Astoria) when I remembered &lt;a href="http://msdn.microsoft.com/en-us/library/bb245659.aspx"&gt;this article that I once read&lt;/a&gt; in the Architecture Journal about using RSS as a technique for surfacing data to load into an enterprise data warehouse.&amp;#160; That led me to think that it would be a cool opportunity to simultaneously learn Astoria and feed my BI sweet tooth by writing a SSIS data source that could consume an Astoria endpoint.&amp;#160; Given the fact that &lt;a href="https://blogs.msdn.com/astoriateam/archive/2009/10/21/using-data-services-over-sharepoint-2010-part-1-getting-started.aspx"&gt;more and more systems are exposing data using syndication&lt;/a&gt;, it seemed like creating an SSIS data source was something that could be helpful beyond my simply wanting to play with the technology.&lt;/p&gt;  &lt;p&gt;Wait - ADO.NET Data Services what??&lt;/p&gt;  &lt;p&gt;Just in case you weren’t 100% sure what ADO.NET Data Services is, it’s technology to expose data and enable operations on data via RESTful operations.&amp;#160; You can find out much more about it &lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;here&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/astoriateam/"&gt;here&lt;/a&gt;, but for the purposes of this exercise, here are the 3 things you need to know.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;It uses all standard HTTP operations – which means that for getting data, you simply format a HTTP GET request for the service URL plus additional path information and optional querystring arguments to tell the framework how to shape the data. &lt;/li&gt;    &lt;li&gt;While there’s not a dependency on the Entity Framework, there is a dependency on the Entity Data Model in that metadata is exposed as &lt;a href="http://msdn.microsoft.com/en-us/library/bb399292.aspx"&gt;CSDL&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Though you have a couple formatting options when getting data, the primary one (and the one used by the data source component) is to get the data as an ATOM feed.&amp;#160; The data source component can then project the XML into the appropriate output buffer (or buffers as you’ll see later). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Wait – SSIS what??&lt;/p&gt;  &lt;p&gt;Sql Server Integration Services is Microsoft’s ETL tool that ships with SQL Server.&amp;#160; You can start learning more about it &lt;a href="http://msdn.microsoft.com/en-us/library/ms141026.aspx"&gt;here&lt;/a&gt;, but for the purpose of this exercise, here are the 3 things you need to know.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The unit of execution in SSIS is the package.&amp;#160; This is really similar nomenclature to DTS because, not surprisingly, SSIS evolved out of DTS. &lt;/li&gt;    &lt;li&gt;Every package contains 2 kinds of logic: 1 “Control Flow” and zero-to-many “Data Flows”.&amp;#160; Control flow is meant for larger operations like copying files or running some kind of database maintenance operation (and while interesting, it’s not something I care about for this exercise).&amp;#160; The real meat of SSIS (IMHO) happens within a data flow – this is where data is extracted from a source, massaged, and put into a destination.&amp;#160; Data flows maintain the entire pipeline of operations between source and destination via in memory buffers. &lt;/li&gt;    &lt;li&gt;Data flows use data source components to extract data from the source and add it to the appropriate buffers in the pipeline.&amp;#160; Data source components are also used to provide metadata to the SSIS design-time experience so that package designers can use it to clean up and massage the data using transformation components (** for those experienced SSIS folks, it’s worth noting that there is an XML data source component that is a part of the product, and since ADO.NET Data Services returns data as an ATOM feed, is is absolutely possible to consume it directly.&amp;#160; However, in order to take this approach, you will need to generate a XSD schema and associate it with the component at design time for the metadata – and every time you change the shape of your data services query, you will have to re-generate and re-associate a new schema.&amp;#160; This was one of the primary motivators for me to create a custom data source). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So then, the initial goals were as follows -&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Create an SSIS data source component that could consume an ADO.NET Data Services endpoint &lt;/li&gt;    &lt;li&gt;Ensure that the component participated in both the design time and runtime experiences &lt;/li&gt;    &lt;li&gt;Ensure that the component was able to handle projections of ADO.NET Data services queries that took advantage of the $&lt;a href="http://msdn.microsoft.com/en-us/library/cc668798.aspx"&gt;expand clause&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As I got into the development I was pretty quickly able to accomplish the first 2 goals.&amp;#160; For example, given the following query configuration..&lt;/p&gt;  &lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/ConfigDialog_5F00_76A43385.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ConfigDialog" border="0" alt="ConfigDialog" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/ConfigDialog_5F00_thumb_5F00_05F79595.png" width="244" height="230" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I was able to get the data shape using ADO.NET Data Service’s &lt;a href="http://blogs.thinktecture.com/cweyer/archive/2007/12/11/415061.aspx"&gt;$metadata&lt;/a&gt; option and expose it back to the designer..&lt;/p&gt;  &lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/ColumnMappings_5F00_395358F1.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ColumnMappings" border="0" alt="ColumnMappings" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/ColumnMappings_5F00_thumb_5F00_36CA2733.png" width="244" height="230" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I was even able to run the package for a simple data services query and pipe the results to a flat file..&lt;/p&gt;  &lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/simpleExample_5F00_47622221.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="simpleExample" border="0" alt="simpleExample" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howard.dierking/simpleExample_5F00_thumb_5F00_740ADBFA.png" width="170" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;However, when I started implementing that 3rd goal of managing projections, I quickly realized my quick and dirty project need more thought in its design.&amp;#160; This drove me to add a few more goals.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Project should separate the core logic of the component from the SSIS infrastructure – which is heavily reliant on inheritance &lt;/li&gt;    &lt;li&gt;Core component logic should be independently testable without requiring a running data services endpoint &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Separating the logic out into a different assembly wasn’t really all that painful – just a few keystrokes of Resharper, a couple file moves, a reference added, and I was good to go.&amp;#160; However, because this started as a quick and dirty project (I should have known better), I hadn’t designed for data source independence and as such had some larger refactorings to do.&amp;#160; And in that process (** keep in mind that this started as an remains primarily a learning exercise for me – I would NEVER recommend taking this kind of “while I’m at it approach” on a real project), I decided that my tests weren’t really where I wanted them and that I had always wanted to get my hands dirty with BDD anyway, so why not?&amp;#160; It’s just one more goal.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Tests should be written as specifications &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I’ve read from some that BDD is really meant more for testing larger behaviors in business applications.&amp;#160; However, it feels pretty good (though initially uncomfortable) at the grain at which I’m using it.&amp;#160; For example..&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;public class when_products_resource_is_queried : with_northwind_CSDL
{
    Establish context = () =&amp;gt; queryBuilder.ResourcePath = &amp;quot;Products&amp;quot;;

    Because of = () =&amp;gt; query = queryBuilder.GetQuery();

    It should_have_8_properties = () =&amp;gt; 
        query.DataShape.First().Properties.Count.ShouldEqual(8);

    It should_have_a_single_resource = () =&amp;gt; 
        query.DataShape.Count().ShouldEqual(1);

    It should_have_1_key_property = () =&amp;gt; 
        query.DataShape.First().Keys.Count.ShouldEqual(1);

    It should_have_correct_query_string = () =&amp;gt;
        query.ToString().ShouldEqual(string.Format(
            &amp;quot;{0}/Products&amp;quot;, Constants.SERVICE_URI));
}&lt;/pre&gt;

&lt;p&gt;Ok – so those were the goals as they evolved. Going forward, my plan is to dig into the logic design and specifications, then talk specifically about projections, then finally talk about how this stuff works with SSIS (and how I would like to see the SSIS API simplified).&amp;#160; Sorry to not get into all that tonight, but as my friend &lt;a href="http://blogs.msdn.com/gblock/archive/2007/08/29/improving-blogging-efficiency.aspx"&gt;Glenn has advised me countless times&lt;/a&gt;, if you wait to blog until everything is nice and tidy and completed, you may never blog again.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=400446" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/mVYHfxnN23s" 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><feedburner:origLink>http://codebetter.com/blogs/howard.dierking/archive/2009/10/28/astoria-ssis-adapters-and-bdd-take-1.aspx</feedburner:origLink></item><item><title>The Most Productive Programming Language</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/vb2_6KdSukk/the-most-productive-programming-language.aspx</link><pubDate>Wed, 28 Oct 2009 00:08:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:398206</guid><dc:creator>Dave Laribee</dc:creator><slash:comments>9</slash:comments><description>&lt;p&gt;The most productive and useful programming language you know, by a long shot, is English or Hindi or Portuguese or Spanish or French or whatever human language your team uses. Good communication skills are way more valuable to you and your customers than picking up &lt;a href="http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X"&gt;a new language every year&lt;/a&gt;. That is, while there is value in the item on
the right, I value the item on the left more. &lt;a href="http://agilemanifesto.org/"&gt;Sound familiar?&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Developers that name things well and program with a respect for human
languages create more maintainable and easier to follow code. Being able to communicate with a customer, analyst and/or product owner can save you hour upon hour of effort and keep your solution simple. No programming language no matter how syntactically minimal or internal DSL friendly can ever come close to this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=398206" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/vb2_6KdSukk" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/Language/default.aspx">Language</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/coaching/default.aspx">coaching</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/craftsmanship/default.aspx">craftsmanship</category><category domain="http://codebetter.com/blogs/david_laribee/archive/tags/basics/default.aspx">basics</category><feedburner:origLink>http://codebetter.com/blogs/david_laribee/archive/2009/10/27/the-most-productive-programming-language.aspx</feedburner:origLink></item><item><title>Hierarchical Namespace Component</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/2vwNcU8ErHo/hierarchical-namespace-component.aspx</link><pubDate>Mon, 26 Oct 2009 14:49:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:398024</guid><dc:creator>Patrick Smacchia</dc:creator><slash:comments>11</slash:comments><description>&lt;p&gt;Modeling components of a code base with namespaces is a much better idea than modeling them with assemblies. I wrote about the &lt;i&gt;namespaces vs. assemblies&lt;/i&gt; debate in the past (&lt;a target="_blank" href="http://www.theserverside.net/tt/articles/showarticle.tss?id=ControllingDependencies"&gt;Control components dependencies to gain clear architecture&lt;/a&gt;). The fact is that namespace is a logical artifact while assembly is a physical artifact. Of course, creating assemblies to partition parts of the code that at runtime physically don&amp;rsquo;t live within the same AppDomain is a good thing. I explained that in &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2008/12/08/advices-on-partitioning-code-through-net-assemblies.aspx"&gt;Advices on partitioning code through .NET assemblies&lt;/a&gt;. Unfortunately, the vast majority of .NET teams use assemblies instead of namespaces to partition logically their code. They end up with hundreds of Visual Studio projects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Hundreds of VS projects organized through dozens of VS solutions is something hard to manage.&lt;/li&gt;
&lt;li&gt;These hundreds of projects takes a lot of extra time to be compiled (because of the repetitive tier assemblies parsing).&amp;nbsp; By extra time I mean that the compilation takes minutes instead of seconds!&lt;/li&gt;
&lt;li&gt;And finally, loading all these assemblies at runtime slow down consequently the startup time of applications. Indeed, doing so multiply the unit overhead that the CLR needs to load an assembly. The overhead is mainly due to CAS security checks, cryptographic signing check, and IO file read.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In order to model logical components, the logical nature of namespace is a preemptive advantage over the physical nature of assembly. However namespace have a second awesome characteristic: they can be declared hierarchically. &lt;b&gt;So namespaces can be use to modelize a hierarchy of components. &lt;/b&gt;Today I would like to ramble on some consequences and caveat of organizing hierarchically components.&lt;/p&gt;
&lt;h2&gt;Theory: Tree vs. Flat view&lt;/h2&gt;
&lt;p&gt;This hierarchical way of componentizing code is something that we took account early in the &lt;a href="http://www.NDepend.com"&gt;NDepend&lt;/a&gt; development. At any time, the user can choose to display namespaces through tree (i.e through a hierarchy)&amp;hellip;&lt;br /&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/TreeOption.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/TreeOption.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&amp;hellip;or to display flat namespaces:&lt;br /&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/FlatOption.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/FlatOption.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Personally I prefer the tree option. After all, why do developers naturally partition the code? Because of the need to organize code and API through some smaller chunks. Smaller chunks are naturally easier to develop, understand, maintain, test and debug. This is the famous &lt;i&gt;divide and conquer tenet&lt;/i&gt;. &lt;br /&gt;&amp;nbsp;&lt;br /&gt;But when the code base is consequently growing developers are facing a dilemma:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Smaller components are easier to be managed one by one. But this implies to deal with hundreds of components. As a consequence the big picture becomes messy.&lt;/li&gt;
&lt;li&gt;Keeping a small number of components force to have large components. While the big picture remain understandable, larger components are harder to manage.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is why developers apply recursively the divide and conquer idea. As a result, they obtain a hierarchical organization of code.&lt;/p&gt;
&lt;h2&gt;Practice: Tree vs. Flat view&lt;/h2&gt;
&lt;p&gt;These 2 dependency graphs illustrate well the point. The first one is made of the 52 namespaces declared in the mscorlib (this is the flat view). &lt;br /&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/FlatOptionGraph.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/FlatOptionGraph.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The second one is made of the 16 root namespaces of the DLL mscorlib (this is the root tree view). The root tree view present less information than the flat view, but at least it is readable. &lt;br /&gt;&lt;img src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/TreeOptionGraph.png" style="max-width:600px;border:0;" border="0" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Moreover the root tree view is a good start to dig into the code and browse nested namespaces inter-dependency:&lt;br /&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/Graph2Graph.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/Graph2Graph.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The same phenomenon appears with the Dependency Matrix. The root tree view (first matrix) is a better way to browse the code than the flat view (second matrix).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/TreeOptionMatrix.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/TreeOptionMatrix.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/FlatOptionMatrix.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/FlatOptionMatrix.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/TreeOptionMatrix.png"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Component Hierarchy and Dependency Cycle&lt;/h2&gt;
&lt;p&gt;Avoiding cycles between components is an essential point to care for, in order to keep a code base maintainable. I detailed that in the post: &lt;a target="_blank" href="http://codebetter.com/blogs/patricksmacchia/archive/2009/07/29/maintainability-learnability-component-layering.aspx"&gt;Maintainability, Learnability, Component, Layering&lt;/a&gt;. When components are hierarchically organized, some &lt;i&gt;non-trivial &lt;/i&gt;cycles can appear. Let&amp;#39;s look at a small example.&lt;br /&gt;&amp;nbsp;&lt;br /&gt;If we look at the dependency between the 3 following namespaces we won&amp;rsquo;t find any cycles:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;namespace ParentA.Child1 {&lt;br /&gt;   public class ClassA1 { private ParentB.ClassB m_Field; }&lt;br /&gt;}&lt;br /&gt;namespace ParentA.Child2 {&lt;br /&gt;   public class ClassA2 { }&lt;br /&gt;}&lt;br /&gt;namespace ParentB {&lt;br /&gt;   public class ClassB { private ParentA.Child2.ClassA2 m_Field; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;


&lt;br /&gt;There is no cycle, this is clear from the flat dependency view, &lt;br /&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/RootCycle.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/RootCycle.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is what I called a &lt;i&gt;non-trivial&lt;/i&gt; cycle because a cycle exists while there are no cycles between namespaces. The fact is that &lt;b&gt;RootA.*&lt;/b&gt; is not considered as a namespace because it doesn&amp;rsquo;t contain directly any class. In our internal NDepend terminology we named &lt;b&gt;RootA.*&lt;/b&gt; a namespace container. Technically your code is entangled and contain a cycle even if physically there is no cycle between namespaces!&lt;br /&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/RootCycle2.png"&gt;&lt;img src="http://codebetter.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/patricksmacchia.NamespaceHierarchy/RootCycle2.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hierarchical components constitute a powerful way to partition code but be aware that some non-trivial entangling cases might appear. In a next post, I&amp;rsquo;ll expose a cool way to use hierarchical componentization effectively in the real world.&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=398024" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/2vwNcU8ErHo" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/patricksmacchia/archive/2009/10/26/hierarchical-namespace-component.aspx</feedburner:origLink></item><item><title>Something you can (not) do with your (old) MSDN disks</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/jYPvSYxdXns/something-you-can-not-do-with-your-old-msdn-disks.aspx</link><pubDate>Mon, 26 Oct 2009 12:52:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:398019</guid><dc:creator>pvanooijen</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;Time to renew my MSDN subscription. I don&amp;rsquo;t know about you but I&amp;rsquo;m still very pleased with the MSDN program.&lt;/p&gt;
&lt;p&gt;In the old pre DVD days it used to be quite a problem what to do with the enormous piles of disks you received over and over again. Especially those things you never use. Anyone interested in a Finish version of Exchange server ? A Greek version of Windows 98 ? Since DVD times the piles has gotten a little smaller, but still. Googling around what to do with all these disks pops up an &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2004/05/31/14832.aspx"&gt;old rant of mine&lt;/a&gt; on this.&lt;/p&gt;
&lt;p&gt;Being a biologist with a huge interest in aquarium fish (see my &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2009/05/20/a-first-glimpse-of-virtualization-in-windows-7.aspx"&gt;W7 betta rant&lt;/a&gt;) and an &amp;ldquo;old goat-wool-socks&amp;rdquo; environmental concern I thought I had a good use. Digital disk are very reflective, using them as a reflector for the light in my aquarium would give the disks a new life and would increase the effectiveness of fish tank lighting. So I tried.&lt;/p&gt;
&lt;p&gt;This is what the result looked like in less than a year.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/OldMSDNdisks_5F00_7F8CB94B.jpg"&gt;&lt;img height="476" width="754" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/OldMSDNdisks_5F00_thumb_5F00_2F675B0D.jpg" alt="Old MSDN disks" border="0" title="Old MSDN disks" 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;Moist has crept between the reflective (aluminum) layer and the (polycarbonate) disk itself and completely oxidized the metal. Resulting in a surface which reflects far less than the ground surface itself. Lesson learned: this does not work.&lt;/p&gt;
&lt;p&gt;What you can do today with your disks is what I already asked for in &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2004/05/31/14832.aspx"&gt;my original rant&lt;/a&gt;: don&amp;rsquo;t get them. MSDN subscriptions now offer a download only option. You are free to download what you need (and what&amp;rsquo;s in your license). For many a product this was what I did already, downloading got me the product long before the disk finally arrived by mail. So if you want to stop wasting resources too: do switch to this subscription type. The only wish I have left is to save a little on money. Not producing and distributing the disks saves MS in the costs. I would like a small share of that in the form of a reduction on my MSDN fee. Fair deal ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=398019" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/jYPvSYxdXns" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/peter.van.ooijen/archive/2009/10/26/something-you-can-not-do-with-your-old-msdn-disks.aspx</feedburner:origLink></item><item><title>Postgresql - Day 2</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/2ElvldQCHAg/postgresql-day-2.aspx</link><pubDate>Sun, 25 Oct 2009 01:40:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:394963</guid><dc:creator>karl</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;My migration from SQL Server to PostgreSQL for an upcoming project continues. On &lt;a href="http://codebetter.com/blogs/karlseguin/archive/2009/10/22/migrating-to-postgresql-with-my-friend-nhibernate.aspx"&gt;day 1&lt;/a&gt; I managed to get the application up and running quickly, thanks to NHibernate. &lt;a href="http://ayende.com/Blog/archive/2009/10/22/database-independence-with-nhibernate.aspx"&gt;Ayende was kind enough&lt;/a&gt; to post some advice to minimize the already-few problems I ran into (I&amp;#39;ve since implemented the &lt;code&gt;ISQLExceptionConverter&lt;/code&gt; and am really happy about it).&lt;/p&gt;
&lt;p&gt;Today I had a bigger challenge - making the OLTP to OLAP transformation script work. I knew this would be more difficult because I&amp;#39;m using straight up SQL (instead of HQL), as well as a couple instances of &lt;code&gt;SQLBulkCopy&lt;/code&gt;. So far, most of the transformation is done in sql - there&amp;#39;s very little data being pulled down and processed by in code. Luckily, I have a huge and thorough integration test, which populates the database with a couple days worth of data, executes the transformation script in steps (as it would be in real life), and then validates the results. Essentially my test figures out the results in C# (by looping over the entities and aggregating the results every which way) and compares it to what the transformation scripts does. &lt;/p&gt;
&lt;p&gt;Aside from a couple syntactical differences, such as having used &lt;code&gt;[ ]&lt;/code&gt; to escape the count column, 1 column ambiguity that PostgreSQL couldn&amp;#39;t resolve (weird), and having used &lt;code&gt;insert {table}&lt;/code&gt; instead of &lt;code&gt;insert into {table}&lt;/code&gt;, I only ran into 2 major problem.&lt;/p&gt;
&lt;p&gt;The first was my usage of &lt;code&gt;SqlBulkCopy&lt;/code&gt; which is tied to SQL Server&amp;#39;s bcp utility. I was only using it to bulk-load the data for my integration test, but it did make a huge difference, and I wanted to keep the same performance if possible. It turns out that PostgreSQL supports a &lt;code&gt;COPY from STDIN&lt;/code&gt; command, which the &lt;a href="http://pgfoundry.org/projects/npgsql/"&gt;.NET Npgsql library&lt;/a&gt; exposes as an &lt;code&gt;NpgsqlCopyIn&lt;/code&gt; class. Although it didn&amp;#39;t perform quite as well (it still rocked), I much prefer the implementation. You essentially write a CSV to a stream - much nicer than having to transform my objects to datatables. Here&amp;#39;s the reusable code that I used:&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;public static void BulkCopy&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; items, Func&amp;lt;T, object[]&amp;gt; callback, string tableName, string columns)
{
    var sql = string.Format(&amp;quot;copy {0} {1} from stdin with delimiter &amp;#39;|&amp;#39;&amp;quot;, tableName, columns &amp;gt;&amp;gt; string.Empty);
    using (var connection = new NpgsqlConnection(_connectionString))
    {
        connection.Open();                
        using (var command = new NpgsqlCommand(sql, connection))                
        {                    
            var copy = new NpgsqlCopyIn(command, connection);                    
            try
            {
                copy.Start();                        
                foreach (var item in items)
                {
                    var data = SerializeData(callback(item));
                    var raw = Encoding.UTF8.GetBytes(string.Concat(data, &amp;quot;\n&amp;quot;));
                    copy.CopyStream.Write(raw, 0, raw.Length);
                }
            }
            catch (Exception)
            {
                copy.Cancel(&amp;quot;Undo copy&amp;quot;);
                throw;
            }
            finally
            {                        
                if (copy.CopyStream != null)
                {                            
                    copy.CopyStream.Close();
                }
                copy.End();
            }
        }
    }
}
private static string SerializeData(object[] data)
{
    var sb = new StringBuilder();
    foreach(var d in data)
    {
        if (d == null)
        {
            sb.Append(&amp;quot;\\N&amp;quot;);
        }
        else if (d is DateTime)
        {
            sb.Append(((DateTime) d).ToString(&amp;quot;yyyy-MM-dd HH:mm:ss&amp;quot;));
        }
        else if (d is Enum)
        {
            sb.Append(((Enum) d).ToString(&amp;quot;d&amp;quot;));
        }
        else
        {
            sb.Append(d.ToString());
        }
        sb.Append(&amp;quot;|&amp;quot;);
    }
    return sb.Remove(sb.Length - 1, 1).ToString();
}&lt;/pre&gt;
&lt;p&gt;You use it like so:&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;var users = GetAllUsers();
Func&amp;lt;User, object[]&amp;gt; callback = u =&amp;gt; new object[] { p.Id, p.Email, p.UserName, p.Password };
BulkCopy(users, callback, &amp;quot;Users&amp;quot;, null);&lt;/pre&gt;
&lt;p&gt;If you want to specify the columns, say because the Id is an auto incrementing field, simply do:&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;var users = GetAllUsers();
Func&amp;lt;User, object[]&amp;gt; callback = u =&amp;gt; new object[] {  p.Email, p.UserName, p.Password };
BulkCopy(users, callback, &amp;quot;Users&amp;quot;, &amp;quot;(Email, UserName, Password)&amp;quot;);&lt;/pre&gt;
&lt;p&gt;The code essentially turns each entity in your collection into a serialized string, which gets written to the stream.&lt;/p&gt;
&lt;p&gt;The other problem I had was that one of my most complex queries was really slow. The query had joins, joining a subselect in the where clause. SQL Server didn&amp;#39;t have a problem with it, PostgreSQL did. I wasn&amp;#39;t too surprised because past experience with MySQL had taught me that such queries could be problematic. I saw a few statements hinting that PostgreSQL 8.4 (I&amp;#39;m running 8.3) might fix it. I spent some time trying to speed it up, had some success, and then simply decided to go about it a different way. While I think its very likely that I wasn&amp;#39;t doing it properly, there&amp;#39;s no denying that SQL Server handled this better.&lt;/p&gt;
&lt;p&gt;I did time the execution of my integration test, using the same code. This was run on identically pathetic machines - an Atom 330 with 4gigs of ram and a 5400RMP hard drive. Postgresql 8.3 on Ubuntu server 9.04 and SQL Server 2008 on Windows Server 2008 R2. Without using any bulk copy, Postgresql was slightly faster. With their respective bulk copy, SQL Server was slightly faster. I&amp;#39;m not giving numbers because the results were really close, seconds apart when everything took minutes. Also, I do think the test was useful; however, the code was single threaded which means it didn&amp;#39;t provide any insight on how the databases behave when under a more typical kind of load. My goal was really just to make sure I wasn&amp;#39;t making a huge mistake, and it doesn&amp;#39;t look, or feel, like I am.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=394963" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/2ElvldQCHAg" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/karlseguin/archive/2009/10/24/postgresql-day-2.aspx</feedburner:origLink></item><item><title>Machine.Migrations changes</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/Xh8I9H0AwVM/machine-migrations-changes.aspx</link><pubDate>Sat, 24 Oct 2009 21:56:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:394459</guid><dc:creator>aaronjensen</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;&lt;a href="http://codebetter.com/blogs/jacob.lewallen/archive/2008/04/25/a-first-look-at-machine-migrations.aspx"&gt;Jacob first introduced&lt;/a&gt; Machine.Migrations over a year ago. Since then, it&amp;#39;s been a solid part of our process and we&amp;#39;re up to nearly 500 migrations with it on one project.&lt;/p&gt; &lt;p&gt;Recently, I finally got around to making some changes I&amp;#39;ve been wanting to make and I wanted to call&amp;#39;em out:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;We split the repostiory out to its own &lt;a href="http://github.com/machine/machine.migrations"&gt;on github&lt;/a&gt;. I did this similar to the &lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2009/03/07/machine-specifications-mspec-has-moved-and-git-rocks.aspx"&gt;way I did it for MSpec&lt;/a&gt; some time ago. Again, the advantage here is that change logs are localized to the project rather than the entire machine overarching project.&lt;/li&gt; &lt;li&gt;I added complaints if you have multiple migrations with the same number. This silently caused problems before as one of the migrations would not get applied. Now it just yells and dies like a good app. Of course, this is not as important because of the next change.&lt;/li&gt; &lt;li&gt;&lt;p&gt;Next I added support for timestamped migrations. This helps &lt;strong&gt;immensely&lt;/strong&gt; on active projects with multiple developers. You don&amp;#39;t have to deal with communicating migration numbers to the team when you add them, you just generate a new one and merge it when you feel like it. We actually had half of our topic on Campfire dedicated to our current migration number. The other half was who owed how many pushups for breaking the build.&lt;/p&gt; &lt;p&gt;Here&amp;#39;s the rake task we use to generate a new migration:&lt;/p&gt; &lt;pre class="ruby" name="code"&gt;
namespace :new do
  task :migration do
    raise &amp;quot;usage: rake new:migration name=\&amp;quot;Your migration name\&amp;quot;&amp;quot; unless ENV.include?(&amp;#39;name&amp;#39;)

    name = ENV[&amp;#39;name&amp;#39;]

    filepath = &amp;quot;db/migrate/#{Time.now.strftime(&amp;#39;%Y%m%d%H%M%S&amp;#39;)}_#{name.gsub(/ /,&amp;#39;_&amp;#39;)}.cs&amp;quot;

    text = File.read(&amp;quot;db/migrate/template.cs&amp;quot;)
    File.open(filepath, &amp;#39;w&amp;#39;) { |file|
      file.puts text.gsub(/\$MigrationName\$/,&amp;quot;#{name.gsub(/ /,&amp;#39;_&amp;#39;)}&amp;quot;)
    }
  end
end
&lt;/pre&gt;
&lt;p&gt;Usage is simple, just create a template.cs in the directory (we use db/migrate) and then type &lt;pre&gt;rake new:migration name=&amp;quot;this is my migration name&amp;quot;&lt;/pre&gt;
Example template:
&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;
using System;
using System.Collections.Generic;
using System.Text;
using Machine.Migrations;

public class $MigrationName$ : SimpleMigration
{
  public override void Up()
  {
    throw new System.NotImplementedException();
  }

  public override void Down()
  {
    throw new System.NotImplementedException();
  }
}
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Finally, I added a command line runner. As we&amp;#39;ve grown more and more sick of msbuild and more and more fond of rake, we&amp;#39;ve wanted to slowly all but eliminate our dependency on MSBuild. The command line tool is quite simple:&lt;/p&gt;
&lt;pre&gt;
Machine.Migrations
Copyright (C) 2007, 2008, 2009 Machine Project

  c, connection-string  Required. The connection string to the database to migrate
  t, to                 Applies or unapplies migrations to get to the specified migration
  u, up                 Applies migrations to the latest
  s, scope              The scope?
  d, directory          Required. Directory containing the migrations
  v, compiler-version   Version of the compiler to use
  debug                 Show Diagnostics
  r, references         Assemblies to reference while building migrations separated by commas
  t, timeout            Default command timeout for migrations
  ?, help               Display this help screen
&lt;/pre&gt;
&lt;p&gt;
The only two flags you usually need are -c and -d. Just pass in the connection string and the location of your migrations. Be sure to quote them: migrate.exe -c &amp;quot;my connection string&amp;quot; -d &amp;quot;my migration directory&amp;quot;.&lt;/p&gt;
&lt;/li&gt;

&lt;p&gt;Hopefully these changes help others. Grab the latest &lt;a href="http://github.com/machine/machine.migrations"&gt;here&lt;/a&gt; and just msbuild the sln to build it.&lt;/p&gt;

&lt;p&gt;I do wonder though, what other active .NET migration projects are people using and how do you like them?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=394459" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/Xh8I9H0AwVM" height="1" width="1"/&gt;</description><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/migrations/default.aspx">migrations</category><feedburner:origLink>http://codebetter.com/blogs/aaron.jensen/archive/2009/10/24/machine-migrations-changes.aspx</feedburner:origLink></item><item><title>Map control confusion</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/F3wBLPEMcOs/map-control-confusion.aspx</link><pubDate>Thu, 22 Oct 2009 15:30:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:392558</guid><dc:creator>pvanooijen</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;When it comes to including geographical information in your web-application you have to make two main decisions. The first one is which API you are going to use, the second one is who is going to provide the geographical data. These two are not always completely independent. &lt;/p&gt;
&lt;p&gt;A lot of websites use google maps. The geographical information is supplied by google and the only way to get to that information is by using the &lt;a href="http://code.google.com/apis/maps/index.html"&gt;Google Javascript API&lt;/a&gt;. When it comes to performance Javascript is far from ideal, more sophisticated applications which try to add a lot of information to a map are quite likely to hit a script timeout. The other downside of this approach is the (lack of) smoothness of the display. Panning or zooming a map results in a lot of&amp;nbsp; image blur.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Blurredmap_5F00_383981DA.png"&gt;&lt;img height="494" width="772" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/Blurredmap_5F00_thumb_5F00_44B70929.png" alt="Blurredmap" border="0" title="Blurredmap" 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;Both downsides can be overcome by using Silverlight. The &lt;a href="http://www.codeplex.com/deepearth"&gt;DeepEarth&lt;/a&gt; project was the first to use &lt;a href="http://silverlight.net/learn/quickstarts/deepzoom/"&gt;deepzoom&lt;/a&gt; which results in a smooth display when scanning or zooming. The strong typed compiled C# Silverlight code working with the map has not only a speed advantage over Javascript, it&amp;rsquo;s running async resulting in an app which not only has a smooth look but also a smooth feel.&lt;/p&gt;
&lt;p&gt;You can build amazing apps with that. This is a part of the &lt;a href="http://www.soulsolutions.com.au/silverlight/mvpsummit09/"&gt;MVP map&lt;/a&gt; created by DeepEarth evangelist &lt;a href="http://www.soulsolutions.com.au/"&gt;SoulSolutions&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/mvpMAP_5F00_77D2F6F8.png"&gt;&lt;img height="593" width="771" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peter.van.ooijen/mvpMAP_5F00_thumb_5F00_1C4438E3.png" alt="mvpMAP" border="0" title="mvpMAP" 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;When it comes to where and how to get the geographical data, which can be satellite images, road maps or routing information, confusion starts. The DeepEarth project offered a choice of map providers. Ranging from &lt;a href="http://www.openstreetmap.org/"&gt;OpenStreetmap&lt;/a&gt; to Yahoo maps and Microsoft Live Earth. Google maps were not included, the reason is the one I mentioned. Nobody so far has managed to create a hack to get Google Map data without leaving JS.&lt;/p&gt;
&lt;p&gt;But the most confusing data-provider was and is Microsoft. The first versions of DeepEarth offered plain Live maps. In later versions you were supposed to use LiveMaps webservices. The usage of these services was by far free. There was a free developer test account available which resulted in map images all decorated with the word &amp;ldquo;staging&amp;rdquo;. For production you had to buy a license. To get a proper deal for that always got stuck somewhere in the pipeline. At the same time the old versions using plain Live maps kept working. At the moment I&amp;rsquo;m writing this seems to be history already.&lt;/p&gt;
&lt;p&gt;LiveMaps have become Bing maps. At the same time Microsoft started developing their own &lt;a href="https://connect.microsoft.com/silverlightmapcontrolctp?wa=wsignin1.0"&gt;Silverlight map control&lt;/a&gt;. The good thing is that it is a marvel to work with. The performance is even better than DeepEarth. The API just shines, a lot of functionality is present straight out of the box. All &lt;a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/12/11/wpf-code-markup-custom-pushpins-for-deepearth.aspx"&gt;the extra work I had to do&lt;/a&gt; to get my app running in Deep Earth could be replaced by just hooking into some events. The architecture is great as well. It is by no means just limited to using Bing Maps data, you can even bypass that completely using the control as a blank working surface with a Latitude-Longitude coordinate system. A part of the architecture is a clear and easy way to use any map provider you can imagine, or build yourself. There are a lot of good stories on the web on working with the control.&lt;/p&gt;
&lt;p&gt;But, as announced, there is still confusion. First there is the name. It is Microsoft Virtual Earth Silverlight Map Control. I have no problem with that, do you like &lt;em&gt;Bing&lt;/em&gt; ? I can&amp;rsquo;t get used to that. More serious is the source of map data. The control, now in CTP, displays full MS map data without any need to apply for a (test-) license. No staging, no problem. The control itself has a &lt;strong&gt;Token&lt;/strong&gt; property, according to the documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Gets or sets the token to use for service requests. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It contains an empty string. How and when to use it is not clear at all. When it comes to production will it be needed to use the control? Or will it only be needed to get actual MS map data? Which data ? Just routing information ? Maps? Satellite images ? I just cannot find out. In case anybody can shed some light on this, I would appreciate that very much. Building software using the control is great, but what are price and conditions my customer will have to deal with?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=392558" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/F3wBLPEMcOs" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/peter.van.ooijen/archive/2009/10/22/map-control-confusion.aspx</feedburner:origLink></item><item><title>Migrating to Postgresql with my friend NHibernate</title><link>http://feedproxy.google.com/~r/CodeBetter/~3/0yBYUnlw9RI/migrating-to-postgresql-with-my-friend-nhibernate.aspx</link><pubDate>Thu, 22 Oct 2009 13:23:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:392493</guid><dc:creator>karl</dc:creator><slash:comments>34</slash:comments><description>&lt;p&gt;Last night I decided to accelerate our plan to migrate from SQL Server to PostgreSQL and gave it a quick attempt to see what the work involved was. I was hoping that due to our usage of NHibernate, and by having meticulously written database agonistic code the migration would be cake.&lt;/p&gt;
&lt;p&gt;Turns out I was right and although there were a couple bugs along the way, the process took less than 30 minutes. Today I want to talk about the small issues we faced in migrating our code, as well as provide an overview of what I&amp;#39;ve learned about PostgreSQL so far.&lt;/p&gt;
&lt;p&gt;First though, why migrate? I want to avoid getting rantish [in this post], but the cost of ownership for SQL Server isn&amp;#39;t remotely close to being competitive. SQL Express is great, provided you can live with (or are willing to) its numerous constraints. Bizspark (I only bring it up because I feel someone will mention it in a comment) is a subprime mortgage - it lulls you into a sense of free only to nail you with licensing cost down the road. I&amp;#39;m sure someone can quote some obscure feature that might be needed by some system, but for the rest of us, the only reason to use SQL Server is familiarity. Familiarity is a pragmatic and reasonable reason to pick a technology, but only for so long. &lt;/p&gt;
&lt;p&gt;Secondly, why PostgresQL? Well, I&amp;#39;ve used sqlite and MySQL extensively in the past (including submitting patches to their .NET drivers). However, MySQL is currently in a weird state (the sun/oracle thing) and I need ACID compliance and my experience with InnoDB has been average.&lt;/p&gt;
&lt;p&gt;Thirdly, its worth pointing out that the system being migrated has a comprehensive set of unit tests, including a number tests which hit the DB. While I generally consider unit tests a design-tool, there&amp;#39;s no doubt that they provided a significant sanity check. &lt;/p&gt;
&lt;h4 style="margin-bottom:0px;padding-bottom:0px;"&gt;So, what problems did I run into?&lt;/h4&gt;
&lt;p&gt;Given the use of NHibernate, the application side of things was migrated quiet easily. However, I did have to modify a few things. This is possibly because of flaws in my code (vs limitations of NHibernate) - hopefully someone will point such flaws out.&lt;/p&gt;
&lt;h5 style="margin-bottom:0px;padding-bottom:0px;"&gt;Unique Constraints&lt;/h5&gt;
&lt;p&gt;I use unique indexes to ensure that things like user emails are unique throughout the system. However, I haven&amp;#39;t yet figured out a way to catch such exceptions in a db-agnostic manner. My code looked something like:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;try
{
    Session.SaveOrUpdate(user);
    transaction.Commit();
    return true;
}
catch (GenericADOException ex)
{
    transaction.Rollback();
    var sql = ex.InnerException as SqlException;
    if (sql != null &amp;amp;&amp;amp; sql.Number == 2601)
    {
        return false;
    }
    throw;
}
catch (Exception)
{
    transaction.Rollback();
    throw;
}&lt;/pre&gt;
&lt;p&gt;Obviously this code isn&amp;#39;t going to work with the PostgreSQL driver. I keep thinking there *is* a way to handle this across multiple databases, but haven&amp;#39;t found the answer yet.&lt;/p&gt;
&lt;h5 style="margin-bottom:0px;padding-bottom:0px;"&gt;Count - Long vs Int&lt;/h5&gt;
&lt;p&gt;When I fetch the number of rows for a query - more often than not to do paging - i do a &lt;code&gt;select count(*) with a UniqueResult&amp;lt;int&amp;gt;()&lt;/code&gt;. However, PostgreSQL&amp;#39;s &lt;code&gt;count(*) &lt;/code&gt;returns a &lt;code&gt;long&lt;/code&gt;, while SQL Server returns an &lt;code&gt;int&lt;/code&gt; (I&amp;#39;ve run into this before, since sqlite also returns a &lt;code&gt;long&lt;/code&gt;). This causes a cast exception. Again, I&amp;#39;m sure there&amp;#39;s a simple solution to this inconsistency.&lt;/p&gt;
&lt;h5 style="margin-bottom:0px;padding-bottom:0px;"&gt;Bit and Booleans&lt;/h5&gt;
&lt;p&gt;There was one specific query which tried to retrieve all active users. The IsActive field was declared as a bit in SQL Server (and a boolean in PostgreSQL). The following query didn&amp;#39;t work in PostgresQL &amp;quot;from User where IsActive = 1&amp;quot;. Why? because 1 isn&amp;#39;t a valid boolean, and you&amp;#39;ll get a database error that an integer can&amp;#39;t be converted to a boolean. I had to change the query to use &amp;#39;1&amp;#39; (in single quotes) or one of the other valid boolean values for true.&lt;/p&gt;
&lt;h5 style="margin-bottom:0px;padding-bottom:0px;"&gt;Connection String&lt;/h5&gt;
&lt;p&gt;Its also worth mentioning that the connection string example listed here, didn&amp;#39;t work for me using the latest &lt;a href="http://pgfoundry.org/projects/npgsql/"&gt;Npgsql drivers&lt;/a&gt;. Instead of &lt;code&gt;Initial Catalog&lt;/code&gt;, I had to use the &lt;code&gt;Database&lt;/code&gt; key.&lt;/p&gt;
&lt;p&gt;Aside from those little issues, the code was up in running in about 10 minutes. Most of my time was actually spent getting to know PostgreSQL. Here&amp;#39;s what I&amp;#39;ve learned so far.&lt;/p&gt;
&lt;h4 style="margin-bottom:0px;padding-bottom:0px;"&gt;Learning PostgreSQL&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL doesn&amp;#39;t consider two null values to be the same, meaning you can put a unique constraint on a nullable column without any problems. In SQL Server you need a Nullbuster column to achieve this same result (its a neat trick, but its neater not having to use it).&lt;/li&gt;
&lt;li&gt;PostgreSQL&amp;#39;s &lt;code&gt;uniqueidentifier&lt;/code&gt; type is called &lt;code&gt;UUID&lt;/code&gt;, a &lt;code&gt;bit&lt;/code&gt; is a &lt;code&gt;boolean&lt;/code&gt;, and a &lt;code&gt;datetime&lt;/code&gt; is a &lt;code&gt;timestamp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All of PostgreSQL&amp;#39;s text types are unicode by default (so there is no &lt;code&gt;nvarchar&lt;/code&gt; you simply use &lt;code&gt;varchar&lt;/code&gt; and get the same benefits)&lt;/li&gt;
&lt;li&gt;PostgreSQL&amp;#39;s handling of auto incrementing (identity) is pretty different. Instead of declaring a column as &lt;code&gt;int not null identity(1, 1) primary key&lt;/code&gt;, you define it as s&lt;code&gt;erial not null primary key&lt;/code&gt;. &amp;quot;serial&amp;quot; is just some syntactical sugar around what actually happens - PostgreSQL generates a sequence table for that column, and sets the columns default value to the sequence&amp;#39;s next value. Of course, identity(1, 1) is just some syntactical sugar around however SQL Server implements the feature - its just more transparent in PostgreSQL.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hopefully as the weeks go by, I&amp;#39;ll have more information to share about the migration, and what I&amp;#39;ve learned.
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=392493" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeBetter/~4/0yBYUnlw9RI" height="1" width="1"/&gt;</description><feedburner:origLink>http://codebetter.com/blogs/karlseguin/archive/2009/10/22/migrating-to-postgresql-with-my-friend-nhibernate.aspx</feedburner:origLink></item></channel></rss>
