<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>TJ Solutions</title>
    <link>http://www.tjsolutions.nl/</link>
    <description />
    <language>en-us</language>
    <copyright>Tijmen van de Kamp, Tom de Koning</copyright>
    <lastBuildDate>Tue, 01 Dec 2009 17:56:46 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@tjsolutions.nl</managingEditor>
    <webMaster>blog@tjsolutions.nl</webMaster>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=e17144a2-3592-4e5f-9389-a4ffc152bc47</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,e17144a2-3592-4e5f-9389-a4ffc152bc47.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,e17144a2-3592-4e5f-9389-a4ffc152bc47.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=e17144a2-3592-4e5f-9389-a4ffc152bc47</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h1>
        </h1>
        <h1>Manipulating the DOM with Firebug
</h1>
        <p>
Currently I"m working more and more with jQuery and JavaScript to get behavior into
my pages. This was quite painful in the beginning as I was depended a bit too much
on alert"('this happens!').
</p>
        <p>
But I guess this is where you get started and then find out about the excellent <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">firebug</a>.
With the console you can then fire your JavaScript and jQuery commands to the page
like so:
</p>
        <p>
 
</p>
        <p>
 <a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_6.png"><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://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_thumb_2.png" width="508" height="481" /></a></p>
        <p>
        </p>
        <p>
 
</p>
        <p>
I'm querying on of Holland's main news sites for their logo. Firebug conveniently
comes up with the link to the element and when clicked it will show where it's located
on the page.
</p>
        <p>
This is all nice but let's change the boring logo of <a href="www.nu.nl">nu.nl</a> to
that of our own!
</p>
        <p>
Easy enough, just use the following to change the picture in the page:
</p>
        <code>$('#sidemenu a img').attr('src', 'http://www.tjsolutions.nl/themes/tjs/images/headerbg.jpg') </code>
        <p>
 
</p>
        <p>
 
</p>
        <p>
          <a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_8.png">
            <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://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_thumb_3.png" width="516" height="488" />
          </a>
        </p>
        <p>
 
</p>
        <p>
But changing the elements in a page is just the beginning. You can just as easily
add new functionality by binding a click event handler to one or more elements in
the page. 
</p>
        <p>
 
</p>
        <h1>Viewing registered events
</h1>
        <p>
You can also use the console to inspect what events are registered to an element on
the page. All you need to do is use this syntax:
</p>
        <code>$(selector).data("events"); </code>
        <p>
 
</p>
        <p>
When we try that on the last input (randomly chosen) you get an overview of all the
events bound (in this case only one).
</p>
        <p>
 
</p>
        <p>
          <a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/events_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="events" border="0" alt="events" src="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/events_thumb.png" width="557" height="147" />
          </a>
        </p>
        <p>
 
</p>
        <p>
If you the click on the object and hover over "function" you'll end up with the code
itself:
</p>
        <p>
 
</p>
        <p>
          <a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/function_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="function" border="0" alt="function" src="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/function_thumb.png" width="559" height="157" />
          </a>
        </p>
        <p>
 
</p>
        <p>
This is a very neat trick that I wish I had stumbled upon earlier. Hope it helps.
</p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=e17144a2-3592-4e5f-9389-a4ffc152bc47" />
      </body>
      <title>Getting started with Firebug and jQuery</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,e17144a2-3592-4e5f-9389-a4ffc152bc47.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/12/01/GettingStartedWithFirebugAndJQuery.aspx</link>
      <pubDate>Tue, 01 Dec 2009 17:56:46 GMT</pubDate>
      <description>&lt;h1&gt;
&lt;/h1&gt;
&lt;h1&gt;Manipulating the DOM with Firebug
&lt;/h1&gt;
&lt;p&gt;
Currently I"m working more and more with jQuery and JavaScript to get behavior into
my pages. This was quite painful in the beginning as I was depended a bit too much
on alert&amp;quot;('this happens!').
&lt;/p&gt;
&lt;p&gt;
But I guess this is where you get started and then find out about the excellent &lt;a href="https://addons.mozilla.org/en-US/firefox/addon/1843"&gt;firebug&lt;/a&gt;.
With the console you can then fire your JavaScript and jQuery commands to the page
like so:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;&lt;a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_6.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://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_thumb_2.png" width="508" height="481" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
I'm querying on of Holland's main news sites for their logo. Firebug conveniently
comes up with the link to the element and when clicked it will show where it's located
on the page.
&lt;/p&gt;
&lt;p&gt;
This is all nice but let's change the boring logo of &lt;a href="www.nu.nl"&gt;nu.nl&lt;/a&gt; to
that of our own!
&lt;/p&gt;
&lt;p&gt;
Easy enough, just use the following to change the picture in the page:
&lt;/p&gt;
&lt;code&gt;$('#sidemenu a img').attr('src', 'http://www.tjsolutions.nl/themes/tjs/images/headerbg.jpg') &lt;/code&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_8.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://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/image_thumb_3.png" width="516" height="488" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
But changing the elements in a page is just the beginning. You can just as easily
add new functionality by binding a click event handler to one or more elements in
the page. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h1&gt;Viewing registered events
&lt;/h1&gt;
&lt;p&gt;
You can also use the console to inspect what events are registered to an element on
the page. All you need to do is use this syntax:
&lt;/p&gt;
&lt;code&gt;$(selector).data(&amp;quot;events&amp;quot;); &lt;/code&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
When we try that on the last input (randomly chosen) you get an overview of all the
events bound (in this case only one).
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/events_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="events" border="0" alt="events" src="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/events_thumb.png" width="557" height="147" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
If you the click on the object and hover over "function" you'll end up with the code
itself:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/function_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="function" border="0" alt="function" src="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/GettingstartedwithFirebugandjQuery_10A6B/function_thumb.png" width="559" height="157" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
This is a very neat trick that I wish I had stumbled upon earlier. Hope it helps.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=e17144a2-3592-4e5f-9389-a4ffc152bc47" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,e17144a2-3592-4e5f-9389-a4ffc152bc47.aspx</comments>
      <category>BDD</category>
      <category>Javascript</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=eac64485-c3f7-4d54-a23b-64be19470e2e</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,eac64485-c3f7-4d54-a23b-64be19470e2e.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,eac64485-c3f7-4d54-a23b-64be19470e2e.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=eac64485-c3f7-4d54-a23b-64be19470e2e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was pointed to the Passionate programmer by one of the blog posts at <a href="http://www.lostechies.com/blogs/louissalin/archive/2009/11/15/the-passionate-programmer-review.aspx?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+LosTechies+%28LosTechies%29">Los
Techies</a>. I'm about a third through the book and so far it has been a real joy
to read!
</p>
        <p>
It's full of good tips on how to alter your behavior to become something better as
a programmer. I'm probably going to write some posts on what I've done with the various
"act on it!", but in the meantime, go get it and start reading!
</p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eac64485-c3f7-4d54-a23b-64be19470e2e" />
      </body>
      <title>The Passionate programmer</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,eac64485-c3f7-4d54-a23b-64be19470e2e.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/11/17/ThePassionateProgrammer.aspx</link>
      <pubDate>Tue, 17 Nov 2009 19:56:09 GMT</pubDate>
      <description>&lt;p&gt;
I was pointed to the Passionate programmer by one of the blog posts at &lt;a href="http://www.lostechies.com/blogs/louissalin/archive/2009/11/15/the-passionate-programmer-review.aspx?utm_source=feedburner&amp;amp;utm_medium=feed&amp;amp;utm_campaign=Feed%3A+LosTechies+%28LosTechies%29"&gt;Los
Techies&lt;/a&gt;. I'm about a third through the book and so far it has been a real joy
to read!
&lt;/p&gt;
&lt;p&gt;
It's full of good tips on how to alter your behavior to become something better as
a programmer. I'm probably going to write some posts on what I've done with the various
"act on it!", but in the meantime, go get it and start reading!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eac64485-c3f7-4d54-a23b-64be19470e2e" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,eac64485-c3f7-4d54-a23b-64be19470e2e.aspx</comments>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=b9a7be28-2e24-451c-82a2-4ac11d6922d4</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=b9a7be28-2e24-451c-82a2-4ac11d6922d4</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Just a quick update on my <a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingWithTheRequestResponseServiceLayer.aspx">previous</a> posting
following one of the comments made by <a href="http://benpittoors.wordpress.com/">den
Ben</a></p>
        <p>
 
</p>
        <p>
just out of curiosity... why wouldn't you 
<br />
dispatcher.Add&lt;SaveDocumentRequest&gt;(s =&gt; 
<br />
{ 
<br />
s.Document = document; 
<br />
s.UserID = userID; 
<br />
});
</p>
        <p>
 
</p>
        <p>
He's absolutely right. Using a Func forced me to (mis)use the params keyword as you
are not allowed  to use the above syntax with the old code: 
</p>
        <pre class="code">
          <span style="color: blue">public virtual </span>
          <span style="color: #2b91af">IDispatcher </span>Add&lt;TRequestType&gt;(<span style="color: blue">params </span><span style="color: #2b91af">Func</span>&lt;TRequestType, <span style="color: blue">object</span>&gt;[]
funcs) <span style="color: blue">where </span>TRequestType : <span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ TRequestType request = <span style="color: blue">new </span>TRequestType(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>func <span style="color: blue">in </span>funcs)
{ func.Invoke(request); } AddRequest(request, <span style="color: blue">false</span>); <span style="color: blue">return
this</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>
        </p>
        <p>
I actually had noticed the code not using the return value by the func but disregarded
it as I was a bit too happy with the "elegance" of my inital solution.
</p>
        <p>
But I think (and I think you'll agree) that using an Action is much cleaner here:
</p>
        <pre class="code">
          <span style="color: blue">public virtual </span>
          <span style="color: #2b91af">IDispatcher </span>Add&lt;TRequestType&gt;(<span style="color: #2b91af">Action</span>&lt;TRequestType&gt;
action) <span style="color: blue">where </span>TRequestType : <span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ TRequestType request = <span style="color: blue">new </span>TRequestType(); action(request);
AddRequest(request, <span style="color: blue">false</span>); <span style="color: blue">return
this</span>; }</pre>
        <p>
 
</p>
        <p>
Thanks <a href="http://benpittoors.wordpress.com/">den Ben</a>!<a href="http://11011.net/software/vspaste"></a></p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=b9a7be28-2e24-451c-82a2-4ac11d6922d4" />
      </body>
      <title>Functional programming with the Request/Response Service Layer part2</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/11/16/FunctionalProgrammingWithTheRequestResponseServiceLayerPart2.aspx</link>
      <pubDate>Mon, 16 Nov 2009 18:01:36 GMT</pubDate>
      <description>&lt;p&gt;
Just a quick update on my &lt;a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingWithTheRequestResponseServiceLayer.aspx"&gt;previous&lt;/a&gt; posting
following one of the comments made by &lt;a href="http://benpittoors.wordpress.com/"&gt;den
Ben&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
just out of curiosity... why wouldn't you 
&lt;br /&gt;
dispatcher.Add&amp;lt;SaveDocumentRequest&amp;gt;(s =&amp;gt; 
&lt;br /&gt;
{ 
&lt;br /&gt;
s.Document = document; 
&lt;br /&gt;
s.UserID = userID; 
&lt;br /&gt;
});
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
He's absolutely right. Using a Func forced me to (mis)use the params keyword as you
are not allowed&amp;#160; to use the above syntax with the old code: 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDispatcher &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TRequestType, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;[]
funcs) &lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType : &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ TRequestType request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;func &lt;span style="color: blue"&gt;in &lt;/span&gt;funcs)
{ func.Invoke(request); } AddRequest(request, &lt;span style="color: blue"&gt;false&lt;/span&gt;); &lt;span style="color: blue"&gt;return
this&lt;/span&gt;; }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
I actually had noticed the code not using the return value by the func but disregarded
it as I was a bit too happy with the "elegance" of my inital solution.
&lt;/p&gt;
&lt;p&gt;
But I think (and I think you'll agree) that using an Action is much cleaner here:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDispatcher &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TRequestType&amp;gt;
action) &lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType : &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ TRequestType request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); action(request);
AddRequest(request, &lt;span style="color: blue"&gt;false&lt;/span&gt;); &lt;span style="color: blue"&gt;return
this&lt;/span&gt;; }&lt;/pre&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Thanks &lt;a href="http://benpittoors.wordpress.com/"&gt;den Ben&lt;/a&gt;!&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=b9a7be28-2e24-451c-82a2-4ac11d6922d4" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</comments>
      <category>.NET</category>
      <category>Functional</category>
      <category>Linq</category>
      <category>Request Response Service Layer</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=eba99e4a-f608-48e7-8541-397cb4c9bcf1</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=eba99e4a-f608-48e7-8541-397cb4c9bcf1</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of my colleagues asked me today if I could write a utility function that would
make his life more easy.
</p>
        <p>
The objective was to call a stored procedure n-times for every item in a list. Of
course some of the properties on every item had to be mapped to parameters in the
stored procedure.
</p>
        <p>
 
</p>
        <p>
The desired syntax would be something like:
</p>
        <p>
builder.SetStoredProcedureNameTo( " some sproc name ") 
<br />
        .Execute ( customerList , 
<br />
            (s,t) =&gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_1) , 
<br />
            (s,t) =&gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_2) , 
<br />
             (s,t) =&gt;
s.AddParameterWIthValue( parameterName, t.PROPERTY_NAME_3) 
<br />
      );
</p>
        <p>
 
</p>
        <p>
I already have a fluent wrapper class to make our ADO.Net a bit more friendly, the
interface looks like:
</p>
        <pre class="code">
          <span style="color: blue">public interface </span>
          <span style="color: #2b91af">ICustomSqlCommandBuilder </span>{ <span style="color: #2b91af">ICustomSqlCommandBuilder </span>SetStoredProcedureNameTo(<span style="color: blue">string </span>storedProcedureName); <span style="color: #2b91af">ICustomSqlCommandBuilder </span>AddParameterWithValue(<span style="color: blue">string </span>parameterNameInDatabase, <span style="color: blue">object </span>value); <span style="color: #2b91af">ICustomSqlCommandBuilder </span>AddExlicitOutputIntParameterToTheCommandFor(<span style="color: blue">string </span>parameterName); <span style="color: #2b91af">ICustomSqlCommandBuilder </span>AddExlicitOutputDateTimeParameterToTheCommandFor(<span style="color: blue">string </span>parameterName); <span style="color: #2b91af">IList</span>&lt;TReturnType&gt;
ExecuteReaderFor&lt;TReturnType&gt;() <span style="color: blue">where </span>TReturnType
: <span style="color: blue">new</span>(); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Nothing special except for the ExecuteReaderFor&lt;TReturnType&gt;() but that's for
another post. 
</p>
        <p>
 
</p>
        <p>
Having the fluent interface on ICustomSqlCommandBuilder combined with the lambda magic
explained in <a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingForEveryday.aspx">this</a> post
I came up with the following signature:
</p>
        <pre class="code">
          <span style="color: #2b91af">ICustomSqlCommandBuilder </span>ExecuteScalarForThisList&lt;T&gt;( <span style="color: #2b91af">IList</span>&lt;T&gt;
list, <span style="color: blue">params </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ICustomSqlCommandBuilder</span>,
T&gt;[] actionList);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>The general idea is to apply <pre class="code">(s, t) =&gt; s.AddParameterWithValue(<span style="color: #a31515">"parameterName"</span>,
t.PROPERTY_NAME)</pre><p>
to every property in the object&lt;T&gt; that needs to be mapped against a parameter
in the stored procedure. Having the params allows us to map as many properties to
parameters in the stored procedure as we like. If we then loop through all the items
in the list first and apply all actions in the actionList per item, we can then call
ExecuteScalar on the stored procedure.
</p><pre class="code"><span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>list)
{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>action <span style="color: blue">in </span>actionList)
{ action.Invoke(<span style="color: blue">this</span>, item); } <span style="color: blue">var </span>result
= sqlCommand.ExecuteScalar(); sqlCommand.Parameters.Clear(); }</pre><p>
Now, another requirement was to wrap it in a transaction and also to have the verification
of the result a bit more flexible. All in all not too complicated, just a matter of
introducing another Action:
</p><pre class="code"><span style="color: blue">public </span><span style="color: #2b91af">ICustomSqlCommandBuilder </span>ExecuteScalarForThisList&lt;T&gt;( <span style="color: #2b91af">IList</span>&lt;T&gt;
list, <span style="color: #2b91af">Action</span>&lt;<span style="color: blue">object</span>&gt;
verify, <span style="color: blue">params </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ICustomSqlCommandBuilder</span>,
T&gt;[] actionList) { sqlCommand.Connection.Open(); <span style="color: #2b91af">SqlTransaction </span>transaction
= sqlCommand.Connection.BeginTransaction(); sqlCommand.Transaction = transaction; <span style="color: blue">try </span>{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>list)
{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>action <span style="color: blue">in </span>actionList)
{ action.Invoke(<span style="color: blue">this</span>, item); } <span style="color: blue">var </span>result
= sqlCommand.ExecuteScalar(); verify(result); sqlCommand.Parameters.Clear(); } transaction.Commit();
} <span style="color: blue">catch </span>(<span style="color: #2b91af">SqlException</span>)
{ transaction.Rollback(); <span style="color: blue">throw</span>; } <span style="color: blue">catch </span>(<span style="color: #2b91af">Exception</span>)
{ transaction.Rollback(); <span style="color: blue">throw</span>; } <span style="color: blue">return
this</span>; }</pre><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><p>
 
</p><p>
Including a bit of exceptionally brittle exception handling and calling the stored
procedure on a list of Customers now resolves to:
</p><pre class="code"><span style="color: #2b91af">Action</span>&lt;<span style="color: blue">object</span>&gt;
resultConstraint = result =&gt; { <span style="color: blue">if </span>(result != <span style="color: blue">null</span>)
{ <span style="color: blue">if </span>((<span style="color: blue">int</span>)result
&gt; 1000) { <span style="color: blue">throw new </span><span style="color: #2b91af">Exception</span>(<span style="color: #a31515">"result
should be less than 1000!"</span>); } } }; builder .SetStoredProcedureNameTo(<span style="color: #a31515">"sens_sp_modifyCustomerIncome"</span>)
.ExecuteScalarForThisList( customerList, resultConstraint, (s, t) =&gt; s.AddParameterWithValue(<span style="color: #a31515">"id"</span>,
t.ID), (s, t) =&gt; s.AddParameterWithValue(<span style="color: #a31515">"income"</span>,
t.Income) <span style="color: green">//etc for the rest of the properties </span>);</pre><a href="http://11011.net/software/vspaste"></a>I really, really like this syntax! <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eba99e4a-f608-48e7-8541-397cb4c9bcf1" /></body>
      <title>Having fun with functional programming and legacy ADO.Net!</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/11/12/HavingFunWithFunctionalProgrammingAndLegacyADONet.aspx</link>
      <pubDate>Thu, 12 Nov 2009 19:14:15 GMT</pubDate>
      <description>&lt;p&gt;
One of my colleagues asked me today if I could write a utility function that would
make his life more easy.
&lt;/p&gt;
&lt;p&gt;
The objective was to call a stored procedure n-times for every item in a list. Of
course some of the properties on every item had to be mapped to parameters in the
stored procedure.
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
The desired syntax would be something like:
&lt;/p&gt;
&lt;p&gt;
builder.SetStoredProcedureNameTo( " some sproc name ") 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Execute ( customerList , 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (s,t) =&amp;gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_1) , 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (s,t) =&amp;gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_2) , 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (s,t) =&amp;gt;
s.AddParameterWIthValue( parameterName, t.PROPERTY_NAME_3) 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; );
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
I already have a fluent wrapper class to make our ADO.Net a bit more friendly, the
interface looks like:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;SetStoredProcedureNameTo(&lt;span style="color: blue"&gt;string &lt;/span&gt;storedProcedureName); &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;AddParameterWithValue(&lt;span style="color: blue"&gt;string &lt;/span&gt;parameterNameInDatabase, &lt;span style="color: blue"&gt;object &lt;/span&gt;value); &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;AddExlicitOutputIntParameterToTheCommandFor(&lt;span style="color: blue"&gt;string &lt;/span&gt;parameterName); &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;AddExlicitOutputDateTimeParameterToTheCommandFor(&lt;span style="color: blue"&gt;string &lt;/span&gt;parameterName); &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;TReturnType&amp;gt;
ExecuteReaderFor&amp;lt;TReturnType&amp;gt;() &lt;span style="color: blue"&gt;where &lt;/span&gt;TReturnType
: &lt;span style="color: blue"&gt;new&lt;/span&gt;(); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Nothing special except for the ExecuteReaderFor&amp;lt;TReturnType&amp;gt;() but that's for
another post. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Having the fluent interface on ICustomSqlCommandBuilder combined with the lambda magic
explained in &lt;a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingForEveryday.aspx"&gt;this&lt;/a&gt; post
I came up with the following signature:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;ExecuteScalarForThisList&amp;lt;T&amp;gt;( &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;T&amp;gt;
list, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder&lt;/span&gt;,
T&amp;gt;[] actionList);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The general idea is to apply &lt;pre class="code"&gt;(s, t) =&amp;gt; s.AddParameterWithValue(&lt;span style="color: #a31515"&gt;&amp;quot;parameterName&amp;quot;&lt;/span&gt;,
t.PROPERTY_NAME)&lt;/pre&gt;
&lt;p&gt;
to every property in the object&amp;lt;T&amp;gt; that needs to be mapped against a parameter
in the stored procedure. Having the params allows us to map as many properties to
parameters in the stored procedure as we like. If we then loop through all the items
in the list first and apply all actions in the actionList per item, we can then call
ExecuteScalar on the stored procedure.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;list)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;action &lt;span style="color: blue"&gt;in &lt;/span&gt;actionList)
{ action.Invoke(&lt;span style="color: blue"&gt;this&lt;/span&gt;, item); } &lt;span style="color: blue"&gt;var &lt;/span&gt;result
= sqlCommand.ExecuteScalar(); sqlCommand.Parameters.Clear(); }&lt;/pre&gt;
&lt;p&gt;
Now, another requirement was to wrap it in a transaction and also to have the verification
of the result a bit more flexible. All in all not too complicated, just a matter of
introducing another Action:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;ExecuteScalarForThisList&amp;lt;T&amp;gt;( &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;T&amp;gt;
list, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;
verify, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder&lt;/span&gt;,
T&amp;gt;[] actionList) { sqlCommand.Connection.Open(); &lt;span style="color: #2b91af"&gt;SqlTransaction &lt;/span&gt;transaction
= sqlCommand.Connection.BeginTransaction(); sqlCommand.Transaction = transaction; &lt;span style="color: blue"&gt;try &lt;/span&gt;{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;list)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;action &lt;span style="color: blue"&gt;in &lt;/span&gt;actionList)
{ action.Invoke(&lt;span style="color: blue"&gt;this&lt;/span&gt;, item); } &lt;span style="color: blue"&gt;var &lt;/span&gt;result
= sqlCommand.ExecuteScalar(); verify(result); sqlCommand.Parameters.Clear(); } transaction.Commit();
} &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SqlException&lt;/span&gt;)
{ transaction.Rollback(); &lt;span style="color: blue"&gt;throw&lt;/span&gt;; } &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;)
{ transaction.Rollback(); &lt;span style="color: blue"&gt;throw&lt;/span&gt;; } &lt;span style="color: blue"&gt;return
this&lt;/span&gt;; }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Including a bit of exceptionally brittle exception handling and calling the stored
procedure on a list of Customers now resolves to:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;
resultConstraint = result =&amp;gt; { &lt;span style="color: blue"&gt;if &lt;/span&gt;(result != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;((&lt;span style="color: blue"&gt;int&lt;/span&gt;)result
&amp;gt; 1000) { &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;result
should be less than 1000!&amp;quot;&lt;/span&gt;); } } }; builder .SetStoredProcedureNameTo(&lt;span style="color: #a31515"&gt;&amp;quot;sens_sp_modifyCustomerIncome&amp;quot;&lt;/span&gt;)
.ExecuteScalarForThisList( customerList, resultConstraint, (s, t) =&amp;gt; s.AddParameterWithValue(&lt;span style="color: #a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;,
t.ID), (s, t) =&amp;gt; s.AddParameterWithValue(&lt;span style="color: #a31515"&gt;&amp;quot;income&amp;quot;&lt;/span&gt;,
t.Income) &lt;span style="color: green"&gt;//etc for the rest of the properties &lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I really, really like this syntax! &lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eba99e4a-f608-48e7-8541-397cb4c9bcf1" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</comments>
      <category>.NET</category>
      <category>ADO.Net</category>
      <category>Functional</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=da163853-8db7-45bf-a157-9c0058030aa3</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=da163853-8db7-45bf-a157-9c0058030aa3</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
At my current project we are using a dispatcher class to batch requests to our WCF
layer based on the <a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/">Request/Response
Service Layer</a> created by Davy Brion. Once you call the Get&lt;TResponseType&gt;()
on the dispatcher the WCF layer starts working on the requests one by one and return
them in one roundtrip.
</p>
        <p>
Adding requests to the dispatcher is easy:
</p>
        <pre class="code">
          <span style="color: blue">var </span>saveDocumentRequest = <span style="color: blue">new </span><span style="color: #2b91af">SaveDocumentRequest</span>(); <span style="color: blue">var </span>getRemainingDocuments
= <span style="color: blue">new </span><span style="color: #2b91af">GetRemainingDocumentsRequest</span>();
dispatcher.Add(saveDocumentRequest, getRemainingDocuments);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <p>
Where the signature of the Add method in Dispatcher is like;
</p>
        <pre class="code">
          <span style="color: blue">public void </span>Add(<span style="color: blue">params </span><span style="color: #2b91af">Request</span>[]
requests)</pre>
        <p>
        </p>
        <p>
Although this syntax is not bad, it feels like so much like code from last month:
not very sexy. Wouldn't be more fun if we could spice it up a bit using generics and
a bit of Func?
</p>
        <p>
I'd love to get rid of instantiating a variable just for the sake of adding it to
my dispatcher. After a bit of fiddling around with my unit tests I came up with the
following solution.
</p>
        <p>
What I want is to call the Add method in a generic way and set the parameters on my
request object inline, using a Func. This is best illustrated with a test:
</p>
        <pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_assign_value_using_a_func_in_a_generic_method_call() { <span style="color: #2b91af">Guid </span>id
= <span style="color: #2b91af">Guid</span>.NewGuid(); <span style="color: blue">var </span>dispatcher
= <span style="color: blue">new </span><span style="color: #2b91af">Dispatcher</span>();
dispatcher.Add&lt;<span style="color: #2b91af">GetPersonsRequest</span>&gt;(s =&gt;
s.ID = id); <span style="color: green">//disregard ugly cast; this is only for test
purposes </span><span style="color: blue">var </span>request = (<span style="color: #2b91af">GetPersonsRequest</span>)dispatcher.Requests.First();
request.ID.ShouldEqual(id); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>Where the Request and dispatcher have
these signatures: 
<br /><pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">GetPersonsRequest</span>:<span style="color: #2b91af">Request </span>{ <span style="color: blue">public </span><span style="color: #2b91af">Guid </span>ID
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } } <span style="color: blue">public
class </span><span style="color: #2b91af">Dispatcher </span>{ <span style="color: blue">public </span>Dispatcher()
{ Requests = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Request</span>&gt;();
} <span style="color: blue">public void </span>Add&lt;TRequestType&gt;(<span style="color: blue">params </span><span style="color: #2b91af">Func</span>&lt;TRequestType, <span style="color: blue">object</span>&gt;[]
funcs)<br /><span style="color: blue">where </span>TRequestType : <span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ <span style="color: blue">var </span>request = <span style="color: blue">new </span>TRequestType(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>func <span style="color: blue">in </span>funcs)
{ func(request); } Requests.Add(request); } <span style="color: blue">public </span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Request</span>&gt;
Requests { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} }</pre><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><p>
I can now set only property in my Request type, what if it has more properties that
need to be set? Just introduce the infamous params to the method signature like so:
</p><pre class="code"><span style="color: blue">public void </span>Add&lt;TRequestType&gt;(<span style="color: blue">params </span><span style="color: #2b91af">Func</span>&lt;TRequestType, <span style="color: blue">object</span>&gt;[]
funcs) 
<br /><span style="color: blue">where </span>TRequestType:<span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ <span style="color: blue">var </span>request = <span style="color: blue">new </span>TRequestType(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>func <span style="color: blue">in </span>funcs)
{ func(request); } Requests.Add(request); }</pre><p><a href="http://11011.net/software/vspaste"></a></p><p>
And again the test:
</p><pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_assign_two_values_using_a_func_in_a_generic_method_call() { <span style="color: blue">var </span>dispatcher
= <span style="color: blue">new </span><span style="color: #2b91af">Dispatcher</span>(); <span style="color: #2b91af">Guid </span>id
= <span style="color: #2b91af">Guid</span>.NewGuid(); dispatcher.Add&lt;<span style="color: #2b91af">GetPersonsRequest</span>&gt;(s
=&gt; s.ID = id, t=&gt; t.GetAllDetails = <span style="color: blue">true</span>); <span style="color: green">//disregard
ugly cast; this is only for test purposes </span><span style="color: blue">var </span>request
= (<span style="color: #2b91af">GetPersonsRequest</span>)dispatcher.Requests.First();
request.ID.ShouldEqual(id); request.GetAllDetails.ShouldBeTrue(); }</pre><a href="http://11011.net/software/vspaste"></a><p>
When we change the return type of the Add method to the dispatcher itself, we can
then use a fluent interface to get a very friendly syntax  for making calls:
</p><p>
 
</p><pre class="code"><span style="color: green">//input parameters for first request </span><span style="color: blue">var </span>document
= <span style="color: blue">new object</span>(); <span style="color: blue">var </span>userID
= <span style="color: #2b91af">Guid</span>.NewGuid(); <span style="color: green">//input
parameters for second request </span><span style="color: blue">var </span>dossierID
= <span style="color: #2b91af">Guid</span>.NewGuid(); dispatcher .Add&lt;<span style="color: #2b91af">SaveDocumentRequest</span>&gt;(s
=&gt; s.Document = document, t =&gt; t.UserID = userID) .Add&lt;<span style="color: #2b91af">GetZaakRequest</span>&gt;(s
=&gt; s.DossierId = dossierID);</pre><p>
I really like the fact that I don't have to initialize a variable and still have access
to all the properties in the request object..
</p><img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=da163853-8db7-45bf-a157-9c0058030aa3" /></body>
      <title>Functional programming with the Request/Response Service Layer</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingWithTheRequestResponseServiceLayer.aspx</link>
      <pubDate>Sat, 31 Oct 2009 16:54:24 GMT</pubDate>
      <description>&lt;p&gt;
At my current project we are using a dispatcher class to batch requests to our WCF
layer based on the &lt;a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/"&gt;Request/Response
Service Layer&lt;/a&gt; created by Davy Brion. Once you call the Get&amp;lt;TResponseType&amp;gt;()
on the dispatcher the WCF layer starts working on the requests one by one and return
them in one roundtrip.
&lt;/p&gt;
&lt;p&gt;
Adding requests to the dispatcher is easy:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;saveDocumentRequest = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SaveDocumentRequest&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;getRemainingDocuments
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GetRemainingDocumentsRequest&lt;/span&gt;();
dispatcher.Add(saveDocumentRequest, getRemainingDocuments);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Where the signature of the Add method in Dispatcher is like;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Add(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;[]
requests)&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Although this syntax is not bad, it feels like so much like code from last month:
not very sexy. Wouldn't be more fun if we could spice it up a bit using generics and
a bit of Func?
&lt;/p&gt;
&lt;p&gt;
I'd love to get rid of instantiating a variable just for the sake of adding it to
my dispatcher. After a bit of fiddling around with my unit tests I came up with the
following solution.
&lt;/p&gt;
&lt;p&gt;
What I want is to call the Add method in a generic way and set the parameters on my
request object inline, using a Func. This is best illustrated with a test:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_assign_value_using_a_func_in_a_generic_method_call() { &lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;id
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); &lt;span style="color: blue"&gt;var &lt;/span&gt;dispatcher
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dispatcher&lt;/span&gt;();
dispatcher.Add&amp;lt;&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;&amp;gt;(s =&amp;gt;
s.ID = id); &lt;span style="color: green"&gt;//disregard ugly cast; this is only for test
purposes &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;request = (&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;)dispatcher.Requests.First();
request.ID.ShouldEqual(id); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Where the Request and dispatcher have
these signatures: 
&lt;br /&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;:&lt;span style="color: #2b91af"&gt;Request &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;ID
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } } &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dispatcher &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;Dispatcher()
{ Requests = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;&amp;gt;();
} &lt;span style="color: blue"&gt;public void &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TRequestType, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;[]
funcs)&lt;br /&gt;
&lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType : &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;func &lt;span style="color: blue"&gt;in &lt;/span&gt;funcs)
{ func(request); } Requests.Add(request); } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;&amp;gt;
Requests { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I can now set only property in my Request type, what if it has more properties that
need to be set? Just introduce the infamous params to the method signature like so:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TRequestType, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;[]
funcs) 
&lt;br /&gt;
&lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType:&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;func &lt;span style="color: blue"&gt;in &lt;/span&gt;funcs)
{ func(request); } Requests.Add(request); }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
And again the test:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_assign_two_values_using_a_func_in_a_generic_method_call() { &lt;span style="color: blue"&gt;var &lt;/span&gt;dispatcher
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dispatcher&lt;/span&gt;(); &lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;id
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); dispatcher.Add&amp;lt;&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;&amp;gt;(s
=&amp;gt; s.ID = id, t=&amp;gt; t.GetAllDetails = &lt;span style="color: blue"&gt;true&lt;/span&gt;); &lt;span style="color: green"&gt;//disregard
ugly cast; this is only for test purposes &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;request
= (&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;)dispatcher.Requests.First();
request.ID.ShouldEqual(id); request.GetAllDetails.ShouldBeTrue(); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
When we change the return type of the Add method to the dispatcher itself, we can
then use a fluent interface to get a very friendly syntax&amp;#160; for making calls:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green"&gt;//input parameters for first request &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;document
= &lt;span style="color: blue"&gt;new object&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;userID
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); &lt;span style="color: green"&gt;//input
parameters for second request &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;dossierID
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); dispatcher .Add&amp;lt;&lt;span style="color: #2b91af"&gt;SaveDocumentRequest&lt;/span&gt;&amp;gt;(s
=&amp;gt; s.Document = document, t =&amp;gt; t.UserID = userID) .Add&amp;lt;&lt;span style="color: #2b91af"&gt;GetZaakRequest&lt;/span&gt;&amp;gt;(s
=&amp;gt; s.DossierId = dossierID);&lt;/pre&gt;
&lt;p&gt;
I really like the fact that I don't have to initialize a variable and still have access
to all the properties in the request object..
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=da163853-8db7-45bf-a157-9c0058030aa3" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</comments>
      <category>.NET</category>
      <category>Functional</category>
      <category>Linq</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=4e1908d7-da65-4aea-bdb8-85b7e6d1c079</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,4e1908d7-da65-4aea-bdb8-85b7e6d1c079.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,4e1908d7-da65-4aea-bdb8-85b7e6d1c079.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=4e1908d7-da65-4aea-bdb8-85b7e6d1c079</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm always trying to improve on my speed in development. Yesterday I noticed Udi Dahan
using the In-proc test feature of TestDriven.Net in one of his presentations at the
SDC - Netherlands, like so:
</p>
        <p>
 
</p>
        <p>
          <a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_4.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_thumb_1.png" width="333" height="192" />
          </a>
        </p>
        <p>
 
</p>
        <p>
 
</p>
        <p>
I had seen this option before but as it was hidden in some submenu and the regular
test method worked just fine, there seemed no benefit in using it.
</p>
        <p>
 
</p>
        <p>
          <strong>However.</strong>
        </p>
        <p>
Running your tests using In-proc makes for about 25% speed difference on average!
To me, this is very noticeable but the function is hidden in some context submenu,
hence still slow to use. After a bit of emailing with TestDriven.Net, it appears that
you can run In-proc using the DTE runner.
</p>
        <p>
To map it to a shortcut key, use the following screenshot:
</p>
        <p>
 
</p>
        <p>
          <a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_2.png">
            <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://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_thumb.png" width="680" height="396" />
          </a>
        </p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=4e1908d7-da65-4aea-bdb8-85b7e6d1c079" />
      </body>
      <title>Faster running unit tests</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,4e1908d7-da65-4aea-bdb8-85b7e6d1c079.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/20/FasterRunningUnitTests.aspx</link>
      <pubDate>Tue, 20 Oct 2009 16:15:40 GMT</pubDate>
      <description>&lt;p&gt;
I'm always trying to improve on my speed in development. Yesterday I noticed Udi Dahan
using the In-proc test feature of TestDriven.Net in one of his presentations at the
SDC - Netherlands, like so:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_thumb_1.png" width="333" height="192" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
I had seen this option before but as it was hidden in some submenu and the regular
test method worked just fine, there seemed no benefit in using it.
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;However.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Running your tests using In-proc makes for about 25% speed difference on average!
To me, this is very noticeable but the function is hidden in some context submenu,
hence still slow to use. After a bit of emailing with TestDriven.Net, it appears that
you can run In-proc using the DTE runner.
&lt;/p&gt;
&lt;p&gt;
To map it to a shortcut key, use the following screenshot:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_2.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://www.tjsolutions.nl/content/binary/WindowsLiveWriter/Fasterrunningunittests_100CC/image_thumb.png" width="680" height="396" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=4e1908d7-da65-4aea-bdb8-85b7e6d1c079" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,4e1908d7-da65-4aea-bdb8-85b7e6d1c079.aspx</comments>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=f90776ac-a285-44a7-aa45-01782a58b52e</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,f90776ac-a285-44a7-aa45-01782a58b52e.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,f90776ac-a285-44a7-aa45-01782a58b52e.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=f90776ac-a285-44a7-aa45-01782a58b52e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have converted quite a few to Resharper recently. I'm quite the addict myself and
I can't see myself using Visual Studio productively without it anymore. 
</p>
        <p>
Resharper does have a learning curve however since there are just because of the sheer
amount of enhancements it brings to working with VS.
</p>
        <p>
 
</p>
        <p>
To get you up to speed using Resharper, perform these settings in Visual studio after
installing: 
<br />
Within Visual studio: ALT - R - O =&gt; intellisense, completion behavior =&gt; 
<br />
Automatically show completion list in <b>0 </b>milliseconds 
<br />
"Check" all options except Case-sensitive prefix matching 
<br />
Finally, "Only those that are at least": set this to 1
</p>
        <p>
 
</p>
        <p>
You can watch the following screencasts: 
<br />
- <a href="http://www.jameskovacs.com/blog/CategoryView.aspx?category=Screencast" target="_blank">James
Kovacs</a> screencasts on Resharper 
<br />
- My own <a href=" http://cid-b81b6d38a9d75bc1.skydrive.live.com/self.aspx/TDD/TDD%20intro%20met%20MVP.avi" target="_blank">MVP
coding demo</a> using Resharper and Rhino mocks 
<br />
- <a href="http://www.ayende.com/102/section.aspx/redirect/1" target="_blank">Ayende</a> demoing
Rhino mocks
</p>
        <p>
(pls note that you'll need the <a href="http://www.techsmith.com/download/codecs.asp">Camtasia
TSCC codec</a> to view the last two)
</p>
        <p>
 
</p>
        <p>
You can use <a href="http://osherove.com/tools/" target="_blank">keyjedi</a> to display
the shortcuts used when someone else is looking over your shoulder. This will greatly
enhance the introduction within your team. This only works on x86 systems, I have
compiled the code into an x64 assembly that works on my win7 machine, let me know
if you need it.
</p>
        <p>
Lastly, have the developers print out the <a href="http://www.jetbrains.com/resharper/docs/ReSharper40DefaultKeymap.pdf">Resharper
keymapping</a> (in plastic cover preferably, in case of any coffee, tea and all that
:-) )
</p>
        <p>
 
</p>
        <p>
If you have any further questions, please let me know.
</p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=f90776ac-a285-44a7-aa45-01782a58b52e" />
      </body>
      <title>How to get started with Resharper quickly</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,f90776ac-a285-44a7-aa45-01782a58b52e.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/20/HowToGetStartedWithResharperQuickly.aspx</link>
      <pubDate>Tue, 20 Oct 2009 12:23:41 GMT</pubDate>
      <description>&lt;p&gt;
I have converted quite a few to Resharper recently. I'm quite the addict myself and
I can't see myself using Visual Studio productively without it anymore. 
&lt;/p&gt;
&lt;p&gt;
Resharper does have a learning curve however since there are just because of the sheer
amount of enhancements it brings to working with VS.
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
To get you up to speed using Resharper, perform these settings in Visual studio after
installing: 
&lt;br /&gt;
Within Visual studio: ALT - R - O =&amp;gt; intellisense, completion behavior =&amp;gt; 
&lt;br /&gt;
Automatically show completion list in &lt;b&gt;0 &lt;/b&gt;milliseconds 
&lt;br /&gt;
"Check" all options except Case-sensitive prefix matching 
&lt;br /&gt;
Finally, "Only those that are at least": set this to 1
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
You can watch the following screencasts: 
&lt;br /&gt;
- &lt;a href="http://www.jameskovacs.com/blog/CategoryView.aspx?category=Screencast" target="_blank"&gt;James
Kovacs&lt;/a&gt; screencasts on Resharper 
&lt;br /&gt;
- My own &lt;a href=" http://cid-b81b6d38a9d75bc1.skydrive.live.com/self.aspx/TDD/TDD%20intro%20met%20MVP.avi" target="_blank"&gt;MVP
coding demo&lt;/a&gt; using Resharper and Rhino mocks 
&lt;br /&gt;
- &lt;a href="http://www.ayende.com/102/section.aspx/redirect/1" target="_blank"&gt;Ayende&lt;/a&gt; demoing
Rhino mocks
&lt;/p&gt;
&lt;p&gt;
(pls note that you'll need the &lt;a href="http://www.techsmith.com/download/codecs.asp"&gt;Camtasia
TSCC codec&lt;/a&gt; to view the last two)
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
You can use &lt;a href="http://osherove.com/tools/" target="_blank"&gt;keyjedi&lt;/a&gt; to display
the shortcuts used when someone else is looking over your shoulder. This will greatly
enhance the introduction within your team. This only works on x86 systems, I have
compiled the code into an x64 assembly that works on my win7 machine, let me know
if you need it.
&lt;/p&gt;
&lt;p&gt;
Lastly, have the developers print out the &lt;a href="http://www.jetbrains.com/resharper/docs/ReSharper40DefaultKeymap.pdf"&gt;Resharper
keymapping&lt;/a&gt; (in plastic cover preferably, in case of any coffee, tea and all that
:-) )
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
If you have any further questions, please let me know.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=f90776ac-a285-44a7-aa45-01782a58b52e" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,f90776ac-a285-44a7-aa45-01782a58b52e.aspx</comments>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=791a33b2-d3cf-4bcb-94a3-af0bccf229b2</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,791a33b2-d3cf-4bcb-94a3-af0bccf229b2.aspx</pingback:target>
      <dc:creator>Tijmen</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,791a33b2-d3cf-4bcb-94a3-af0bccf229b2.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=791a33b2-d3cf-4bcb-94a3-af0bccf229b2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of my machines refused to install updates to the .NET Framework 3.5 sp1, both
through auto-update and when installing manually. I finally fixed it by using Aaron
Stebner's <a href="http://blogs.msdn.com/astebner/archive/2009/07/27/9850215.aspx" target="_blank">.NET
framework cleanup tool</a> and re-installing .NET. This tool radically removes all
versions of the .NET framework, including IIS artefacts, registry entries, the works.
I found this tool through a post on <a href="http://www.raymond.cc/blog/archives/2008/12/02/uninstall-microsoft-net-framework-with-aaron-stebner-cleanup-tool/" target="_blank">raymond.cc</a>.
</p>
        <p>
Note that this tool should probably not be the first thing to try; uninstalling and
re-installing the .NET framework versions would probably a safer bet to try first.
</p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=791a33b2-d3cf-4bcb-94a3-af0bccf229b2" />
      </body>
      <title>.NET framework cleanup tool</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,791a33b2-d3cf-4bcb-94a3-af0bccf229b2.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/15/NETFrameworkCleanupTool.aspx</link>
      <pubDate>Thu, 15 Oct 2009 07:53:47 GMT</pubDate>
      <description>&lt;p&gt;
One of my machines refused to install updates to the .NET Framework 3.5 sp1, both
through auto-update and when installing manually. I finally fixed it by using Aaron
Stebner's &lt;a href="http://blogs.msdn.com/astebner/archive/2009/07/27/9850215.aspx" target="_blank"&gt;.NET
framework cleanup tool&lt;/a&gt; and re-installing .NET. This tool radically removes all
versions of the .NET framework, including IIS artefacts, registry entries, the works.
I found this tool through a post on &lt;a href="http://www.raymond.cc/blog/archives/2008/12/02/uninstall-microsoft-net-framework-with-aaron-stebner-cleanup-tool/" target="_blank"&gt;raymond.cc&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Note that this tool should probably not be the first thing to try; uninstalling and
re-installing the .NET framework versions would probably a safer bet to try first.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=791a33b2-d3cf-4bcb-94a3-af0bccf229b2" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,791a33b2-d3cf-4bcb-94a3-af0bccf229b2.aspx</comments>
      <category>Tools</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=19fd90fc-5243-427f-9875-51e90003dc22</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</pingback:target>
      <dc:creator>Tijmen</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=19fd90fc-5243-427f-9875-51e90003dc22</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Just a quick note: there is an update out
for the latest .NET framework version, that addresses issues with ASP.NET dynamic
data. Amongst others, the site will not display foreign key fields in a one-to-one
relationship. This <a href="http://support.microsoft.com/kb/959209">KB article</a> contains
all the download links to the various versions.<br />
Not a new issue by any means (the fix is dated January 2009), but good to know nonetheless.<br /><p></p><img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=19fd90fc-5243-427f-9875-51e90003dc22" /></body>
      <title>Update to .NET 3.5 sp1 required for dynamic data</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/01/UpdateToNET35Sp1RequiredForDynamicData.aspx</link>
      <pubDate>Thu, 01 Oct 2009 17:36:27 GMT</pubDate>
      <description>Just a quick note: there is an update out for the latest .NET framework version, that addresses issues with ASP.NET dynamic data. Amongst others, the site will not display foreign key fields in a one-to-one relationship. This &lt;a href="http://support.microsoft.com/kb/959209"&gt;KB
article&lt;/a&gt; contains all the download links to the various versions.&lt;br&gt;
Not a new issue by any means (the fix is dated January 2009), but good to know nonetheless.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=19fd90fc-5243-427f-9875-51e90003dc22" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</comments>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=eedd4d22-2ac2-4e85-b62b-626cea249eb2</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,eedd4d22-2ac2-4e85-b62b-626cea249eb2.aspx</pingback:target>
      <dc:creator>Tijmen</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,eedd4d22-2ac2-4e85-b62b-626cea249eb2.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=eedd4d22-2ac2-4e85-b62b-626cea249eb2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Nothing much to, just that it took me some time to figure it out today. The code below
tests whether DivisionID is actually set in the presenter.
</p>
        <p>
 
</p>
        <pre class="code">
          <span style="color: blue;">public class </span>
          <span style="color: rgb(43, 145, 175);">Presenter </span>{ <span style="color: blue;">readonly </span><span style="color: rgb(43, 145, 175);">IRepository </span>repository; <span style="color: blue;">public </span>Presenter(<span style="color: rgb(43, 145, 175);">IRepository </span>repository)
{ <span style="color: blue;">this</span>.repository = repository; } <span style="color: blue;">public </span><span style="color: rgb(43, 145, 175);">IView </span>View
{ <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; } <span style="color: blue;">public
void </span>GetDivision() { View.DivisionID = repository.GetDivisionID(); } } <span style="color: blue;">public
interface </span><span style="color: rgb(43, 145, 175);">IView </span>{ <span style="color: blue;">int </span>DivisionID
{ <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
} [<span style="color: rgb(43, 145, 175);">TestFixture</span>] <span style="color: blue;">public
class </span><span style="color: rgb(43, 145, 175);">PresenterTests </span>{ <span style="color: rgb(43, 145, 175);">Presenter </span>sut; <span style="color: rgb(43, 145, 175);">IView </span>view; <span style="color: rgb(43, 145, 175);">IRepository </span>repository;
[<span style="color: rgb(43, 145, 175);">SetUp</span>] <span style="color: blue;">public
void </span>SetUp() { view = <span style="color: rgb(43, 145, 175);">MockRepository</span>.GenerateMock&lt;<span style="color: rgb(43, 145, 175);">IView</span>&gt;();
repository = <span style="color: rgb(43, 145, 175);">MockRepository</span>.GenerateMock&lt;<span style="color: rgb(43, 145, 175);">IRepository</span>&gt;();
sut = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">Presenter</span>(repository){View
= view}; } [<span style="color: rgb(43, 145, 175);">Test</span>] <span style="color: blue;">public
void </span>when_on_the_sut_GetDivisionID_is_called_it_should_set_the_divisionID_on_the_view()
{ <span style="color: green;">//AAA mocking style //Arrange </span><span style="color: blue;">const
int </span>divisionID = 1; repository.Stub(s =&gt; s.GetDivisionID()).Return(divisionID); <span style="color: green;">//note:
the expectation is set when DivisionID is set to divisionID </span>view.Expect(s =&gt;
s.DivisionID = divisionID); <span style="color: green;">//Act </span>sut.GetDivision(); <span style="color: green;">//Assert </span>view.VerifyAllExpectations();
} }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eedd4d22-2ac2-4e85-b62b-626cea249eb2" />
      </body>
      <title>RhinoMocks - set expectations on setter for automatic properties</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,eedd4d22-2ac2-4e85-b62b-626cea249eb2.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/09/16/RhinoMocksSetExpectationsOnSetterForAutomaticProperties.aspx</link>
      <pubDate>Wed, 16 Sep 2009 21:00:20 GMT</pubDate>
      <description>&lt;p&gt;
Nothing much to, just that it took me some time to figure it out today. The code below
tests whether DivisionID is actually set in the presenter.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Presenter &lt;/span&gt;{ &lt;span style="color: blue;"&gt;readonly &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IRepository &lt;/span&gt;repository; &lt;span style="color: blue;"&gt;public &lt;/span&gt;Presenter(&lt;span style="color: rgb(43, 145, 175);"&gt;IRepository &lt;/span&gt;repository)
{ &lt;span style="color: blue;"&gt;this&lt;/span&gt;.repository = repository; } &lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IView &lt;/span&gt;View
{ &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; } &lt;span style="color: blue;"&gt;public
void &lt;/span&gt;GetDivision() { View.DivisionID = repository.GetDivisionID(); } } &lt;span style="color: blue;"&gt;public
interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IView &lt;/span&gt;{ &lt;span style="color: blue;"&gt;int &lt;/span&gt;DivisionID
{ &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }
} [&lt;span style="color: rgb(43, 145, 175);"&gt;TestFixture&lt;/span&gt;] &lt;span style="color: blue;"&gt;public
class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;PresenterTests &lt;/span&gt;{ &lt;span style="color: rgb(43, 145, 175);"&gt;Presenter &lt;/span&gt;sut; &lt;span style="color: rgb(43, 145, 175);"&gt;IView &lt;/span&gt;view; &lt;span style="color: rgb(43, 145, 175);"&gt;IRepository &lt;/span&gt;repository;
[&lt;span style="color: rgb(43, 145, 175);"&gt;SetUp&lt;/span&gt;] &lt;span style="color: blue;"&gt;public
void &lt;/span&gt;SetUp() { view = &lt;span style="color: rgb(43, 145, 175);"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IView&lt;/span&gt;&amp;gt;();
repository = &lt;span style="color: rgb(43, 145, 175);"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IRepository&lt;/span&gt;&amp;gt;();
sut = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Presenter&lt;/span&gt;(repository){View
= view}; } [&lt;span style="color: rgb(43, 145, 175);"&gt;Test&lt;/span&gt;] &lt;span style="color: blue;"&gt;public
void &lt;/span&gt;when_on_the_sut_GetDivisionID_is_called_it_should_set_the_divisionID_on_the_view()
{ &lt;span style="color: green;"&gt;//AAA mocking style //Arrange &lt;/span&gt;&lt;span style="color: blue;"&gt;const
int &lt;/span&gt;divisionID = 1; repository.Stub(s =&amp;gt; s.GetDivisionID()).Return(divisionID); &lt;span style="color: green;"&gt;//note:
the expectation is set when DivisionID is set to divisionID &lt;/span&gt;view.Expect(s =&amp;gt;
s.DivisionID = divisionID); &lt;span style="color: green;"&gt;//Act &lt;/span&gt;sut.GetDivision(); &lt;span style="color: green;"&gt;//Assert &lt;/span&gt;view.VerifyAllExpectations();
} }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eedd4d22-2ac2-4e85-b62b-626cea249eb2" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,eedd4d22-2ac2-4e85-b62b-626cea249eb2.aspx</comments>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=d5fbb2c1-e30f-42b1-9abb-3706503c33e2</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,d5fbb2c1-e30f-42b1-9abb-3706503c33e2.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,d5fbb2c1-e30f-42b1-9abb-3706503c33e2.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=d5fbb2c1-e30f-42b1-9abb-3706503c33e2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is the third posting in the <a href="http://www.tjsolutions.nl/2009/06/01/AnnouncingTheSeriesWorkingWithLegacyCode.aspx" target="_blank">WEWLC-series</a>. 
</p>
        <p>
 
</p>
        <p>
Other posts are:
</p>
        <p>
          <a href="http://www.tjsolutions.nl/2009/09/14/WEWLCICantGetThisClassIntoATestHarness.aspx" target="_blank">I
Can't Get This Class into a Test Harness</a>
        </p>
        <p>
          <a href="http://www.tjsolutions.nl/2009/09/12/WEWLCIDontHaveMuchTimeAndIHaveToChangeIt.aspx" target="_blank">I
don't have much time and I have to change it</a>
        </p>
        <p>
          <a href="http://www.tjsolutions.nl/2009/09/12/WEWLCAssumptions.aspx" target="_blank">Assumptions
on the reader</a>
        </p>
        <p>
 
</p>
        <p>
"Getting tests in place to make changes can be a bit of a problem. If you can instantiate
your class separately in a test harness, consider yourself lucky." 
</p>
        <p>
Instantiating a class should not be hard, if you do have issues have a look at <a href="http://www.tjsolutions.nl/2009/09/14/WEWLCICantGetThisClassIntoATestHarness.aspx" target="_blank">I
Can't Get This Class into a Test Harness</a>. That should take care of most of your
problems. 
</p>
        <p>
But instantiating is often just the first part of all the hurdles to take. The second
part is writing a test for the methods inside. 
</p>
        <p>
 
</p>
        <p>
Fortunately, in most cases, the amount of work that we have to do to write tests for
methods isn't as drastic. Here are some of the problems that we can run into.<a name="ch10index03"></a><a name="ch10index04"></a><a name="ch10index05"></a></p>
        <ul>
          <li>
            <p>
The method may be private
</p>
          </li>
          <li>
            <p>
The method may be hard to call because it has parameters that are hard to construct
</p>
          </li>
          <li>
            <p>
The method might have bad side effects (modifying a database, writing to the file
system etc)
</p>
          </li>
        </ul>
        <p>
          <strong>
          </strong>
        </p>
        <p>
          <strong>The Case of the Hidden Method</strong>
        </p>
        <p>
If we need to write a test for a private method we need to make it public. There are
some tricks in getting visibility on the method in your tests but I don't recommend
it. First of all, if there is a need for private method odds are that what you need
is a another collaborator. 
</p>
        <p>
This is of course a rather fundamental discussion. If you do care about keeping the
method invisible to callers you can make it protected. Once it is protected you can
derive a test class and add a public method that will simply call the protected method
in the base class.
</p>
        <p>
In most cases I would recommend moving the method to a separate class and inject it
into the constructor of the class that interacts with it. Once there is a need to
alter the behavior of a private method it is obviously doing something important,
hence, it should be under test.
</p>
        <p>
 
</p>
        <p>
          <strong>The Case of the "Helpful" Language Feature </strong>
        </p>
        <p>
Michael Feather is referring to sealed classes that don't have constructor. How do
you test interaction code that takes such a class as a parameter in a method? Let's
have a look at an example:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">StreamHelper </span>{ <span style="color: blue">const
int </span>MIN_LEN = 4; <span style="color: blue">public </span><span style="color: #2b91af">IList </span>GetStreams(<span style="color: #2b91af">HttpFileCollection </span>files)
{ <span style="color: blue">var </span>list = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Stream</span>&gt;(); <span style="color: blue">foreach </span>(<span style="color: blue">string </span>name <span style="color: blue">in </span>files)
{ <span style="color: #2b91af">HttpPostedFile </span>file = files[name]; <span style="color: blue">if </span>((file.FileName.EndsWith(<span style="color: #a31515">".txt"</span>)
&amp;&amp; file.ContentLength &gt; MIN_LEN)) { list.Add(file.InputStream); } } <span style="color: blue">return </span>list;
} }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
According to <a href="http://msdn.microsoft.com/en-us/library/system.web.httpfilecollection.aspx" target="_blank">MSDN</a>,
the HttpFileCollection provides access to and organizes files uploaded by a client.
Since this class is sealed and only has an internal constructor, how can you test
this method?
</p>
        <p>
Turns out that HttpFileCollection derives from NameObjectCollectionBase which has
a parameterless constructor which is protected. If we thus derive from NameObjectCollectionBase,
implement the indexer required and change the signature of the GetStreams method a
bit to take our custom class instead of the HttpFileCollection we should be able to
get going. 
</p>
        <p>
 
</p>
        <p>
Please note, this is going to be a hefty refactoring. Right, lets get down to it.
First let's create a fixture for our tests:
</p>
        <pre class="code">[<span style="color: #2b91af">TestFixture</span>] <span style="color: blue">public
class </span><span style="color: #2b91af">HttpCollectionTests </span>{ [<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_instantiate_a_derived_class_from_NameObjectCollectionBase() { <span style="color: blue">var </span>customHttpFileCollection
= <span style="color: blue">new </span><span style="color: #2b91af">CustomHttpFileCollection</span>(); <span style="color: blue">var </span>collectionBase
= customHttpFileCollection <span style="color: blue">as </span><span style="color: #2b91af">NameObjectCollectionBase</span>;
collectionBase.ShouldNotBeNull(); } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <p>
This means deriving from NameObjectCollectionBase
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">CustomHttpFileCollection </span>: <span style="color: #2b91af">NameObjectCollectionBase </span>{
}</pre>
        <p>
The next test is to be able to call GetStreams() in StreamHelper. Rather than changing
the existing method we are going to create a new method with the same name, but different
signature so that we can call it with our new CustomerHttpFileCollection:
</p>
        <pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_use_the_custom_class() { <span style="color: blue">var </span>collection
= <span style="color: blue">new </span><span style="color: #2b91af">CustomHttpFileCollection</span>(); <span style="color: blue">new </span><span style="color: #2b91af">StreamHelper</span>().GetStreams(collection);
}</pre>
        <pre class="code">
          <span style="color: blue">public </span>
          <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Stream</span>&gt;
GetStreams(<span style="color: #2b91af">CustomHttpFileCollection </span>files) { <span style="color: blue">var </span>list
= <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Stream</span>&gt;(); <span style="color: blue">foreach </span>(<span style="color: blue">string </span>name <span style="color: blue">in </span>files)
{ <span style="color: #2b91af">HttpPostedFile </span>file = files[name]; <span style="color: blue">if </span>((file.FileName.EndsWith(<span style="color: #a31515">".txt"</span>)
&amp;&amp; file.ContentLength &gt; MIN_LEN)) { list.Add(file.InputStream); } } <span style="color: blue">return </span>list;
}</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>This means implementing the indexer
used in GetStreams() in our custom class. Luckily Resharper knows what to do and we
can use ALT-ENTER to generate the code. Next to the indexer we will also need an IList
to store the files and a method to add them to the internal collection:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">CustomHttpFileCollection </span>: <span style="color: #2b91af">NameObjectCollectionBase </span>{ <span style="color: blue">public </span><span style="color: #2b91af">HttpPostedFile </span><span style="color: blue">this</span>[<span style="color: blue">string </span>name]
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>httpPostedFiles.Where(s
=&gt; s.FileName == name).FirstOrDefault(); } } }</pre>
        <p>
This poses a bit of an issue; we can instantiate a HttpPostedFile so how are we going
to populate an internal collection? Since this one is sealed, does not have a suitable
base class and has no constructors we need to be creative. We need a custom implementation
of the HttpPostedFile. First we create an interface, then define the same members
on the interface as are in the HttpPostedFile class. Once that is done we can change
the signature of the indexer to IHttpPostedFile and add some logic to store and add
the files to the collection class, like so:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">CustomHttpFileCollection </span>: <span style="color: #2b91af">NameObjectCollectionBase </span>{ <span style="color: blue">readonly </span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">IHttpPostedFile</span>&gt;
httpPostedFiles = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">IHttpPostedFile</span>&gt;(); <span style="color: blue">public </span><span style="color: #2b91af">IHttpPostedFile </span><span style="color: blue">this</span>[<span style="color: blue">string </span>name]
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>httpPostedFiles.Where(s
=&gt; s.FileName == name).FirstOrDefault(); } } <span style="color: blue">public void </span>AddHttpPostedFile(<span style="color: #2b91af">IHttpPostedFile </span>file)
{ httpPostedFiles.Add(file); } }</pre>
        <pre class="code">
          <span style="color: blue">public interface </span>
          <span style="color: #2b91af">IHttpPostedFile </span>{ <span style="color: blue">int </span>ContentLength
{ <span style="color: blue">get</span>; } <span style="color: blue">string </span>ContentType
{ <span style="color: blue">get</span>; } <span style="color: blue">string </span>FileName
{ <span style="color: blue">get</span>; } <span style="color: #2b91af">Stream </span>InputStream
{ <span style="color: blue">get</span>; } }</pre>
        <p>
Change the HttpPostedFile to the new IHttpPostedFile in GetStreams():
</p>
        <pre class="code">
          <span style="color: blue">public </span>
          <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Stream</span>&gt;
GetStreams(<span style="color: #2b91af">CustomHttpFileCollection </span>files) { <span style="color: blue">var </span>list
= <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Stream</span>&gt;(); <span style="color: blue">foreach </span>(<span style="color: blue">string </span>name <span style="color: blue">in </span>files)
{ <span style="color: #2b91af">IHttpPostedFile </span>file = files[name]; <span style="color: blue">if </span>((file.FileName.EndsWith(<span style="color: #a31515">".txt"</span>)
&amp;&amp; file.ContentLength &gt; MIN_LEN)) { list.Add(file.InputStream); } } <span style="color: blue">return </span>list;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre class="code"> </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Now that we have set up the collection, let's write a test to see if it all works
</p>
        <pre class="code">
          <span style="color: #2b91af">Stream </span>GetStream() { <span style="color: blue">const
string </span>s = <span style="color: #a31515">"hello world"</span>; <span style="color: blue">var </span>memoryStream
= <span style="color: blue">new </span><span style="color: #2b91af">MemoryStream</span>(System.Text.<span style="color: #2b91af">Encoding</span>.Default.GetBytes(s)); <span style="color: blue">return </span>memoryStream;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_use_the_customcollection_to_test_the_StreamHelper_class() { <span style="color: blue">var </span>collection
= <span style="color: blue">new </span><span style="color: #2b91af">CustomHttpFileCollection</span>(); <span style="color: blue">var </span>httpPostedFile
= <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">IHttpPostedFile</span>&gt;();
httpPostedFile.Stub(s =&gt; s.InputStream).Return(GetStream()); httpPostedFile.Stub(s
=&gt; s.FileName).Return(<span style="color: #a31515">"somefilename.txt"</span>);
httpPostedFile.Stub(s =&gt; s.ContentLength).Return(5); collection.AddHttpPostedFile(httpPostedFile); <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Stream</span>&gt;
streams = <span style="color: blue">new </span><span style="color: #2b91af">StreamHelper</span>().GetStreams(collection);
streams.Count.ShouldEqual(1); }</pre>
        <p>
When we execute this test a lot is happening. First I create a mock of the IHttpPostedFile
interface so that I can stub the properties. This allows me to test the method without
having an actual implementation of the interface. Not that it would have complicated
things tremendously, but it's a nice technique.
</p>
        <p>
I also use a convenient helper method to get me a stream. This will come in handy
later when I want to verify the outcome of the GetStreams() method.
</p>
        <p>
Let's execute and:
</p>
        <p>
 
</p>
        <p>
------ Test started: Assembly: Koning.WEWLC.Tests.dll ------ 
</p>
        <p>
TestCase 'Koning.WEWLC.Tests.HttpCollectionTests.can_use_the_customcollection_to_test_the_StreamHelper_class' 
<br />
failed: 
<br />
  Expected: 1 
<br />
  But was:  0 
<br />
    at Machine.Specifications.NUnitShouldExtensionMethods.ShouldEqual(Object
actual, Object expected) 
<br />
    C:\Dev\Personal\WEWLC\Koning.WEWLC\Koning.WEWLC.Tests\Tests\HttpCollectionTests.cs(59,0):
at Koning.WEWLC.Tests.HttpCollectionTests.can_use_the_customcollection_to_test_the_StreamHelper_class() 
</p>
        <p>
0 passed, 1 failed, 0 skipped, took 1.37 seconds. 
</p>
        <p>
 
</p>
        <p>
Why is this happening? My bad, I didn't implement GetEnumerator() on the custom collection
class. This is easily fixed however:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">CustomHttpFileCollection </span>: <span style="color: #2b91af">NameObjectCollectionBase</span>, <span style="color: #2b91af">IEnumerable </span>{ <span style="color: blue">readonly </span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">IHttpPostedFile</span>&gt;
httpPostedFiles = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">IHttpPostedFile</span>&gt;(); <span style="color: blue">public </span><span style="color: #2b91af">IHttpPostedFile </span><span style="color: blue">this</span>[<span style="color: blue">string </span>name]
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>httpPostedFiles.Where(s
=&gt; s.FileName == name).FirstOrDefault(); } } <span style="color: blue">public new </span><span style="color: #2b91af">IEnumerator</span>&lt;<span style="color: blue">string</span>&gt;
GetEnumerator() { <span style="color: blue">return </span>httpPostedFiles.Select(s
=&gt; s.FileName).GetEnumerator(); } <span style="color: #2b91af">IEnumerator IEnumerable</span>.GetEnumerator()
{ <span style="color: blue">return </span>GetEnumerator(); } <span style="color: blue">public
void </span>AddHttpPostedFile(<span style="color: #2b91af">IHttpPostedFile </span>file)
{ httpPostedFiles.Add(file); } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I'm using some Linq goodness to get to the filenames and iterate over that. Next up
is refining our test and run it again:
</p>
        <pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_use_the_customcollection_to_test_the_StreamHelper_class() { <span style="color: blue">var </span>collection
= <span style="color: blue">new </span><span style="color: #2b91af">CustomHttpFileCollection</span>(); <span style="color: blue">var </span>httpPostedFile
= <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">IHttpPostedFile</span>&gt;();
httpPostedFile.Stub(s =&gt; s.InputStream).Return(GetStream()); httpPostedFile.Stub(s
=&gt; s.FileName).Return(<span style="color: #a31515">"somefilename.txt"</span>);
httpPostedFile.Stub(s =&gt; s.ContentLength).Return(5); collection.AddHttpPostedFile(httpPostedFile); <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Stream</span>&gt;
streams = <span style="color: blue">new </span><span style="color: #2b91af">StreamHelper</span>().GetStreams(collection);
streams.Count.ShouldEqual(1); streams.First().Length.ShouldBeGreaterThan(0); <span style="color: blue">new </span><span style="color: #2b91af">StreamReader</span>(streams.First()).ReadLine().ShouldContain(<span style="color: #a31515">"hello
world"</span>); }</pre>
        <p>
------ Test started: Assembly: Koning.WEWLC.Tests.dll ------ 
</p>
        <p>
1 passed, 0 failed, 0 skipped, took 1.48 seconds. 
</p>
        <p>
 
</p>
        <p>
Now note that when using the GetStreams() method one would have to transform the items
in HttpPostedFileCollection to the IHttpPostedFile interface before using it. 
</p>
        <p>
 
</p>
        <p>
 
</p>
        <p>
          <strong>The Case of the Undetectable Side Effect</strong>
        </p>
        <p>
Every so often you encounter code that just makes you mumble WTF way more than is
healthy for any sane developer. In most cases things just happen everywhere. When
the code is performing a query, it's also updating some other pieces, altering the
file system a bit and lastly setting a whole bunch of other objects in an invalid
state.
</p>
        <p>
I wouldn't recommend getting the debugger up and running to see what's going on. Most
of the time it consumes a lot of time and keeps the frustration really high.
</p>
        <p>
What this kind of code is lacking is proper Command Query Separation (CQS). From the
WEWLC book "It's a is a design principle first described by Bertrand Meyer. Simply
put, it is this: A method should be a command or a query, but not both. A command
is a method that can modify the state of the object but that doesn't return a value.
A query is a method that returns a value but that does not modify the object."
</p>
        <p>
 
</p>
        <p>
You should always strive to adhere to this principle. It will make your code easier
to read and more importantly, easier to skip when reading. When I encounter code that
does not adhere to CQS I always split it up in several smaller methods, break out
objects etc etc. Once that is done, testing immediately becomes easier on the class.
</p>
        <p>
It's a matter of subclassing and overriding any behavior that is unwanted so that
the method you like to test can be tested in isolation. 
</p>
        <p>
 
</p>
        <p>
And with the Undetectable side effect covered we end the I can't get this class into
a test harness chapter of WEWLC. Hope you enjoyed reading. Drop a line in the comments
for feedback, or ideas.
</p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=d5fbb2c1-e30f-42b1-9abb-3706503c33e2" />
      </body>
      <title>WEWLC - I Can't Run This Method in a Test Harness</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,d5fbb2c1-e30f-42b1-9abb-3706503c33e2.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/09/15/WEWLCICantRunThisMethodInATestHarness.aspx</link>
      <pubDate>Tue, 15 Sep 2009 20:31:59 GMT</pubDate>
      <description>&lt;p&gt;
This is the third posting in the &lt;a href="http://www.tjsolutions.nl/2009/06/01/AnnouncingTheSeriesWorkingWithLegacyCode.aspx" target="_blank"&gt;WEWLC-series&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Other posts are:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/2009/09/14/WEWLCICantGetThisClassIntoATestHarness.aspx" target="_blank"&gt;I
Can't Get This Class into a Test Harness&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/2009/09/12/WEWLCIDontHaveMuchTimeAndIHaveToChangeIt.aspx" target="_blank"&gt;I
don't have much time and I have to change it&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.tjsolutions.nl/2009/09/12/WEWLCAssumptions.aspx" target="_blank"&gt;Assumptions
on the reader&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
"Getting tests in place to make changes can be a bit of a problem. If you can instantiate
your class separately in a test harness, consider yourself lucky." 
&lt;/p&gt;
&lt;p&gt;
Instantiating a class should not be hard, if you do have issues have a look at &lt;a href="http://www.tjsolutions.nl/2009/09/14/WEWLCICantGetThisClassIntoATestHarness.aspx" target="_blank"&gt;I
Can't Get This Class into a Test Harness&lt;/a&gt;. That should take care of most of your
problems. 
&lt;/p&gt;
&lt;p&gt;
But instantiating is often just the first part of all the hurdles to take. The second
part is writing a test for the methods inside. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Fortunately, in most cases, the amount of work that we have to do to write tests for
methods isn't as drastic. Here are some of the problems that we can run into.&lt;a name="ch10index03"&gt;&lt;/a&gt;&lt;a name="ch10index04"&gt;&lt;/a&gt;&lt;a name="ch10index05"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;
The method may be private
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
The method may be hard to call because it has parameters that are hard to construct
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
The method might have bad side effects (modifying a database, writing to the file
system etc)
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Case of the Hidden Method&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
If we need to write a test for a private method we need to make it public. There are
some tricks in getting visibility on the method in your tests but I don't recommend
it. First of all, if there is a need for private method odds are that what you need
is a another collaborator. 
&lt;/p&gt;
&lt;p&gt;
This is of course a rather fundamental discussion. If you do care about keeping the
method invisible to callers you can make it protected. Once it is protected you can
derive a test class and add a public method that will simply call the protected method
in the base class.
&lt;/p&gt;
&lt;p&gt;
In most cases I would recommend moving the method to a separate class and inject it
into the constructor of the class that interacts with it. Once there is a need to
alter the behavior of a private method it is obviously doing something important,
hence, it should be under test.
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Case of the &amp;quot;Helpful&amp;quot; Language Feature &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Michael Feather is referring to sealed classes that don't have constructor. How do
you test interaction code that takes such a class as a parameter in a method? Let's
have a look at an example:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamHelper &lt;/span&gt;{ &lt;span style="color: blue"&gt;const
int &lt;/span&gt;MIN_LEN = 4; &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList &lt;/span&gt;GetStreams(&lt;span style="color: #2b91af"&gt;HttpFileCollection &lt;/span&gt;files)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;list = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;string &lt;/span&gt;name &lt;span style="color: blue"&gt;in &lt;/span&gt;files)
{ &lt;span style="color: #2b91af"&gt;HttpPostedFile &lt;/span&gt;file = files[name]; &lt;span style="color: blue"&gt;if &lt;/span&gt;((file.FileName.EndsWith(&lt;span style="color: #a31515"&gt;&amp;quot;.txt&amp;quot;&lt;/span&gt;)
&amp;amp;&amp;amp; file.ContentLength &amp;gt; MIN_LEN)) { list.Add(file.InputStream); } } &lt;span style="color: blue"&gt;return &lt;/span&gt;list;
} }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
According to &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.httpfilecollection.aspx" target="_blank"&gt;MSDN&lt;/a&gt;,
the HttpFileCollection provides access to and organizes files uploaded by a client.
Since this class is sealed and only has an internal constructor, how can you test
this method?
&lt;/p&gt;
&lt;p&gt;
Turns out that HttpFileCollection derives from NameObjectCollectionBase which has
a parameterless constructor which is protected. If we thus derive from NameObjectCollectionBase,
implement the indexer required and change the signature of the GetStreams method a
bit to take our custom class instead of the HttpFileCollection we should be able to
get going. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Please note, this is going to be a hefty refactoring. Right, lets get down to it.
First let's create a fixture for our tests:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestFixture&lt;/span&gt;] &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpCollectionTests &lt;/span&gt;{ [&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_instantiate_a_derived_class_from_NameObjectCollectionBase() { &lt;span style="color: blue"&gt;var &lt;/span&gt;customHttpFileCollection
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;collectionBase
= customHttpFileCollection &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NameObjectCollectionBase&lt;/span&gt;;
collectionBase.ShouldNotBeNull(); } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
This means deriving from NameObjectCollectionBase
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;NameObjectCollectionBase &lt;/span&gt;{
}&lt;/pre&gt;
&lt;p&gt;
The next test is to be able to call GetStreams() in StreamHelper. Rather than changing
the existing method we are going to create a new method with the same name, but different
signature so that we can call it with our new CustomerHttpFileCollection:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_use_the_custom_class() { &lt;span style="color: blue"&gt;var &lt;/span&gt;collection
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection&lt;/span&gt;(); &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamHelper&lt;/span&gt;().GetStreams(collection);
}&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;
GetStreams(&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection &lt;/span&gt;files) { &lt;span style="color: blue"&gt;var &lt;/span&gt;list
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;string &lt;/span&gt;name &lt;span style="color: blue"&gt;in &lt;/span&gt;files)
{ &lt;span style="color: #2b91af"&gt;HttpPostedFile &lt;/span&gt;file = files[name]; &lt;span style="color: blue"&gt;if &lt;/span&gt;((file.FileName.EndsWith(&lt;span style="color: #a31515"&gt;&amp;quot;.txt&amp;quot;&lt;/span&gt;)
&amp;amp;&amp;amp; file.ContentLength &amp;gt; MIN_LEN)) { list.Add(file.InputStream); } } &lt;span style="color: blue"&gt;return &lt;/span&gt;list;
}&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;This means implementing the indexer
used in GetStreams() in our custom class. Luckily Resharper knows what to do and we
can use ALT-ENTER to generate the code. Next to the indexer we will also need an IList
to store the files and a method to add them to the internal collection:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;NameObjectCollectionBase &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpPostedFile &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;string &lt;/span&gt;name]
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;httpPostedFiles.Where(s
=&amp;gt; s.FileName == name).FirstOrDefault(); } } }&lt;/pre&gt;
&lt;p&gt;
This poses a bit of an issue; we can instantiate a HttpPostedFile so how are we going
to populate an internal collection? Since this one is sealed, does not have a suitable
base class and has no constructors we need to be creative. We need a custom implementation
of the HttpPostedFile. First we create an interface, then define the same members
on the interface as are in the HttpPostedFile class. Once that is done we can change
the signature of the indexer to IHttpPostedFile and add some logic to store and add
the files to the collection class, like so:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;NameObjectCollectionBase &lt;/span&gt;{ &lt;span style="color: blue"&gt;readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile&lt;/span&gt;&amp;gt;
httpPostedFiles = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;string &lt;/span&gt;name]
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;httpPostedFiles.Where(s
=&amp;gt; s.FileName == name).FirstOrDefault(); } } &lt;span style="color: blue"&gt;public void &lt;/span&gt;AddHttpPostedFile(&lt;span style="color: #2b91af"&gt;IHttpPostedFile &lt;/span&gt;file)
{ httpPostedFiles.Add(file); } }&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile &lt;/span&gt;{ &lt;span style="color: blue"&gt;int &lt;/span&gt;ContentLength
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; } &lt;span style="color: blue"&gt;string &lt;/span&gt;ContentType
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; } &lt;span style="color: blue"&gt;string &lt;/span&gt;FileName
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; } &lt;span style="color: #2b91af"&gt;Stream &lt;/span&gt;InputStream
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; } }&lt;/pre&gt;
&lt;p&gt;
Change the HttpPostedFile to the new IHttpPostedFile in GetStreams():
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;
GetStreams(&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection &lt;/span&gt;files) { &lt;span style="color: blue"&gt;var &lt;/span&gt;list
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;string &lt;/span&gt;name &lt;span style="color: blue"&gt;in &lt;/span&gt;files)
{ &lt;span style="color: #2b91af"&gt;IHttpPostedFile &lt;/span&gt;file = files[name]; &lt;span style="color: blue"&gt;if &lt;/span&gt;((file.FileName.EndsWith(&lt;span style="color: #a31515"&gt;&amp;quot;.txt&amp;quot;&lt;/span&gt;)
&amp;amp;&amp;amp; file.ContentLength &amp;gt; MIN_LEN)) { list.Add(file.InputStream); } } &lt;span style="color: blue"&gt;return &lt;/span&gt;list;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; &lt;pre class="code"&gt;&amp;#160;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Now that we have set up the collection, let's write a test to see if it all works
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Stream &lt;/span&gt;GetStream() { &lt;span style="color: blue"&gt;const
string &lt;/span&gt;s = &lt;span style="color: #a31515"&gt;&amp;quot;hello world&amp;quot;&lt;/span&gt;; &lt;span style="color: blue"&gt;var &lt;/span&gt;memoryStream
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;(System.Text.&lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.Default.GetBytes(s)); &lt;span style="color: blue"&gt;return &lt;/span&gt;memoryStream;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_use_the_customcollection_to_test_the_StreamHelper_class() { &lt;span style="color: blue"&gt;var &lt;/span&gt;collection
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;httpPostedFile
= &lt;span style="color: #2b91af"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile&lt;/span&gt;&amp;gt;();
httpPostedFile.Stub(s =&amp;gt; s.InputStream).Return(GetStream()); httpPostedFile.Stub(s
=&amp;gt; s.FileName).Return(&lt;span style="color: #a31515"&gt;&amp;quot;somefilename.txt&amp;quot;&lt;/span&gt;);
httpPostedFile.Stub(s =&amp;gt; s.ContentLength).Return(5); collection.AddHttpPostedFile(httpPostedFile); &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;
streams = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamHelper&lt;/span&gt;().GetStreams(collection);
streams.Count.ShouldEqual(1); }&lt;/pre&gt;
&lt;p&gt;
When we execute this test a lot is happening. First I create a mock of the IHttpPostedFile
interface so that I can stub the properties. This allows me to test the method without
having an actual implementation of the interface. Not that it would have complicated
things tremendously, but it's a nice technique.
&lt;/p&gt;
&lt;p&gt;
I also use a convenient helper method to get me a stream. This will come in handy
later when I want to verify the outcome of the GetStreams() method.
&lt;/p&gt;
&lt;p&gt;
Let's execute and:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
------ Test started: Assembly: Koning.WEWLC.Tests.dll ------ 
&lt;/p&gt;
&lt;p&gt;
TestCase 'Koning.WEWLC.Tests.HttpCollectionTests.can_use_the_customcollection_to_test_the_StreamHelper_class' 
&lt;br /&gt;
failed: 
&lt;br /&gt;
&amp;#160; Expected: 1 
&lt;br /&gt;
&amp;#160; But was:&amp;#160; 0 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; at Machine.Specifications.NUnitShouldExtensionMethods.ShouldEqual(Object
actual, Object expected) 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; C:\Dev\Personal\WEWLC\Koning.WEWLC\Koning.WEWLC.Tests\Tests\HttpCollectionTests.cs(59,0):
at Koning.WEWLC.Tests.HttpCollectionTests.can_use_the_customcollection_to_test_the_StreamHelper_class() 
&lt;/p&gt;
&lt;p&gt;
0 passed, 1 failed, 0 skipped, took 1.37 seconds. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Why is this happening? My bad, I didn't implement GetEnumerator() on the custom collection
class. This is easily fixed however:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;NameObjectCollectionBase&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IEnumerable &lt;/span&gt;{ &lt;span style="color: blue"&gt;readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile&lt;/span&gt;&amp;gt;
httpPostedFiles = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;string &lt;/span&gt;name]
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;httpPostedFiles.Where(s
=&amp;gt; s.FileName == name).FirstOrDefault(); } } &lt;span style="color: blue"&gt;public new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
GetEnumerator() { &lt;span style="color: blue"&gt;return &lt;/span&gt;httpPostedFiles.Select(s
=&amp;gt; s.FileName).GetEnumerator(); } &lt;span style="color: #2b91af"&gt;IEnumerator IEnumerable&lt;/span&gt;.GetEnumerator()
{ &lt;span style="color: blue"&gt;return &lt;/span&gt;GetEnumerator(); } &lt;span style="color: blue"&gt;public
void &lt;/span&gt;AddHttpPostedFile(&lt;span style="color: #2b91af"&gt;IHttpPostedFile &lt;/span&gt;file)
{ httpPostedFiles.Add(file); } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I'm using some Linq goodness to get to the filenames and iterate over that. Next up
is refining our test and run it again:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_use_the_customcollection_to_test_the_StreamHelper_class() { &lt;span style="color: blue"&gt;var &lt;/span&gt;collection
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomHttpFileCollection&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;httpPostedFile
= &lt;span style="color: #2b91af"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpPostedFile&lt;/span&gt;&amp;gt;();
httpPostedFile.Stub(s =&amp;gt; s.InputStream).Return(GetStream()); httpPostedFile.Stub(s
=&amp;gt; s.FileName).Return(&lt;span style="color: #a31515"&gt;&amp;quot;somefilename.txt&amp;quot;&lt;/span&gt;);
httpPostedFile.Stub(s =&amp;gt; s.ContentLength).Return(5); collection.AddHttpPostedFile(httpPostedFile); &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt;&amp;gt;
streams = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamHelper&lt;/span&gt;().GetStreams(collection);
streams.Count.ShouldEqual(1); streams.First().Length.ShouldBeGreaterThan(0); &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamReader&lt;/span&gt;(streams.First()).ReadLine().ShouldContain(&lt;span style="color: #a31515"&gt;&amp;quot;hello
world&amp;quot;&lt;/span&gt;); }&lt;/pre&gt;
&lt;p&gt;
------ Test started: Assembly: Koning.WEWLC.Tests.dll ------ 
&lt;/p&gt;
&lt;p&gt;
1 passed, 0 failed, 0 skipped, took 1.48 seconds. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Now note that when using the GetStreams() method one would have to transform the items
in HttpPostedFileCollection to the IHttpPostedFile interface before using it. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Case of the Undetectable Side Effect&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Every so often you encounter code that just makes you mumble WTF way more than is
healthy for any sane developer. In most cases things just happen everywhere. When
the code is performing a query, it's also updating some other pieces, altering the
file system a bit and lastly setting a whole bunch of other objects in an invalid
state.
&lt;/p&gt;
&lt;p&gt;
I wouldn't recommend getting the debugger up and running to see what's going on. Most
of the time it consumes a lot of time and keeps the frustration really high.
&lt;/p&gt;
&lt;p&gt;
What this kind of code is lacking is proper Command Query Separation (CQS). From the
WEWLC book "It's a is a design principle first described by Bertrand Meyer. Simply
put, it is this: A method should be a command or a query, but not both. A command
is a method that can modify the state of the object but that doesn't return a value.
A query is a method that returns a value but that does not modify the object."
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
You should always strive to adhere to this principle. It will make your code easier
to read and more importantly, easier to skip when reading. When I encounter code that
does not adhere to CQS I always split it up in several smaller methods, break out
objects etc etc. Once that is done, testing immediately becomes easier on the class.
&lt;/p&gt;
&lt;p&gt;
It's a matter of subclassing and overriding any behavior that is unwanted so that
the method you like to test can be tested in isolation. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
And with the Undetectable side effect covered we end the I can't get this class into
a test harness chapter of WEWLC. Hope you enjoyed reading. Drop a line in the comments
for feedback, or ideas.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=d5fbb2c1-e30f-42b1-9abb-3706503c33e2" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,d5fbb2c1-e30f-42b1-9abb-3706503c33e2.aspx</comments>
      <category>WEWLC</category>
    </item>
  </channel>
</rss>