<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elegant Code &#187; TDD</title>
	<atom:link href="http://elegantcode.com/tag/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 04:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>BDD Test Naming Experiment</title>
		<link>http://elegantcode.com/2008/09/08/bdd-test-naming-experiment/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bdd-test-naming-experiment</link>
		<comments>http://elegantcode.com/2008/09/08/bdd-test-naming-experiment/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 06:25:16 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/09/08/bdd-test-naming-experiment/</guid>
		<description><![CDATA[Of course, I am not the first person to do this, but gosh, isn’t it cool? This is the test suite from a project I am working on and I decided to move the BDD naming convention into the syntax: (class name) + (method name) = behavioral sentence This library has 100% code coverage and [...]]]></description>
			<content:encoded><![CDATA[<p>Of course, I am not the first person to do this, but gosh, isn’t it cool? </p>
<p>This is the test suite from a project I am working on and I decided to move the BDD naming convention into the syntax:   <br />(class name) + (method name) = behavioral sentence </p>
<p>This library has 100% code coverage and tests the sad path pretty well, in my opinion.</p>
<p>Click the image to get a bigger view.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/09/image2.png"><img title="Click the image to get a bigger view." style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="313" alt="Click the image to get a bigger view." src="http://elegantcode.com/wp-content/uploads/2008/09/image-thumb2.png" width="400" border="0" /></a></p>
<p>What’s your opinion? Is this easy to read to understand intent? Does this document my code for the next developer? Or is it just too much?</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/09/08/bdd-test-naming-experiment/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Get Started Writing Testable Code</title>
		<link>http://elegantcode.com/2008/05/20/get-started-writing-testable-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=get-started-writing-testable-code</link>
		<comments>http://elegantcode.com/2008/05/20/get-started-writing-testable-code/#comments</comments>
		<pubDate>Tue, 20 May 2008 16:11:11 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/05/20/get-started-writing-testable-code/</guid>
		<description><![CDATA[Testing is hard.&#160; Writing unit tests, regardless of if you write them first or last, is hard.&#160; This is because creating a design that allows you to write testable code is difficult at first &#8211; you have to break some old habits that you&#8217;ve relied on for a long time, and learn some new habits.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Testing is hard.&#160; Writing unit tests, regardless of if you write them first or last, is hard.&#160; This is because creating a design that allows you to write testable code is difficult at first &#8211; you have to break some old habits that you&#8217;ve relied on for a long time, and learn some new habits.&#160; </p>
<p>Many of the technologies we work with don&#8217;t easily lend themselves to unit testing: web pages, stored procedures, 3rd party controls.&#160; You&#8217;ll probably have to learn some new technologies that make testing practical, which also takes time and energy.&#160; You might have to write test barriers between the &quot;untestable&quot; part, and the logic; moving as much logic out of the hard to reach area and into easily tested code as possible.&#160; Which at a minimum represents more classes, more complexity, more typing&#8230;</p>
<p>Writing testable code is hard, and requires a shift in how you write code.&#160; This shift won&#8217;t come overnight.&#160; At first you will write &quot;sort of&quot; testable code, hopefully following some ideas laid out by other codebases (Various parts of <a href="http://www.castleproject.org/">Castle</a>, <a href="http://www.codeplex.com/aspnet">ASP.NET MVC</a>, and Rhino-Commons and other examples by <a href="http://www.ayende.com/Blog/">Oren Eini</a> are some good places to start).&#160; You will end up making mistakes, and a few months later you&#8217;ll look back at this code and wish you had done things differently.&#160; If you&#8217;re not having that experience with your code, then how have you improved as a developer?&#160; If you look at last year&#8217;s code and think that you&#8217;d change absolutely nothing, doesn&#8217;t that at least imply you haven&#8217;t improved your skills?</p>
<p><em>&quot;But, we don&#8217;t have time to write unit tests.&quot;</em>&#160; Does that mean that you have time to debug your application by stepping through hundreds of lines of code?&#160; Do you have time to litter your code with printf()s hoping to track down where a variable gets set to null?&#160; Maybe we&#8217;re just too used to the Edit-Build-Debug Cycle taking minutes for each iteration, waiting for ASP.NET to finish compiling its pages so we can navigate to where the problem is, enter what we think the bad data is, click a button, and then start hitting breakpoints&#8230; lather, rinse, repeat, for hours on end.</p>
<p>So when the project gets to the end of the timeline and budget, and the result is buggy, and we&#8217;re breaking more things with each deployment because of regression issues, and the client starts saying things like &quot;this project is a total failure,&quot; and the project manager is demanding that everyone start putting in all-nighters to deal with the bug list &#8230; is that better than an alternative of spending a little bit more time up front on writing easily-tested code?&#160; </p>
<p>So, how about we strike a pragmatic agreement &#8211; identify a difficult part of an application you&#8217;re working on, and figure out how to write unit tests for it.&#160; I&#8217;m not even talking about &quot;test first&quot; or &quot;100% test coverage,&quot; just write something to make it easier to confirm that the behavior of this code is correct.&#160; At least identify the parts of your code that stand in the way of tests &#8211; its almost always database or web-dependencies, and is an indicator of further problems, but we&#8217;re taking baby steps here.&#160; See how that goes, and see what you can learn from it.</p>
<p>I await your flames in the comments&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/05/20/get-started-writing-testable-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Meet Me at Agile 2008</title>
		<link>http://elegantcode.com/2008/05/05/meet-me-at-agile-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=meet-me-at-agile-2008</link>
		<comments>http://elegantcode.com/2008/05/05/meet-me-at-agile-2008/#comments</comments>
		<pubDate>Mon, 05 May 2008 15:15:10 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/05/05/meet-me-at-agile-2008/</guid>
		<description><![CDATA[I know it is a bit early to be throwing this out there, but I am pretty excited about this one. I have had 2 sessions accepted for presentation at the Agile 2008 conference in Toronto. This year&#8217;s Agile conference will be in August and if you haven&#8217;t been to one of these conferences, do [...]]]></description>
			<content:encoded><![CDATA[<p>I know it is a bit early to be throwing this out there, but I am pretty excited about this one. I have had 2 sessions accepted for presentation at the Agile 2008 conference in Toronto. This year&#8217;s Agile conference will be in August and if you haven&#8217;t been to one of these conferences, do yourself a favor and get there. You will meet people you&#8217;ve only read about and they are actually nice <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . <a href="http://elegantcode.com/2007/08/16/agile2007-day-3-flippin-awesome/" target="_blank">This post</a> details a great day in the life of my attendance at the Agile 2007 conference. It is actually pretty typical of the experience you can expect.</p>
<p>The sessions I will be presenting are:</p>
<ul>
<li>&quot;Healthwise &#8211; An Agile Adoption Case Study&quot; &#8211; This is a co-presentation with our PMO officer, Ken Long.</li>
<li>TDD Clinic: .Net and C#</li>
</ul>
<p>I really hope to see you there (there are great parties).</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/05/05/meet-me-at-agile-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>
<p>&#160;</p>
<div>
<div class="csharpcode">
<pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>
<pre class="alteven"><span class="lnum">   2:</span> {</pre>
<pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>
<pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>
<pre class="alteven"><span class="lnum">   5:</span>         </pre>
<pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>
<pre class="alteven"><span class="lnum">   7:</span>     {</pre>
<pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>
<pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>
<pre class="alteven"><span class="lnum">  10:</span>     }</pre>
<pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>
<pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>
<pre class="alteven"><span class="lnum">  13:</span>     {</pre>
<pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>
<pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre class="alteven"><span class="lnum">  16:</span>     }</pre>
<pre class="alteven"><span class="lnum">  17:</span>         </pre>
<pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>
<pre class="alteven"><span class="lnum">  19:</span>     {</pre>
<pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>
<pre class="alteven"><span class="lnum">  21:</span>     }</pre>
<pre class="alteven"><span class="lnum">  22:</span> }</pre>
</p></div>
</div>
<pre class="alteven">This fluent builder class can then be used this way:</pre>
<div>
<div class="csharpcode">
<pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>
<pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>
<pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>
<pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
</p></div>
</div>
<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I&#8217;m really looking forward to.</p>
<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>
<p>&#160;</p>
<div>
<div class="csharpcode">
<pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>
<pre class="alteven"><span class="lnum">   2:</span> {</pre>
<pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>
<pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>
<pre class="alteven"><span class="lnum">   5:</span>         </pre>
<pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>
<pre class="alteven"><span class="lnum">   7:</span>     {</pre>
<pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>
<pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>
<pre class="alteven"><span class="lnum">  10:</span>     }</pre>
<pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>
<pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>
<pre class="alteven"><span class="lnum">  13:</span>     {</pre>
<pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>
<pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre class="alteven"><span class="lnum">  16:</span>     }</pre>
<pre class="alteven"><span class="lnum">  17:</span>         </pre>
<pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>
<pre class="alteven"><span class="lnum">  19:</span>     {</pre>
<pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>
<pre class="alteven"><span class="lnum">  21:</span>     }</pre>
<pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>
<pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>
<pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>
<pre class="alteven"><span class="lnum">  25:</span>     {  </pre>
<pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>
<pre class="alteven"><span class="lnum">  27:</span>     } </pre>
<pre class="alteven"><span class="lnum">  28:</span> }</pre>
</p></div>
</div>
<p>which results in the following usage:</p>
<p>&#160;</p>
<div>
<div class="csharpcode">
<pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>
<pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>
<pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
</p></div>
<p>Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don&#8217;t you agree?</div>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p>
<p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class &#8212; but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p>
<p>So I&#8217;m going to re-implement my class methods.&nbsp; Here you go:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don&#8217;t like using Rhino Mocks you do this:</p>
<pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don&#8217;t create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn&#8217;t there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd &#8220;<strong>virtual</strong>&#8221; keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don&#8217;t like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can&#8217;t help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p>
<pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression &#8220;<strong>x =&gt; x.Bar()</strong>&#8221; where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn&#8217;t do unless you really need to.&#160; Also, I&#8217;m sure this is not new, I just can&#8217;t find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[<p>I am writing this as a way to document something that is possible, but you probably shouldn&#8217;t do unless you really need to.&nbsp; Also, I&#8217;m sure this is not new, I just can&#8217;t find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn&#8217;t know about something it must be wrong.</p>
<h2>The problem</h2>
<p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here</span>&nbsp;&nbsp;&nbsp; return i + 5}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests &#8212; assuming both methods exist in a class call MyClass):</p>
<pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p>
<pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn&#8217;t be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal
<li>BarDelegate delegate
<li>Created a variable named Bar to point to the method BarInternal
<li>Created a constructor to hook up Bar to BarInternal.</li>
</ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p>
<pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p>
<pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I&#8217;ve done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this &#8212; but make sure you are out of alternatives first.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Injecting Some Control Into Legacy Code</title>
		<link>http://elegantcode.com/2008/04/01/injecting-some-control-into-legacy-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=injecting-some-control-into-legacy-code</link>
		<comments>http://elegantcode.com/2008/04/01/injecting-some-control-into-legacy-code/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 21:57:10 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/01/injecting-some-control-into-legacy-code/</guid>
		<description><![CDATA[I&#8217;ve been talking a lot recently on how one might get started using techniques such as TDD and Inversion of Control in their daily code, particularly for those of us who spend most of our time dealing with &#8220;Legacy&#8221; codebases. Some of these ideas are tough to grasp until you&#8217;ve had a chance to try [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been talking a lot recently on how one might get started using techniques such as TDD and Inversion of Control in their daily code, particularly for those of us who spend most of our time dealing with &#8220;Legacy&#8221; codebases. Some of these ideas are tough to grasp until you&#8217;ve had a chance to try them out. So here&#8217;s another take on how to get started.</p>
<p>How would we deal with adding or modifying some functionality in a Legacy application, while moving forward with some better practices?</p>
<h3>In the Beginning&#8230;</h3>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/inthebeginning.jpg"><img src="http://elegantcode.com/wp-content/uploads/2008/04/inthebeginning-thumb.jpg" style="border-width: 0px" alt="InTheBeginning" border="0" height="142" width="294" /></a></p>
<p>We have a big lump of Legacy Code, a formless void of hard to manage complexity. We need to modify some functionality in the MyConsumerClass, that <em>DealsWithThings</em> and can <em>DoStuff.  </em>Let&#8217;s modify MyConsumerClass in a way that allows us to decouple what MyConsumerClass does from the rest of the Legacy Code.  This will be the starting point for making this application cleaner.</p>
<h3>Build in Layers</h3>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/addeddependencyinjection.jpg"><img src="http://elegantcode.com/wp-content/uploads/2008/04/addeddependencyinjection-thumb.jpg" style="border-width: 0px" alt="AddedDependencyInjection" border="0" height="149" width="356" /></a></p>
<p>The class we&#8217;re working on relies on functionality from The Cloud for doing something with text. (Maybe we&#8217;re showing the latest sales figures from the corporate SharePoint portal &#8211; the specifics aren&#8217;t very important&#8230;)</p>
<p>We want to be able to modify the behavior of the text provider without needing sweeping changes everywhere else. So how do we separate these pieces?</p>
<p>First, let&#8217;s change MyConsumerClass to access the text via an interface instead of creating specific instances directly from the cloud.  MyConsumerClass is going to ask &#8220;something&#8221; for the appropriate concrete instance &#8211; instead of making decisions about what that concrete instance should be.  This &#8220;something&#8221; is our Inversion of Control Container, and it will know how to pick the right ITextProvider implementation, create one of them, and return the reference to MyConsumerClass for use.</p>
<p>The type that the IoC returns might be a thin wrapper on top of the &#8220;real&#8221; functionality in the Legacy Cloud.   It could also be configured to inject an easily testable provider instead. So, we have broken the direct dependency of MyConsumerClass to the Legacy Code Cloud.</p>
<h3>The Result</h3>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/theresult.jpg"><img src="http://elegantcode.com/wp-content/uploads/2008/04/theresult-thumb.jpg" style="border-width: 0px" alt="TheResult" border="0" height="96" width="360" /></a></p>
<p>So, what do we have? Our ConsumerClass, and the TextProvider, can now be worked with (and tested) as individual units. They can be maintained without having to get too deep into that Legacy Cloud. We now have the beginnings of infrastructure, the InversionOfControlContainer, which we can leverage against other maintenance tasks. The Legacy Cloud itself is now just that much more smaller &#8211; we have walled off a small section of order from the chaos of the rest of the cloud.</p>
<h3>Conclusion</h3>
<p>After you have spent some time working on breaking dependencies, you&#8217;ll find that code without dependencies is easier to maintain.  Your classes get less complex, so they are easier to test and verify as correct.  Because of fewer dependencies, changes to one part of the application won&#8217;t cause problems for another part.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/01/injecting-some-control-into-legacy-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NetDug Unit Testing</title>
		<link>http://elegantcode.com/2008/03/22/netdug-unit-testing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=netdug-unit-testing</link>
		<comments>http://elegantcode.com/2008/03/22/netdug-unit-testing/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 21:00:30 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/22/netdug-unit-testing/</guid>
		<description><![CDATA[So last night I had the opportunity to be the moderator at NetDug, we were talking about Unit Testing.  It was very interesting.  We had one of those nice mixes between people who had not done unit testing yet, to guys who knew way more about it than I do.  I went into the talk [...]]]></description>
			<content:encoded><![CDATA[<p>So last night I had the opportunity to be the moderator at <a href="http://www.netdug.com">NetDug</a>, we were talking about Unit Testing.  It was very interesting.  We had one of those nice mixes between people who had not done unit testing yet, to guys who knew way more about it than I do.  I went into the talk with 20 slides, I think I showed about 5 of them.  I consider that a success (I threatened the audience, the less they talked, the more slides I would show).</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/03/unittestingdemo.zip">Slides and sample code</a></p>
<p>What I learned from last night:</p>
<ol>
<li>I&#8217;m not much of a moderator (I talk to much)</li>
<li>Code coverage is a art in divination</li>
<li>Mock/Stub/Fake Objects are hard to explain</li>
<li>Over use Mocks could be a code smell</li>
<li>The discussion format actually works pretty well for talking about unit testing</li>
<li>If you give geeks a chance to talk, they will talk</li>
</ol>
<p>What I hope everyone else got from the topic:</p>
<ol>
<li>Unit testing forces you to write better code</li>
<li>Unit testing forces you to use better object oriented principles</li>
</ol>
<h3>Test Technology</h3>
<p>This was an early question, what technologies to use to test your code.  <a href="http://www.nunit.org/index.php">NUnit</a>, MSUnit, and <a href="http://www.mbunit.com/">MBUnit</a> were all mentioned and viable, and stable, technologies.  For mock objects (which you are going to need) there is <a href="http://www.ayende.com/projects/rhino-mocks/downloads.aspx">Rhino Mocks</a> and <a href="http://www.typemock.com/">TypeMock</a>.  I suggest Rhino Mocks over <a href="http://www.typemock.com/">TypeMock</a> for anyone who can&#8217;t purchase the full version of <a href="http://www.typemock.com/">TypeMock</a> (but there is a nice free version as well).</p>
<p>Then there are the helper technologies: <a href="http://testdriven.net">TestDriven.Net</a> and <a href="http://www.jetbrains.com/resharper/index.html">ReSharper</a> are the main two, but apparently MSUnit&#8217;s runner isn&#8217;t bad either (I haven&#8217;t used it yet).  All of these tools make it easier to run your unit tests from inside Visual Studio, and each has other benefits.  <a href="http://testdriven.net">TestDriven.Net</a> has wonderful Reflector integration (a better object browser), and ReSharper&#8230;<a href="http://www.jetbrains.com/resharper/index.html">ReSharper</a> is just a must have (that is me talking, not the group).</p>
<h3>Code Coverage</h3>
<p>Code Coverage, using tools like <a href="http://www.ncover.com/">NCover</a> (<a href="http://www.ncover.com/download/discontinued">free version</a>), tell you how much of your code base is tested.  This was one of the early questions.  How much of your code base should be tested?  Answer: it depends on the product.  My view is that a library (no UI element) should have +90% coverage, if there is a substantial amount of UI then the project can get down to 60%.  If your code coverage gets below that you just aren&#8217;t trying, or you need to rethink your architecture. </p>
<p>I did talk a bit about code that was not really testable (not in an automated way anyway): UI, Database code, and external resource code are all potentially untestable.  More on this in a moment.</p>
<p>Database code is not testable if you can&#8217;t return the database to a known state (you can restore the database data). </p>
<p>UI code testing should be avoided because UI tests have to be so bound to the UI that they are brittle.  So you end up spending a considerable portion of your development time fixing broken test. Brittle tests are to be avoided whenever possible.</p>
<h3>Testing existing code</h3>
<p>Probably the hardest question to answer from last night was: how do you test existing code bases.  And that came up over and over.  Do you start with integration tests and then work in unit tests, or the other way around. </p>
<p>I will admit, most of the time when I talk about unit testing, I center it around new projects.  Unit test is hard enough with new code, testing old code that wasn&#8217;t written with unit test in mind is a migraine waiting to happen.</p>
<p>The audience did have some good advice though.  Pick parts of the project that you need to work on (say one class) and make that testable and tested.  Essentially you start creating silos of tested code.  The other suggestion (and I actually had a slide for this) was to get the book: <a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1206114497&amp;sr=8-1">Working Effectively with Legacy Code</a>.  Finally, buy TypeMock &#8212; the big boy version, not the free-be version.  You&#8217;ll need it.</p>
<h3>UI Testing</h3>
<p>This is my treatise on writing unit tests for the User Interface:</p>
<p>1. Don&#8217;t do it until you are done with the UI.<br />
2. Only test that the UI works in a basic sense.<br />
3. You still have to test the UI by hand.</p>
<p>If you start writing UI tests while you are developing the UI, you will continually break the tests.  Writing the test by hand is none trivial, and using tools to write them is also error prone.  Finally, these tests can&#8217;t tell you if the UI is just bad.  Only exposure can tell you that.  Every developer should be forced to have to use their UI repeatedly so they know where is sucks.  You wont get that with automated UI tests.</p>
<p>That said, there are tools to help you test web user interfaces. <a href="http://watin.sourceforge.net/">Watin</a>, <a href="http://wtr.rubyforge.org/">Watir</a>, and <a href="http://selenium.openqa.org/">Selenium</a> are all there to help.  We shall see if Team Systems adds anything to help with this (I&#8217;m hoping it does).</p>
<h3>Testing Database code</h3>
<p>This is essentially, testing code that touches the database.  Could be your <a href="http://www.hibernate.org/343.html">NHibernate</a> code, could be a stored procedure, could be ADO.Net code.  But really, this is just code that must touch an outside resource (database, file system, serial connection, etc) and there is no way, or point, to abstract it out any further.</p>
<p>The main point here was to have a way to get the data back to a known state.  Without that everything else gets much harder.</p>
<p>We also came up with a new term: SQL Ejaculation.  That is a pattern smell where an excessive amount of your HTML comes from you SQL database.  Like the entire site.  All of the HTML.  And you have stored procedures that simply build the HTML &#8212; in a 10,000 line stored procedure.  That is SQL Ejaculation.</p>
<p>Note: if this comes up in a Google search I might get worried.</p>
<h3></h3>
<h3>After the meeting</h3>
<p>We all went to Table Rock for a beer.  &#8216;nuf said, it is a great group.</p>
<p>Finally, to the guy who asked me a question after the meeting was over: I&#8217;m sorry, I just haven&#8217;t had to draw multiple, overlapping, transparent images on a win form using GDI.Net in a really long time.  I know it involves alpha-transparency values, but I haven&#8217;t done that in years.  Maybe you should look at WPF.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/22/netdug-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=microsoft-releases-unity-an-ioc-library</link>
		<comments>http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 03:41:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/</guid>
		<description><![CDATA[Here is a bit of interesting news out of Microsoft.&#160; They have released their first IoC container named Unity.&#160; It is out on CodePlex now: http://www.codeplex.com/unity If you are looking for a similar product with a longer lineage, you could look up: StructureMap, Spring.Net, and Castle Windsor.&#160; StructureMap is maintained by Jeremy Miller, who is [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a bit of interesting news out of Microsoft.&nbsp; They have released their first IoC container named Unity.&nbsp; It is out on CodePlex now: <a href="http://www.codeplex.com/unity">http://www.codeplex.com/unity</a></p>
<p>If you are looking for a similar product with a longer lineage, you could look up: <a href="http://structuremap.sourceforge.net/Default.htm">StructureMap</a>, <a href="http://www.springframework.net/">Spring.Net</a>, and <a href="http://www.castleproject.org/container/">Castle Windsor</a>.&nbsp; StructureMap is maintained by <a href="http://codebetter.com/blogs/jeremy.miller/">Jeremy Miller</a>, who is a favorite blogger of mine (everyone should read his series on <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/07/25/the-build-your-own-cab-series-table-of-contents.aspx">Build your own CAB</a>).&nbsp; Spring.Net is a port of an existing Java project, and Castle Windsor seems to have a lot of mind share.&nbsp; Currently, Spring.Net is the only IoC library that I have used.</p>
<p>OK, definitions: <br />IoC: Inversion of Control, also called Dependency Injection.<br />This is based on a pattern found in the Gang Of Four books on Design Patterns.<br />You can read more about this pattern at Martin Fowlers web site:<br /><a href="http://martinfowler.com/articles/injection.html">http://martinfowler.com/articles/injection.html</a><br />Or on Wikipedia:<br /><a href="http://en.wikipedia.org/wiki/Inversion_of_control">http://en.wikipedia.org/wiki/Inversion_of_control</a></p>
<p>The basic principle that IoC is used for is Loose Coupling.&nbsp; If you have a multi-layered application, each layer should not have to know specific details about the other layers.&nbsp; So each object in a specific layer will implement an interface that the IoC provider knows about.&nbsp; Then, when a specific dependency is needed, you ask the IoC container for it (note: the IoC container is Tightly coupled) for the object to do the work.&nbsp;&nbsp; If you look at the Asp.Net Membership Provider, you can see a simple example of that at work.</p>
<p>One of the ways that this can help you is that now all of your middle pieces can be swapped out without having to make massive code changes.&nbsp; This also makes testing easier, as now you can more easily stub/mock/fake dependencies and just test your application logic.</p>
<p>Also, if you really get into the whole IoC thing, you will find that there are a number of other cool Computer Sciency things are possible.&nbsp; Like Aspect Oriented Programming (unknown if Unity supports that or not).</p>
<p><em>Now with all of the other IoC libraries out there, why would I like the fact that Microsoft has created their own?&nbsp; <br /></em>One of the current issues with a lot of Microsoft software, especially the samples, is that they are all VERY tightly coupled (again, tight coupling is not good), but it is a pain to get around without an IoC solution.&nbsp; And, as per Microsoft&#8217;s own structure, they will not use any software that they didn&#8217;t write or own.&nbsp;&nbsp; So I&#8217;m hopeful that now they can start producing some software with loose coupling in mind, and make all of our lives easier.</p>
<p>All right, there are still a lot of definitions and ideas that could leave you puzzled, that happens.&nbsp; That is also why we have google.&nbsp; If you see something you don&#8217;t understand, leave a comment and I&#8217;ll try to clarify, or point you to someone else with a better explanation than I can give.</p>
<p>Finally, if you are looking for more information on this sort of thing, check out the session on Castle Windsor at <a href="http://BoiseCodeCamp.com">Boise Code Camp</a> on March 8.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>It Hurts When I Do That</title>
		<link>http://elegantcode.com/2008/02/09/it-hurts-when-i-do-that/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=it-hurts-when-i-do-that</link>
		<comments>http://elegantcode.com/2008/02/09/it-hurts-when-i-do-that/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 20:51:03 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Scrum]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/02/09/it-hurts-when-i-do-that/</guid>
		<description><![CDATA[One of the most curious things I encounter when meeting people new to Agile are long time veterans of the software development game who all agree that up-front design doesn&#8217;t work and also think Agile is not valuable. See the contradiction? These folks (and I have met many over the years) are quick to point [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most curious things I encounter when meeting people new to Agile are long time veterans of the software development game who all agree that up-front design doesn&#8217;t work and also think Agile is not valuable.</p>
<p>See the contradiction? </p>
<p>These folks (and I have met many over the years) are quick to point out the flaws, inconsistencies, and downright disfunction of their current waterfall process. </p>
<p>&#8220;We get the requirements from the Business Analysts,&#8221; they say, &#8220;and they are never right.&#8221;</p>
<p>&#8220;We build an deliver to QA and move on to the next project,&#8221; they say, &#8220;and it always comes back with defects.&#8221;</p>
<p>&#8220;There is no such thing as defect-free software,&#8221; they say, &#8220;an in their world they are right.&#8221;</p>
<p>&#8220;It hurts when I hit my finger with the hammer,&#8221; they say while placing their index finger atop the next nail.</p>
<p>These same folks will dismiss Agile and iterative development practices in the next breath with words like &#8220;impractical&#8221;, &#8220;unrealistic&#8221;, or &#8220;chaotic&#8221;. Is this fear of change? Is it threatening to have the system you hate so much be threatened by an up-and-coming idea? This seems to me a bit like hating the king and leaving him in power for fear of who might will replace him.</p>
<p>This is one of the reasons I find it terribly important to discuss not just the benefits of all things Agile like Scrum and TDD, but to also introduce the history and practices of failed engineering projects that caused these things to emerge. This provides a pathway to enlightenment for the most diehard anti-Agilista. Once we can establish the history, theory, demonstrated success, and techniques, engineering minded folks are more able to see these Agile practices as real tools for their toolbox.</p>
<p>Another useful technique is to start these folks off with engineering process first, not methodology changes. Almost everyone can agree to the demonstrated value of TDD. Once a surly developer sees TDD as a small, iterative feedback loop, we can start asking why the next level of the feedback loop isnt&#8217;t happening with automated builds. Then Continuous Integration. Then iterative delivery to a product owner. Then Agile because by now they are already there without knowing it.</p>
<p>Leading with process in a top down implementation can be ineffective when selling to grizzled engineers. Let&#8217;s help people stop hitting their fingers before we try to get them to build something beautiful.</p>
<div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e2e1dbff-ca7c-4d3b-a896-2e39d65ac4e3" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a href="http://technorati.com/tags/Agile" rel="tag">Agile</a>,<a href="http://technorati.com/tags/TDD" rel="tag">TDD</a>,<a href="http://technorati.com/tags/Scrum" rel="tag">Scrum</a></div>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/02/09/it-hurts-when-i-do-that/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

