<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.mozingo.net/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Taller Code</title>
	
	<link>http://darrell.mozingo.net</link>
	<description>Darrell Mozingo's blog on .NET and development in general</description>
	<pubDate>Thu, 02 Sep 2010 23:40:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.mozingo.net/TallerCode" /><feedburner:info uri="tallercode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Comments are just missed refactorings</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/Z9PoWhdFdJo/</link>
		<comments>http://darrell.mozingo.net/2010/09/02/comments-are-just-missed-refactorings/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 23:40:49 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Musings]]></category>

		<category><![CDATA[Add new tag]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=783</guid>
		<description><![CDATA[Ah comments, those delectable little nuggets of static information that routinely get out of sync with your code&#8217;s intention. Actually, I&#8217;m sort of excited when I run into them though. See, to me they represent missed refactorings that are usually pretty easy to implement.

Say you run across a piece of code like this:


1
2
3
4
5
6
7
8
9
10
11
12
public void ProcessOrder&#40;Order [...]]]></description>
			<content:encoded><![CDATA[<p>Ah comments, those delectable little nuggets of static information that routinely get out of sync with your code&#8217;s intention. Actually, I&#8217;m sort of excited when I run into them though. See, to me they represent missed refactorings that are usually pretty easy to implement.</p>

<p>Say you run across a piece of code like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ProcessOrder<span style="color: #000000;">&#40;</span>Order order<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// If the order is on hold, figure out why and get the next availability date.</span>
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>order.<span style="color: #0000FF;">Status</span> <span style="color: #008000;">==</span> OrderStatus.<span style="color: #0000FF;">OnHold</span> <span style="color: #008000;">&amp;&amp;</span> order.<span style="color: #0000FF;">OrderDate</span> <span style="color: #008000;">&lt;</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// &lt;imagine complex logic to check reason here...&gt;</span>
		order.<span style="color: #0000FF;">OnHoldReason</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>result of complex logic above<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// &lt;imagine complex logic to get the next available date here...&gt;</span>
		order.<span style="color: #0000FF;">NextAvailableDate</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>result of complex logic above<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>So it&#8217;s usually not this obvious in real code bases, but almost every time I&#8217;ve run into a grouping of comments in this arrangement it could be boiled down into something this simple. Can you see the refactoring potential? It&#8217;s pretty easy - extracting some variables and methods based upon the text in the comment:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ProcessOrder<span style="color: #000000;">&#40;</span>Order order<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var orderIsOnHold <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>order.<span style="color: #0000FF;">Status</span> <span style="color: #008000;">==</span> OrderStatus.<span style="color: #0000FF;">OnHold</span> <span style="color: #008000;">&amp;&amp;</span> order.<span style="color: #0000FF;">OrderDate</span> <span style="color: #008000;">&lt;</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>orderIsOnHold<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		order.<span style="color: #0000FF;">OnHoldReason</span> <span style="color: #008000;">=</span> getOnHoldReason<span style="color: #000000;">&#40;</span>order<span style="color: #000000;">&#41;</span>
		order.<span style="color: #0000FF;">NextAvailableDate</span> <span style="color: #008000;">=</span> getNextAvailableDate<span style="color: #000000;">&#40;</span>order<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> getOnHoldReason<span style="color: #000000;">&#40;</span>Order order<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">return</span> <span style="color: #008080; font-style: italic;">// &lt;imagine complex logic to check reason here...&gt;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> DateTime getNextAvailableDate<span style="color: #000000;">&#40;</span>Order order<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">return</span> <span style="color: #008080; font-style: italic;">// &lt;imagine complex logic to get the next available date here...&gt;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Like I said, it&#8217;s not much and it&#8217;s certainly not hard (a few keystrokes in Visual Studio, with or without ReSharper), but it moves the comments from sitting idly above the code, not participating, to being true first class citizens in the program in the form of variable and method names. It doesn&#8217;t guarantee they&#8217;ll be updated with the code if the logic changes in the future, but it gives them a lot better shot at it. I mean, there aren&#8217;t really developers out there lazy enough to update the intention of a variable or method and <i>not</i> change the name, right? Right?</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/09/02/comments-are-just-missed-refactorings/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/09/02/comments-are-just-missed-refactorings/</feedburner:origLink></item>
		<item>
		<title>Controlling IIS7 remotely with PowerShell</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/RTPagNAlNZc/</link>
		<comments>http://darrell.mozingo.net/2010/08/26/controlling-iis7-remotely-with-powershell/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 23:26:24 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Misc.]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=753</guid>
		<description><![CDATA[Our deployment script needed to do some basic IIS administrative tasks remotely on a Windows 2008 (non-R2) server, which runs IIS7, recently. Finding the information and fiddling around with it took me a good day and a half, so I thought I&#8217;d post the steps here to help someone else (more than likely myself) in [...]]]></description>
			<content:encoded><![CDATA[<p>Our deployment script needed to do some basic IIS administrative tasks remotely on a Windows 2008 (non-R2) server, which runs IIS7, recently. Finding the information and fiddling around with it took me a good day and a half, so I thought I&#8217;d post the steps here to help someone else (more than likely myself) in the future:</p>

<ol>
<li>Download the <a href="http://support.microsoft.com/kb/968929">Windows Management Framework Core package</a> for your setup</li>
<li>If your machine is something older than Windows 7 or Server 2008 <strong>R2</strong>, you&#8217;ll need to get the <a href="http://www.iis.net/download/powershell">PowerShell IIS7 Snap-In</a></li>
<li>If your workstation/build server and target web servers happen to be on different Windows domains, you&#8217;ll need to run this one time on each client machine:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">Set-Item WSMan:\localhost\Client\TrustedHosts *</pre></td></tr></table></div>


</li>
<li>Run this command once on each server:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">winrm quickconfig</pre></td></tr></table></div>


</li>
<li>You&#8217;ll need to load the PowerShell Snap-In once on each client, which differs depending on which version of Windows you&#8217;re running. Anything older than Windows 7 or Server 2008 <strong>R2</strong>:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">Add-PSSnapin WebAdministration</pre></td></tr></table></div>



Windows 7 and Server 2008 <strong>R2</strong> run:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">Load-Module WebAdministration</pre></td></tr></table></div>


</li>
<li>Check if it&#8217;s working properly by running this command on any version of Windows (you should see the IIS7 Snap-In listed):


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">Get-Module -ListAvailable</pre></td></tr></table></div>


</li>
<li><a href="http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell">Get credentials for accessing the remote server</a></li>
<li>Start running some remote commands on your web servers:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Invoke<span style="color: #008000;">-</span>Command <span style="color: #008000;">-</span>ComputerName <span style="color: #666666;">&quot;webserver_computerName&quot;</span> <span style="color: #008000;">-</span>Credential $credentials_from_last_step <span style="color: #008000;">-</span>ScriptBlock <span style="color: #000000;">&#123;</span> Add<span style="color: #008000;">-</span>PSSnapin WebAdministration<span style="color: #008000;">;</span> Stop<span style="color: #008000;">-</span>Website IIS_Site_Name <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


</li>
</ol>

<p>It&#8217;s not really  that hard once you get the proper packages installed and the permissions worked out, and since it&#8217;s so powerful and useful for scripting purposes it&#8217;s well worth the trouble. The available commands are awesome for use in automated deployment scripts.</p>

<p>You can learn more about the PowerShell Snap-In provider <a href="http://learn.iis.net/page.aspx/428/getting-started-with-the-iis-70-powershell-snap-in">here</a>, and at its download site <a href="http://www.iis.net/download/powershell">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/08/26/controlling-iis7-remotely-with-powershell/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/08/26/controlling-iis7-remotely-with-powershell/</feedburner:origLink></item>
		<item>
		<title>Clever vs Explicit</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/YVsFUlrPj3A/</link>
		<comments>http://darrell.mozingo.net/2010/08/19/clever-vs-explicit/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 00:03:23 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Musings]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=759</guid>
		<description><![CDATA[When we all start out developing, either through classes in high school/college or slowly on our own time, we inevitably want to write code thats super clever. Like, use-3-lines-to-express-what-used-to-take-20-lines type of clever. We see less code and we&#8217;re pleased. All is good and well with the world.

Until you look at that code a few months [...]]]></description>
			<content:encoded><![CDATA[<p>When we all start out developing, either through classes in high school/college or slowly on our own time, we inevitably want to write code thats super clever. Like, use-3-lines-to-express-what-used-to-take-20-lines type of clever. We see less code and we&#8217;re pleased. All is good and well with the world.</p>

<p>Until you look at that code a few months down the road and wounder what the hell you were smoking, and it takes you almost as long to decipher it again as it did to write it in the first place. All of a sudden saving those few extra lines of code don&#8217;t seem so smart, huh?</p>

<p>The simple fact of the matter is you spend more time maintaining code than you do writing it in the first place, so <strong>being more explicit in your code always trumps being clever</strong> to save a few lines. Always.</p>

<p>There&#8217;s the obvious places where people get clever, like algorithms or loops, but there&#8217;s plenty of other places too. Places where I wouldn&#8217;t really call it &#8220;being clever&#8221;, or at least I&#8217;m sure the original authors never thought they were trying to be clever when they wrote it. It was probably just quicker to write it in a certain way. For example, take this code:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var mappedEmployees <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>EmployeeViewModel<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var employee <span style="color: #0600FF;">in</span> _employeeRepository.<span style="color: #0000FF;">All</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">IsRetired</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span> <span style="color: #008000;">&amp;&amp;</span> x.<span style="color: #0000FF;">Salary</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">100000</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	mappedEmployees.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_employeeMapper.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span>employee<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>mappedEmployees<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>




<p>It&#8217;s not really hard to read, but it&#8217;s not really easy either. It might take you an extra second or two to figure out what&#8217;s going on when you first look at (even if you wrote it a few months ago), but multiply that by how many places you see code like this and how often you go back in to modify it (for new features, bugs, whatever). It adds up, quick. Written more explicitly, it might look something like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var mappedEmployees <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>EmployeeViewModel<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var nonRetiredHighEarningEmployees <span style="color: #008000;">=</span> _employeeRepository.<span style="color: #0000FF;">All</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">IsRetired</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span> <span style="color: #008000;">&amp;&amp;</span> x.<span style="color: #0000FF;">Salary</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">100000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var nonRetiredHighEarningEmployee <span style="color: #0600FF;">in</span> nonRetiredHighEarningEmployees<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var mappedEmployee <span style="color: #008000;">=</span> _employeeMapper.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span>nonRetiredHighEarningEmployee<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	mappedEmployees.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>mappedEmployee<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>mappedEmployees<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>




<p>You might call it verbose, but I&#8217;d say it&#8217;s a net gain. Each line is doing one thing. Yon can step through and read it without mentally pulling pieces apart. None of this &#8220;OK, that&#8217;s the mapped object call there, and its return is going into the collection there, and the whole thing is looping through that query there&#8221;. Things are given names and methods aren&#8217;t nested inside each other.</p>

<p>Always be on the lookout for &#8220;clever&#8221; areas in your code. Be explicit. Try to stick to each line doing one thing so there&#8217;s no hidden surprises.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/08/19/clever-vs-explicit/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/08/19/clever-vs-explicit/</feedburner:origLink></item>
		<item>
		<title>OCP in action</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/NEl8oJcKZ6k/</link>
		<comments>http://darrell.mozingo.net/2010/05/21/ocp-in-action/#comments</comments>
		<pubDate>Sat, 22 May 2010 02:04:58 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Design Principles]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=692</guid>
		<description><![CDATA[What is OCP?

The Open/Closed Principle is the O in the nearly infamous SOLID set of design principles. The gist of the principle is that any given class should be open for extension, but closed for modification. In more practical terms, you should be able to make most classes do something new or different without actually [...]]]></description>
			<content:encoded><![CDATA[<h3>What is OCP?</h3>

<p>The <a href="http://www.objectmentor.com/resources/articles/ocp.pdf">Open/Closed Principle</a> is the O in the nearly infamous <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">SOLID</a> set of design principles. The gist of the principle is that any given class should be open for extension, but closed for modification. In more practical terms, you should be able to make most classes do something new or different without actually changing a single line of code within the class itself.</p>

<p>That&#8217;s simple! Right? Ummm, sure. How, exactly, is a class supposed to be able to do more stuff without being modified? Well, there&#8217;s lots of ways, if you think about it. I mean, that&#8217;s sort of what inheritance and polymorphism were made for. There&#8217;s also other pretty neat ways, too. One of them shows the true beauty of inversion of control, and dependency injection frameworks.</p>

<p>Lets say you have a collection of customers. You need to loop through these people, running a few different checks on them (only preferred customers can carry a negative balance, credit cards on file aren&#8217;t expired, etc), and adding any resulting errors from those checks to another collection for display later. Nothing too terribly complicated.</p>

<h3>Solving without considering OCP</h3>

<p>First we have to load up the customers and send them on through:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var customers <span style="color: #008000;">=</span> get_customers_from_somewhere<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var check_runner <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Check_runner<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var warnings <span style="color: #008000;">=</span> check_runner.<span style="color: #0000FF;">run_checks_on</span><span style="color: #000000;">&#40;</span>customers<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var warning <span style="color: #0600FF;">in</span> warnings<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>warning<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> get_customers_from_somewhere<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>						<span style="color: #008080; font-style: italic;">// database, webservice, whatever.</span>
	<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
	       	<span style="color: #000000;">&#123;</span>
	       		<span style="color: #008000;">new</span> Customer
	       			<span style="color: #000000;">&#123;</span>
					name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Joe Smith&quot;</span>,
	       				credit_card <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Credit_card <span style="color: #000000;">&#123;</span> is_valid <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span> <span style="color: #000000;">&#125;</span>,
	       				balance <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span>,
	       				is_preferred <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
	       			<span style="color: #000000;">&#125;</span>,
	       		<span style="color: #008000;">new</span> Customer
	       			<span style="color: #000000;">&#123;</span>
					name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Nathan Hawes&quot;</span>,
	       				credit_card <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Credit_card <span style="color: #000000;">&#123;</span> is_valid <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span> <span style="color: #000000;">&#125;</span>,
	       				balance <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
	       				is_preferred <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
	       			<span style="color: #000000;">&#125;</span>,
	       		<span style="color: #008000;">new</span> Customer
	       			<span style="color: #000000;">&#123;</span>
					name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Melinda Plunkett&quot;</span>,
	       				credit_card <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Credit_card <span style="color: #000000;">&#123;</span> is_valid <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span> <span style="color: #000000;">&#125;</span>,
	       				balance <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">100</span>,
	       				is_preferred <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>
	       			<span style="color: #000000;">&#125;</span>
	       	<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>The class running all the checks:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Check_runner
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> IList<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> _warnings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> run_checks_on<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> customers<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var customer <span style="color: #0600FF;">in</span> customers<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			check_that_only_preferred_customer_can_have_a_negative_balance<span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			check_that_on_file_credit_card_is_not_expired<span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">// additional checks in the future...</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> _warnings<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> check_that_on_file_credit_card_is_not_expired<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>customer.<span style="color: #0000FF;">credit_card</span>.<span style="color: #0000FF;">is_valid</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			return<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		_warnings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Credit card expired for customer: &quot;</span> <span style="color: #008000;">+</span> customer.<span style="color: #0000FF;">name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> check_that_only_preferred_customer_can_have_a_negative_balance<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>customer.<span style="color: #0000FF;">is_preferred</span> <span style="color: #008000;">||</span> customer.<span style="color: #0000FF;">balance</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			return<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		_warnings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Negative balance for non preferred customer: &quot;</span> <span style="color: #008000;">+</span> customer.<span style="color: #0000FF;">name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Pretty standard. Loop through the customers, calling a separate private method for each check you need to preform, adding a message for that check&#8217;s error to a collection that ultimately gets returned to the caller for display.</p>

<h3>The problems with the first approach</h3>

<p>At first blush, this way of running the checks might seem very simple and understandable, but it starts to break down for a few different reasons:</p>

<ul>
<li>New checks could get pretty complicated, requiring access to other expensive objects (repositories, web services, file I/O, etc). Even if only one check needed a certain dependency, the whole <code>Check_runner</code> class is now burdened with that dependency.</li>
<li>Every new check requires you to open up the <code>Check_runner</code> class and making a modification. Opening a class <b>and</b> modifying it? That&#8217;s pretty much the definition of an OCP violation. Modifying a class always introduces the possibility for regression bugs. No matter how small the possibility or bug, they&#8217;re there.</li>
<li>Testing this thing as it gets larger and larger is going to be a pain in the rear. It&#8217;ll also get much harder, especially if outside dependencies are brought in (having to setup multiple dependencies when the one check you&#8217;re testing doesn&#8217;t even use them isn&#8217;t fun, or clear to read later).</li>
</ul>

<h3>One possible solution</h3>

<p>There&#8217;s a few different ways to go about fixing this. My suggestion would be to break each check into its own individual class, with the <code>Check_runner</code> taking them all in and looping through them, running each as it goes. It sounds a little more black-magicy than it really is. I&#8217;m going to show all the code first, then go over the benefits of an approach like this later on. Lets start by defining an interface for these check classes:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ICustomer_check
<span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">string</span> buildWarningFor<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #FF0000;">bool</span> failsFor<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Now we can define a single check, which knows when it fails for a given customer, and how to build a warning message for that failure. The check classes for the two checks that are ran above would be a simple conversion of the existing code:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Negative_balance_check <span style="color: #008000;">:</span> ICustomer_check
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> buildWarningFor<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;Negative balance for non preferred customer: &quot;</span> <span style="color: #008000;">+</span> customer.<span style="color: #0000FF;">name</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> failsFor<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>customer.<span style="color: #0000FF;">is_preferred</span> <span style="color: #008000;">&amp;&amp;</span> customer.<span style="color: #0000FF;">balance</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Expired_credit_card_check <span style="color: #008000;">:</span> ICustomer_check
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> buildWarningFor<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;Credit card expired for customer: &quot;</span> <span style="color: #008000;">+</span> customer.<span style="color: #0000FF;">name</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> failsFor<span style="color: #000000;">&#40;</span>Customer customer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>customer.<span style="color: #0000FF;">credit_card</span>.<span style="color: #0000FF;">is_valid</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Now the <code>Check_runner</code> just has to loop through all of the <code>ICustomer_check</code> implementations and run them:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Check_runner
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IEnumerable<span style="color: #008000;">&lt;</span>ICustomer_check<span style="color: #008000;">&gt;</span> _customer_checks<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Check_runner<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>ICustomer_check<span style="color: #008000;">&gt;</span> customerChecks<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_customer_checks <span style="color: #008000;">=</span> customerChecks<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> run_checks_on<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> customers<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		var warnings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var customer <span style="color: #0600FF;">in</span> customers<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var check <span style="color: #0600FF;">in</span> _customer_checks<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>check.<span style="color: #0000FF;">failsFor</span><span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					warnings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>check.<span style="color: #0000FF;">buildWarningFor</span><span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> warnings<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Again, pretty simple and focused. Where does that enumeration of <code>ICustomer_check</code> implementations come from though? The missing key: our dependency injection framework. I&#8217;ll use <a href="http://structuremap.github.com/structuremap/index.html">StructureMap</a> for this example. After downloading that and referencing the assembly, we&#8217;ll modify our <code>main</code> method to set it up:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	ObjectFactory.<span style="color: #0000FF;">Initialize</span><span style="color: #000000;">&#40;</span>y <span style="color: #008000;">=&gt;</span> y.<span style="color: #0000FF;">Scan</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span>
	                                     <span style="color: #000000;">&#123;</span>
	                                     	x.<span style="color: #0000FF;">TheCallingAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
						x.<span style="color: #0000FF;">AddAllTypesOf</span><span style="color: #008000;">&lt;</span>ICustomer_check<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	                                     <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var customers <span style="color: #008000;">=</span> get_customers_from_somewhere<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var check_runner <span style="color: #008000;">=</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>Check_runner<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var warnings <span style="color: #008000;">=</span> check_runner.<span style="color: #0000FF;">run_checks_on</span><span style="color: #000000;">&#40;</span>customers<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var warning <span style="color: #0600FF;">in</span> warnings<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>warning<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>We fire up StructureMap, telling it to scan the calling assembly and find all implementations of <code>ICustomer_check</code>. When we ask for an instance of <code>Check_runner</code>, StructureMap knows to provide all the implementations it found of <code>ICustomer_check</code> to <code>Check_runner</code>&#8217;s constructor argument in a list. Since this is the <a href="http://darrell.mozingo.net/2009/12/23/my-attempt-at-demystifying-dependency-injection/">outer most edge of the application</a>, we&#8217;ll interact with the dependency injection framework here instead of inside <code>Check_runner</code>.</p>

<h3>Benefits</h3>

<p>So perhaps other than the StructureMap related code (if you don&#8217;t already know the basics of it), nothing I&#8217;ve done here has really complicated the system. It&#8217;s still a few primitive classes working together in a fairly obvious way. What benefits do we gain from these changes though?</p>

<ul>
<li>Each piece of the system now has a single, specific responsibility. You can look at each check and quickly figure out what its purpose is. The runner simply takes in all the checks and runs them (funny how its name now follows its responsibility too).</li>
<li>The check classes can now take in their own dependencies. Need an <code>ICustomerRepository</code> or <code>ICustomerAccountService</code> for something? List it in the constructor. Each check is getting pulled from the container, so their dependencies will get filled as well. Checks will also only take on what each one needs, as opposed to requiring dependencies they might not have before.</li>
<li>With decreased responsibilities, each piece now becomes much easier to test. Supply a list of dummy checks and dummy customer to make sure the runner is doing its job. Same for the checks themselves. In fact, too many tests for a class is a smell that class is doing too much in the first place.</li>
<li>The point of the article: <b>no more OCP violations</b>! Future requirements for different kinds of checks now become almost mind numbingly easy. Slap in a new class and implement <code>ICustomer_check</code>. That&#8217;s it - the container will take care of the rest. Virtually no possibility of introducing a regression bug and messing up one of the other checks by adding a new one.</li>
</ul>

<h3>Conclusion</h3>

<p>One thing to remember when looking for OCP violations in your code base is that &#8220;closed for modifications&#8221; should be taken within context. Fixing bugs, adding complete new features, etc, will obviously require modifications to something. You&#8217;re not going to create every class in your system and never touch them again. Within reason, you should apply the Open-Closed Principle to your code as much as possible. It makes it simpler to understand on the micro and macro level once your familiar with some of the more common patterns, and it helps reduce the possibility of introducing bugs from future additions.</p>

<p>The code from this post is available <a href="http://darrell.mozingo.net/wp-content/uploads/2010/05/ocp_in_action.zip">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/05/21/ocp-in-action/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/05/21/ocp-in-action/</feedburner:origLink></item>
		<item>
		<title>Stakeholder editable content that automatically gets pushed live</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/o799715tt0U/</link>
		<comments>http://darrell.mozingo.net/2010/04/27/stakeholder-editable-content-that-automatically-gets-pushed-live/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 00:04:26 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Build Management]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=647</guid>
		<description><![CDATA[I finally got around to implementing help screens on our site recently. We needed a system that would enable our domain peeps to update the help text directly with no intervention from us, along with being easy to implement and maintain on our end. I ended up using flat HTML files and a jQuery modal [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got around to implementing help screens on our site recently. We needed a system that would enable our domain peeps to update the help text directly with no intervention from us, along with being easy to implement and maintain on our end. I ended up using flat HTML files and a jQuery modal dialog (<a href="http://colorpowered.com/colorbox/">Colorbox</a>), which has support for asynchronously loading those HTML files from disk when needed. The one thing we didn&#8217;t want to do with this solution was give our domain peeps production server access or the responsibility of keeping those HTML files up to date on the servers - I could only imagine the chaos that&#8217;d ensue from that.</p>

<p>Solution: use our build script &#038; build server to handle it for us.</p>

<p>We gave our domain peeps commit access to the repository - thankfully we&#8217;re still on SVN, as I&#8217;m sure their heads will explode when we switch to a DVCS. This provides nice versioning and accountability features if someone messes up (imagine that), and gives us a hook for the build server. All help files are contained in a hierarchy under a folder that&#8217;s appropriately named <code>HelpFiles</code>. I checked out just that folder from the source tree on their machines and gave them a quick commit/update spiel. We created empty HTML files for them, and they went about their way filling them all in.</p>

<p>Now on to the more interesting part, our build script. As I&#8217;ve mentioned previously, we&#8217;re using <a href="http://code.google.com/p/psake/">psake</a>. Here&#8217;s the relevant properties and task:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">properties {
	$scm_hidden_dir = &quot;.svn&quot;;
&nbsp;
	$executing_directory = new-object System.IO.DirectoryInfo $pwd
	$base_dir = $executing_directory.Parent.FullName
&nbsp;
	$source_dir = &quot;$base_dir\src&quot;
	$build_dir = &quot;$base_dir\build&quot;
	$build_tools_dir = &quot;$build_dir\tools&quot;
&nbsp;
	$share_web = &quot;wwwroot&quot;
	$servers_production = @(&quot;server1&quot;, &quot;server2&quot;)
&nbsp;
	$security_user = &quot;user_with_write_access&quot;
	$security_password = &quot;pa55w0rd&quot;
&nbsp;
	$tools_robocopy = &quot;$build_tools_dir\robocopy\robocopy.exe&quot;
&nbsp;
	$help_folder = &quot;HelpFiles&quot;
	$help_local_dir = &quot;$source_dir\$project_name.Web\$help_folder&quot;
	$deployTarget_help = &quot;$project_name\$help_folder&quot;
}
&nbsp;
task publish_help {
	foreach ($server in $servers_production)
	{
		$full_server_share = &quot;\\$server\$share_web&quot;
&nbsp;
		exec { &amp; net use $full_server_share /user:$security_user $security_password }
&nbsp;
		&amp; $tools_robocopy $help_local_dir $full_server_share\$deployTarget_help /xd $scm_hidden_dir /fp /r:2 /mir
&nbsp;
		# See page 33 of the help file in the tool's folder for exit code explaination.
		if ($lastexitcode -gt 3)
		{
			Exit $lastexitcode
		}
&nbsp;
		exec { &amp; net use $full_server_share /delete }
	}
}</pre></td></tr></table></div>




<p>There&#8217;s an array of production server names, which we iterate over and use the <code>net</code> command built into Windows to map its <code>wwwroot</code> share using a different username &amp; password than the current user (this allows the build server to run as an unprivileged user but still access needed resources).</p>

<p>Then we use the surprisingly awesome <a href="http://en.wikipedia.org/wiki/Robocopy">Robocopy</a> tool from Microsoft, which is basically xcopy on steroids, to copy out the help files themselves. The <code>xd</code> flag is excluding the hidden .svn folders, the <code>fp</code> flag is displaying full path names in the output (for display in the build output from TeamCity later), the <code>r</code> flag is telling it to only retry failed file twice (as opposed to the default <i>million</i> times!), and the <code>mir</code> flag is telling it to mirror the source directory tree to the destination, including empty folders and removing dead files.<p/>

<p>We can&#8217;t use psake&#8217;s built in <code>exec</code> function to run Robocopy, as exec only checks for non-zero return codes before considerng it a failure. Of course, just to be different, Robocopy only fails if its return code is above 3 (1 = one or more files copied successfully, 2 = extra files or folders detected, and there is no 3). So we check the return code ourselves and exit if Robocopy failed. We then delete the share, effectively making the machine forget the username/password associated with it.</p>

<p>With that done, we created a new build configuration in TeamCity and had it check the repository for changes only to the help file directory by adding <code>+:src/Project.Web/HelpFiles/**</code> to the Trigger Patterns field on the Build Triggers configuration step.</p>

<p>That&#8217;s pretty much it. Our domain peeps have been pretty receptive to it so far, and they love being able to edit the help files, commit, and see them live only a minute or two later. We loved not having to pull all that text from the database on each page load and not having to create editing/viewing/versioning/etc tools around such a process. It&#8217;s a win-win.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/04/27/stakeholder-editable-content-that-automatically-gets-pushed-live/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/04/27/stakeholder-editable-content-that-automatically-gets-pushed-live/</feedburner:origLink></item>
		<item>
		<title>2010 Goals - April Update</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/CHg0DcPsB3o/</link>
		<comments>http://darrell.mozingo.net/2010/04/03/2010-goals-april-update/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 14:30:52 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Goals]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=668</guid>
		<description><![CDATA[First quarter&#8217;s down. In full disclosure fashion, here&#8217;s how I&#8217;m doing so far:
Books

	Code Complete - Steve McConnell - Not started.
	Patterns of Enterprise Application Architecture - Martin Fowler - Not started.
	Applying Domain-Driven Design and Patterns - Jimmy Nilsson
	Working Effectively With Legacy Code - Michael Feathers - Not started.

Tools/Techniques/Processes @ Work

    Move from SVN [...]]]></description>
			<content:encoded><![CDATA[<p>First quarter&#8217;s down. In full disclosure fashion, here&#8217;s how I&#8217;m doing so far:</p>
<h3>Books</h3>
<ol>
	<li><a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670">Code Complete - Steve McConnell</a> - <b>Not started.</b></li>
	<li><a href="http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1229896370&amp;sr=8-1">Patterns of Enterprise Application Architecture - Martin Fowler</a> - <b>Not started.</b></li>
	<li><strike><a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202/ref=cm_cr_pr_product_top">Applying Domain-Driven Design and Patterns - Jimmy Nilsson</a></strike></li>
	<li><a href="http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1262481595&#038;sr=1-1">Working Effectively With Legacy Code - Michael Feathers</a> - <b>Not started.</b></li>
</ol>
<h3>Tools/Techniques/Processes @ Work</h3>
<ul>
    <li>Move from SVN to Git (and learn more about Git in the process). - <b>Learning about Git and bugging our sys admin about setting up Git, but haven&#8217;t actually started yet.</b></li>
    <li><strike>Move from CC.NET to Team City for our build server.</strike></li>
    <li>Build a more robust build script and management process - including production deployment and database migration scenarios. - <b>Well underway.</b></li>
</ul>
<h3>Involvement</h3>
<ul>
    <li>Develop an idea I was given for an open source project and get it live to see what happens. - <b>In progress.</b></li>
    <li>At least 24 blog posts (I&#8217;m not going to say 2 per month as I&#8217;m getting married this summer and I&#8217;m certain I won&#8217;t be able to maintain a schedule around it). - <b>4 down (ouch!).</b></li>
    <li>At least 3 feature/patch submissions to open source projects. - <b>Not started.</b></li>
</ul>
<h3>Coding</h3>
<ul>
    <li>Get a version 1 out there on at least 1 of the 3 product ideas I have floating around. - <b>Not started.</b></li>
    <li>Keep working on a good working knowledge of Ruby &#038; Ruby on Rails (and use it to build the product mentioned above). - <b>Playing and reading.</b></li>
</ul>
<p>So-so. Lets see about next quarter.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/04/03/2010-goals-april-update/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/04/03/2010-goals-april-update/</feedburner:origLink></item>
		<item>
		<title>Revisiting my automated build &amp; continuous integration setup</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/GbPPf212LUg/</link>
		<comments>http://darrell.mozingo.net/2010/04/02/revisiting-my-automated-build-continuous-integration-setup/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 23:03:47 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Build Management]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=651</guid>
		<description><![CDATA[A while back I wrote a small series on creating a basic build script and setting up a build server (part 1, part 2, and part 3). I used NAnt and CruiseControl.NET in that series, but alluded to a few other options for each. I recently got around to switching our build script from NAnt [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I wrote a small series on creating a basic build script and setting up a build server (<a href="http://darrell.mozingo.net/2008/08/28/automated-builds-continuous-integration-part-1/">part 1</a>, <a href="http://darrell.mozingo.net/2008/09/26/automated-builds-continuous-integration-part-2/">part 2</a>, and <a href="http://darrell.mozingo.net/2008/12/31/automated-builds-continuous-integration-part-3/">part 3</a>). I used <a href="http://nant.sourceforge.net/">NAnt</a> and <a href="http://confluence.public.thoughtworks.org/display/CCNET/Welcome+to+CruiseControl.NET">CruiseControl.NET</a> in that series, but alluded to a few other options for each. I recently got around to switching our build script from NAnt to <a href="http://code.google.com/p/psake/">psake</a>, which is written in <a href="http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx">PowerShell</a>, and switching our build server from CruiseControl.NET to JetBrain&#8217;s <a href="http://www.jetbrains.com/teamcity/">TeamCity</a>. I&#8217;ll give a quick overview of our new build script here, which I&#8217;ll use to build on in future posts showing a few of the more interesting things that suddenly became much easier with this setup, and in a few cases, possible at all.</p>

<p>To start with, you&#8217;ll want to make sure you have the latest versions of PowerShell (2.0) and psake. Here&#8217;s the basics of our build script:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">$ErrorActionPreference = 'Stop'
&nbsp;
Include &quot;.\functions_general.ps1&quot;
&nbsp;
properties {
	$project_name = &quot;MainApplication&quot;
	$build_config = &quot;Debug&quot;
}
&nbsp;
properties { # Directories
	$scm_hidden_dir = &quot;.svn&quot;;
&nbsp;
	$executing_directory = new-object System.IO.DirectoryInfo $pwd
	$base_dir = $executing_directory.Parent.FullName
&nbsp;
	$source_dir = &quot;$base_dir\src&quot;
&nbsp;
	$build_dir = &quot;$base_dir\build&quot;
	$tools_dir = &quot;$base_dir\tools&quot;
	$build_tools_dir = &quot;$build_dir\tools&quot;
&nbsp;
	$build_artifacts_dir = &quot;$build_dir\artifacts&quot;
	$build_output_dir = &quot;$build_artifacts_dir\output&quot;
	$build_reports_dir = &quot;$build_artifacts_dir\reports&quot;
&nbsp;
	$transient_folders = @($build_artifacts_dir, $build_output_dir, $build_reports_dir)
}
&nbsp;
properties { # Executables
	$tools_nunit = &quot;$tools_dir\nunit\nunit-console-x86.exe&quot;
	$tools_gallio = &quot;$tools_dir\Gallio\Gallio.Echo.exe&quot;
	$tools_coverage = &quot;$build_tools_dir\ncover\ncover.console.exe&quot;
	$tools_coverageExplorer = &quot;$build_tools_dir\ncover_explorer\NCoverExplorer.Console.exe&quot;
}
&nbsp;
properties { # Files
	$solution_file = &quot;$source_dir\$project_name.sln&quot;
&nbsp;
	$output_unitTests_dll = &quot;$build_output_dir\$project_name.UnitTests.dll&quot;
	$output_unitTests_xml = &quot;$build_reports_dir\UnitTestResults.xml&quot;
	$output_coverage_xml = &quot;$build_reports_dir\NCover.xml&quot;
	$output_coverage_log = &quot;$build_reports_dir\NCover.log&quot;
	$output_coverageExplorer_xml = &quot;$build_reports_dir\NCoverExplorer.xml&quot;
	$output_coverageExplorer_html = &quot;$build_reports_dir\NCover.html&quot;
}
&nbsp;
properties { # Skip coverage attributes
	$skipCoverage_general = &quot;Testing.SkipTestCoverageAttribute&quot;
}
&nbsp;
task default -depends unit_test_coverage
&nbsp;
task clean {
	$transient_folders | ForEach-Object { delete_directory $_ }
	$transient_folders | ForEach-Object { create_directory $_ }
}
&nbsp;
task compile -depends clean {
	exec { msbuild $solution_file /p:Configuration=$build_config /p:OutDir=&quot;&quot;$build_output_dir\\&quot;&quot; /consoleloggerparameters:ErrorsOnly }
}
&nbsp;
task unit_test_coverage -depends compile {
	exec { &amp; $tools_coverage $tools_nunit $output_unitTests_dll /nologo /xml=$output_unitTests_xml //reg //ea $skipCoverage_general //l $output_coverage_log //x &quot;$output_coverage_xml&quot; //a $project_name }
	exec { &amp; $tools_coverageExplorer $output_coverage_xml /xml:$output_coverageExplorer_xml /html:$output_coverageExplorer_html /project:$project_name /report:ModuleClassFunctionSummary /failMinimum }
}</pre></td></tr></table></div>




<p>As the second line alludes to, you can break functions out into separate files and include them back into the main one. Here&#8217;s <code>functions_general.ps1</code>:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">function delete_directory($directory_name)
{
	Remove-Item -Force -Recurse $directory_name -ErrorAction SilentlyContinue
}
&nbsp;
function create_directory($directory_name)
{
	New-Item $directory_name -ItemType Directory | Out-Null
}</pre></td></tr></table></div>




<p>This script will build our project and run the unit tests, producing a coverage report we can display later inside Team City. Much of this maps loosely one to one against the NAnt version discussed in my past series, and there&#8217;s plenty of articles/posts online explaining this stuff in much more detail than I can here. Note that all the pieces that can &#8220;fail&#8221; the script are wrapped in <code>exec</code>, which will execute the code block (i.e. lambda/anonymous delegate) and basically alert the build server if it fails. Not too difficult, at least for now <img src='http://darrell.mozingo.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p>As for getting this to work with Team City, if you specify the runner as a command line and point it at a batch file with these contents:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="batch" style="font-family:monospace;">@echo off
cls
powershell -Command &quot;&amp; { Set-ExecutionPolicy Unrestricted; Import-Module .\build\tools\psake\psake.psm1; $psake.use_exit_on_error = $true; Invoke-psake '.\build\build.ps1' %*; Remove-Module psake}&quot;</pre></td></tr></table></div>




<p>You&#8217;ll be golden. This batch allows the build server to run the script (perhaps setting unrestricted execution isn&#8217;t the smartest from a security standpoint, but oh well), sets up the psake environment, tells psake to raise its warnings in a way that TeamCity can pick up on, executes your build script, and tears down the psake environment. Looks a little complicated, but it&#8217;s just a bunch of smaller commands strung together on one line, and you shouldn&#8217;t have to look at it again.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/04/02/revisiting-my-automated-build-continuous-integration-setup/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/04/02/revisiting-my-automated-build-continuous-integration-setup/</feedburner:origLink></item>
		<item>
		<title>Juggling Git with multiple accounts</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/y5dSUcjhTEE/</link>
		<comments>http://darrell.mozingo.net/2010/03/05/juggling-git-with-multiple-accounts/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 23:10:29 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Source Control]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=617</guid>
		<description><![CDATA[We&#8217;re finally getting a Git server setup at work. Since I also happen to do a bit of open source contributing at work (usually via submitting patches for issues that effect us) and virtually every open source project and its brother is moving to Github lately, I could already see juggling these two systems would [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re finally getting a Git server setup at work. Since I also happen to do a bit of open source contributing at work (usually via submitting patches for issues that effect us) and virtually every open source project and its brother is moving to <a href="https://github.com/">Github</a> lately, I could already see juggling these two systems would be an issue. I can&#8217;t use the same SSH key for both our internal Git server and Github for various reasons, and I wouldn&#8217;t I want to, really. Fortunately, it turns out managing multiple accounts with Git, and SSH in general, isn&#8217;t too bad.</p>

<p>I&#8217;ll walk you through doing this, assuming you don&#8217;t already have an SSH key or Github account (if you do, though, it&#8217;s no biggie, just make sure to back everything up and swap out a few obvious steps below).</p>

<ol>
  <li>Sign up with <a href="http://github.com">Github</a>.</li>
  <li>Download &#038; install <a href="http://code.google.com/p/msysgit/">msysgit</a> (chose to use the OpenSSH instead of Tortoise/Putty&#8217;s PLink when prompted).</li>
  <li>Open a Git Bash prompt (an icon is placed in the Start menu during msysgit&#8217;s installation).</li>
  <li>
    Create your SSH key pairs, doing the following steps once for each email/account (internal work &#038; external public ones, for instance):
    <ol>
      <li><code>ssh-keygen -t rsa -C "public_or_private@email.com"</code></li>
      <li>When it asks for the file to save to, specify the same folder it&#8217;s suggesting for the default one (i.e. <code>/c/Users/dmozingo/.ssh/</code>) but name the file something different, based on whether it&#8217;s for the public or private key (maybe something like <code>id_rsa_github</code> and <code>id_rsa_work</code>).</li>
      <li>Enter a passphrase. I usually leave it blank as other guides suggest, trusting (perhaps mistakingly?) Window&#8217;s folder security to keep it safe for me.</li>
    </ol>
  </li>
  <li>Navigate to the folder where you just created your SSH key pairs, and create a file named <code>config</code>.</li>
  <li>
    Open the file in notepad and enter the following:
<pre>
Host github.com
	HostName github.com
	User git
	PreferredAuthentications publickey
	IdentityFile /Users/dmozingo/.ssh/id_rsa_github

Host work
	HostName workGitServername
	User dmozingo
	IdentityFile /Users/dmozingo/.ssh/id_rsa_work
</pre>
   You&#8217;ll have to do a few substitutions in the file, mainly for the work <code>HostName</code> and both section&#8217;s <code>IdentityFile</code>&#8217;s, if they&#8217;re different for your setup. The first line of each section specified the name you&#8217;ll use in &#8220;addressing&#8221; your Git commands later. By keeping the first one github.com, you won&#8217;t have to change anything when you follow along with other guides online.
  </li>
  <li>Now give GitHub the contents of your public/GitHub SSH key file (the id_rsa_github.pub from my example). You can do this on your account settings page.</li>
  <li>Still at the Git Bash prompt, enter <code>ssh-agent.exe bash</code> to start the key manager. <b>You&#8217;ll have to do this each time you use Git</b>, unless someone (please?) can tell me a better way.</li>
  <li>Enter <code>ssh-add.exe ~/.ssh/id_rsa_github</code>, where the tilday (~) represents your home directory (C:\Users\username for Vista/7, C:\Documents and Settings\username for XP). This should point to the public SSH key file you made earlier. <b>You&#8217;ll also have to do this each tiem you use Git</b>, again, unless someone can set me straight here.</li>
</ol>

<p>That should be it! Test it out by entering <code>ssh git@github.com</code>. You should see something like this:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2010/03/github_ssh_success.png" alt="Github SSH success" />

<p>So while it&#8217;s definitely harder than not having to juggling multiple accounts, it&#8217;s not as bad as it sounds. Know a better way to do this? Please let me know!</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/03/05/juggling-git-with-multiple-accounts/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/03/05/juggling-git-with-multiple-accounts/</feedburner:origLink></item>
		<item>
		<title>Naming</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/6MMlT3dqoKg/</link>
		<comments>http://darrell.mozingo.net/2010/02/07/naming/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:36:30 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Musings]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=27</guid>
		<description><![CDATA[Alright people, we&#8217;re not on 8088&#8217;s with 20M hard drives anymore. It&#8217;s OK, and in fact recommended, to stretch out your variable and method names so they make sense. We don&#8217;t need variables with names like x, col, or cAcctNum anymore. Use Intellisense and don&#8217;t be afraid to type a bit when you first enter [...]]]></description>
			<content:encoded><![CDATA[<p>Alright people, we&#8217;re not on 8088&#8217;s with 20M hard drives anymore. It&#8217;s OK, and in fact recommended, to stretch out your variable and method names so they make sense. We don&#8217;t need variables with names like <code>x</code>, <code>col</code>, or <code>cAcctNum</code> anymore. Use Intellisense and don&#8217;t be afraid to type a bit when you first enter the names. Meaningful names like <code>employeeCount</code> are good, as is getting rid of stupid one-off abbreviations and instead opting for naming like <code>column</code> and <code>clientAccountNumber</code>.</p>

<p>By using a shortened variable or method name, you&#8217;re forcing some other developer in the future (who often ends up being yourself) to mentally substitute that shortened version in their head each time they see it while reading through your code. Lets face it, our code is usually hard enough to understand long after we&#8217;ve written it just on its own, so strive to do everything possible to more quickly and easily figure it out in the future. Those small substation times accumulate pretty quick when you&#8217;re trying to find a bug in foreign code.</p>

<p>Queue the contrived example:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> AddEmp<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> fname, <span style="color: #FF0000;">string</span> lname, <span style="color: #FF0000;">decimal</span> rate, Dept dept<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var emp <span style="color: #008000;">=</span> EmpFact.<span style="color: #0000FF;">CreateEmp</span><span style="color: #000000;">&#40;</span>fname, lname, rate, dept<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Employess</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>emp<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var emp <span style="color: #0600FF;">in</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Employees</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		x<span style="color: #008000;">++;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> x<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Yea yea, like I said, contrived. Anyway, while you can probably tell somewhat easily what&#8217;s going on there, you can see there&#8217;s a lot of common and not so common abbreviations and shortened names being used. <code>Dept</code>? <code>x</code>? What they hell do they do? Lets try this again without being quite so lazy:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> AddEmployee<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> firstName, <span style="color: #FF0000;">string</span> lastName, <span style="color: #FF0000;">decimal</span> payRate, Department department<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var newEmployee <span style="color: #008000;">=</span> EmployeeFactory.<span style="color: #0000FF;">CreateEmployee</span><span style="color: #000000;">&#40;</span>firstName, lastName, payRate, department<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Employess</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>newEmployee<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var newEmployeeCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var employee <span style="color: #0600FF;">in</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Employees</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		newEmployeeCount<span style="color: #008000;">++;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> newEmployeeCount<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>A bit better, I&#8217;d say. On your first read through this code it&#8217;s easier to see what&#8217;s going on and what each piece plays in the puzzle. You don&#8217;t have to stop for a split second to think &#8220;first name&#8221; when you see &#8220;fname&#8221;. You just read &#8220;first name&#8221;.</p>

<p>I know a lot of people scoff at longer names when they first see them (like <code>FindAllCustomersInStateNotUsingDiscountWhenAvailable</code>), but that&#8217;s cool. It is weird at first, but you get used to it. Even more foreign to most developers is using underlines for spacing: <code>Find_all_customers_in_state_not_using_discount_when_available</code>. I personally find that version even more readable as you don&#8217;t have to mentally parse the words, but it&#8217;s a hard pill for a lot of developers to swallow.</p>

<p>Regardless of how you do it, the next time you find yourself using a quick abbreviation or some meaningless variable/method name, <strong>please</strong> do your future self and other developers a favor by putting some meaning into it.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/02/07/naming/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/02/07/naming/</feedburner:origLink></item>
		<item>
		<title>In-memory view rendering with Spark</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/RH3BqqRs38I/</link>
		<comments>http://darrell.mozingo.net/2010/01/28/in-memory-view-rendering-with-spark/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 22:45:59 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=409</guid>
		<description><![CDATA[We recently had a requirement to start printing some documents from our web application. These documents required some very precise positioning of a few elements that can&#8217;t be achieved with standard web browser printing capabilities. After weighing our options, we decided to go the generated PDF route. There&#8217;s quite a few HTML -&#62; PDF generation [...]]]></description>
			<content:encoded><![CDATA[<p>We recently had a requirement to start printing some documents from our web application. These documents required some very precise positioning of a few elements that can&#8217;t be achieved with standard web browser printing capabilities. After weighing our options, we decided to go the generated PDF route. There&#8217;s quite a few HTML -&gt; PDF generation options out there, but they almost all require you to either point them at an HTML document on disk or feed them a string of your HTML. Options, options, options. Yea, I think we&#8217;ll take the string route, thanks.</p>

<p>Turns out (surprise surprise!) that getting your rendered HTML from the Web Forms view engine that&#8217;s used by default in ASP.NET MVC isn&#8217;t so, um, easy. It likes its HttpContext with a side of HttpSession, and that&#8217;s just the appetizer. Kind of hard to get that to it in-memory without firing up a whole other server instance. Thankfully, it turns out our good friend <a href="http://sparkviewengine.com/">Spark</a> makes rendering a view to an HTML string in-memory incredibly easy.</p>

<p>How? Glad you asked:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> RenderViewToHtml<span style="color: #008000;">&lt;</span>VIEW_MODEL<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> viewPathAndName, VIEW_MODEL viewModel<span style="color: #000000;">&#41;</span> where VIEW_MODEL <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #000000;">&#123;</span>
	var templatesLocation <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileSystemViewFolder<span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Server</span>.<span style="color: #0000FF;">MapPath</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Views&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var viewEngine <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SparkViewEngine<span style="color: #000000;">&#40;</span>BuildSparkSettings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> ViewFolder <span style="color: #008000;">=</span> templatesLocation <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
	var descriptor <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SparkViewDescriptor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddTemplate</span><span style="color: #000000;">&#40;</span>viewPathAndName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var view <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>SparkView<span style="color: #008000;">&lt;</span>VIEW_MODEL<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>viewEngine.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>descriptor<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	view.<span style="color: #0000FF;">ViewData</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ViewDataDictionary<span style="color: #008000;">&lt;</span>VIEW_MODEL<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>viewModel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #FF0000;">string</span> html<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var writer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringWriter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		view.<span style="color: #0000FF;">RenderView</span><span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		html <span style="color: #008000;">=</span> writer.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> html<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SparkSettings BuildSparkSettings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SparkSettings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		.<span style="color: #0000FF;">AddNamespace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;System.Linq&quot;</span><span style="color: #000000;">&#41;</span>
		.<span style="color: #0000FF;">AddNamespace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;System.Web.Mvc&quot;</span><span style="color: #000000;">&#41;</span>
		.<span style="color: #0000FF;">AddNamespace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Microsoft.Web.Mvc&quot;</span><span style="color: #000000;">&#41;</span>
		.<span style="color: #0000FF;">SetPageBaseType</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>SparkView<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		.<span style="color: #0000FF;">SetDebug</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>Simply pass in a path to your view (minus the <code>/View</code> part) along with a view model and you&#8217;ll get back a string full of rendered HTML goodness. The <code>BuildSparkSettings()</code> method can shared with the application startup code where you create and add Spark as an ASP.NET MVC view engine. Here&#8217;s a sample call:</p>

<p>It&#8217;s worth noting that Spark and WebForms views can happily live side-by-side in a single project, too. We use this for only a hand full of pages and the rest are still using the WebForms view engine. Plus, converting them to Spark was as simple as renaming the file and adding a view model declaration at the top of the page (along the lines of <code>&lt;viewdata model="OurNeatViewModel" /&gt;</code>). Granted these pages aren&#8217;t really leveraging the power and beauty of Spark, but they still run and with virtually no modifications.</p>

<p>Will we be converting all of our views to Spark and using some of it&#8217;s neat-o features and conventions? Probably not any time soon. While lots of folks are apparently feeling lots of pain with Web Forms views, we&#8217;re not (well, other than this whole in-memory rendered affair anyway). So there really isn&#8217;t much gain for us from switching over. I also really don&#8217;t like the non-strongly-typedness of their views, which already bit me a few times just on the hand full of views we&#8217;re using it for. Perhaps that might get fixed or ReSharper will step up with support for it.</p>

<p>Spark does have a lot of neat features though. It&#8217;s ability to easily render a full view to an HTML string in-memory was just the first thing we needed from it. I&#8217;m sure there&#8217;ll be more in the future.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/01/28/in-memory-view-rendering-with-spark/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/01/28/in-memory-view-rendering-with-spark/</feedburner:origLink></item>
		<item>
		<title>Downtime</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/SlU_vBlLn60/</link>
		<comments>http://darrell.mozingo.net/2010/01/16/downtime/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 17:43:38 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Misc.]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=582</guid>
		<description><![CDATA[My home-ran Linux server crashed shortly after the first of the year. I&#8217;ve gotten really good up time with it since I started using it 6 years ago, with only a power supply dieing on me once, and other than that having gone years at a time without having to do a single thing - [...]]]></description>
			<content:encoded><![CDATA[<img src="http://darrell.mozingo.net/wp-content/uploads/2010/01/downtime.png" alt="downtime" title="downtime" style="float: right;" /><p>My home-ran Linux server crashed shortly after the first of the year. I&#8217;ve gotten really good up time with it since I started using it 6 years ago, with only a power supply dieing on me once, and other than that having gone years at a time without having to do a single thing - even rebooting. Well, I&#8217;m thinking the motherboard gave out this time, and I really don&#8217;t feel like messing with the server any more between software updates and hardware issues (plus it was hosted in my company&#8217;s data center, and with a few of the ideas I&#8217;m hoping to bring to fruition this year, I don&#8217;t want to overstay my welcome there!).</p>

<p>So I moved to an actual hosted solution with <a href="http://www.site5.com/">Site5</a>. I never realized it&#8217;d be so hard to find a freakin&#8217; provider! I understand it&#8217;s probably the nature of the beast, but it seems like there&#8217;s no single provider out there that damn near everyone just <i>loves</i>, even as you move up the price scale. For every story about someone being with a host for years without a single problem, there&#8217;s one where they guy&#8217;s site was down every other day and he lost half of his data in a crash. Eek! I&#8217;ve have heard good things about Site5&#8217;s Ruby on Rails hosting and their uptime in general, however, plus they provide SVN &#038; Git on all their servers so I don&#8217;t have to worry about hosting that separately with <a href="http://github.com/">GitHub</a> or <a href="http://unfuddle.com/">Unfuddle</a>, so I took the plunge for a year to try them out. Pretty good so far, though there were a few hiccups such as having to move my DNS hosting to them and at least one server outage. Guess I&#8217;ll see how it goes.</p>

<p>Anyway, that&#8217;s why the site was done for two weeks or so. Hopefully it shant happen again any time soon.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/01/16/downtime/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/01/16/downtime/</feedburner:origLink></item>
		<item>
		<title>2010 Goals</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/ppGuj6QPwZE/</link>
		<comments>http://darrell.mozingo.net/2010/01/03/2010-goals/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 03:14:00 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Goals]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=571</guid>
		<description><![CDATA[Time to lay out some professional and coding related goals for 2010:
Books

	Code Complete - Steve McConnell
	Patterns of Enterprise Application Architecture - Martin Fowler
	Applying Domain-Driven Design and Patterns - Jimmy Nilsson
	Working Effectively With Legacy Code - Michael Feathers

Tools/Techniques/Processes @ Work

    Move from SVN to Git (and learn more about Git in the process).
 [...]]]></description>
			<content:encoded><![CDATA[<p>Time to lay out some professional and coding related goals for 2010:</p>
<h3>Books</h3>
<ol>
	<li><a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670">Code Complete - Steve McConnell</a></li>
	<li><a href="http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1229896370&amp;sr=8-1">Patterns of Enterprise Application Architecture - Martin Fowler</a></li>
	<li><a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202/ref=cm_cr_pr_product_top">Applying Domain-Driven Design and Patterns - Jimmy Nilsson</a></li>
	<li><a href="http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1262481595&#038;sr=1-1">Working Effectively With Legacy Code - Michael Feathers</a></li>
</ol>
<h3>Tools/Techniques/Processes @ Work</h3>
<ul>
    <li>Move from SVN to Git (and learn more about Git in the process).</li>
    <li>Move from CC.NET to Team City for our build server.</li>
    <li>Build a more robust build script and management process - including production deployment and database migration scenarios.</li>
</ul>
<h3>Involvement</h3>
<ul>
    <li>Develop an idea I was given for an open source project and get it live to see what happens.</li>
    <li>At least 24 blog posts (I&#8217;m not going to say 2 per month as I&#8217;m getting married this summer and I&#8217;m certain I won&#8217;t be able to maintain a schedule around it).</li>
    <li>At least 3 feature/patch submissions to open source projects.</li>
</ul>
<h3>Coding</h3>
<ul>
    <li>Get a version 1 out there on at least 1 of the 3 product ideas I have floating around.</li>
    <li>Keep working on a good working knowledge of Ruby &#038; Ruby on Rails (and use it to build the product mentioned above).</li>
</ul>
<p>Pretty similar to last years goals overall, actually. I&#8217;ll be updating my progress every 3 months instead of 2 this year though.</p>
<p>Set any goals yourself this year?</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/01/03/2010-goals/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/01/03/2010-goals/</feedburner:origLink></item>
		<item>
		<title>2009 Goals - Year End Update</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/Fk1ecEAXRLE/</link>
		<comments>http://darrell.mozingo.net/2010/01/02/2009-goals-year-end-update/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 17:45:28 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Goals]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=558</guid>
		<description><![CDATA[2009&#8217;s over. How did I do on my goals for last year? Let&#8217;s see (comments in italics after each goal):
Books

	Code Complete - Steve McConnell (yep, I&#8217;ve never actually read it)&#160;&#160;&#160; No progress.
	Patterns of Enterprise Application Architecture - Martin Fowler&#160;&#160;&#160; No progress.
	Domain Driven Design - Erick Evans&#160;&#160;&#160; Done.
	Working Effectively With Legacy Code - Michael Feathers&#160;&#160;&#160; No [...]]]></description>
			<content:encoded><![CDATA[<p>2009&#8217;s over. How did I do on my <a href="http://darrell.mozingo.net/2009/01/02/new-year-new-goals-2009/">goals for last year</a>? Let&#8217;s see (comments in italics after each goal):</p>
<h3>Books</h3>
<ol>
	<li><a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670">Code Complete - Steve McConnell</a> (yep, I&#8217;ve never actually read it)&nbsp;&nbsp;&nbsp; <em>No progress.</em></li>
	<li><a href="http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1229896370&amp;sr=8-1">Patterns of Enterprise Application Architecture - Martin Fowler</a>&nbsp;&nbsp;&nbsp; <em>No progress.</em></li>
	<li><strike><a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1229896453&amp;sr=1-1">Domain Driven Design - Erick Evans</a></strike>&nbsp;&nbsp;&nbsp; <em>Done.</em></li>
	<li><a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1229896657&amp;sr=1-1">Working Effectively With Legacy Code - Michael Feathers</a>&nbsp;&nbsp;&nbsp; <em>No progress.</em></li>
</ol>
<h3>Tools/Techniques/Processes @ Work</h3>
<ul>
    <li><strike>NHibernate and all its trimmings (FluentNHibernate, LINQ to NHibernate, etc), which I&#8217;ll need on a project here real soon.</strike>&nbsp; <em>Done.</em></li>
    <li>Actually move from CC.NET to Team City (last attempt didn&#8217;t go so well).&nbsp; <em>No progress.</em></li>
    <li>Build a more robust build script and management process - including production deployment scenarios.&nbsp;&nbsp; <em>No progress.</em></li>
    <li>Messaging framework, either <a href="http://code.google.com/p/masstransit/">MassTransit</a>, <a href="http://www.nservicebus.com/">NServiceBus</a>, or Ayende&#8217;s new <a href="http://ayende.com/Blog/archive/2008/12/17/rhino-service-bus.aspx">Rhino Service Bus</a> if he&#8217;s able to release it in time for our current project.&nbsp; <em>Didn&#8217;t need it, so no progress.</em></li>
</ul>
<h3>Involvement</h3>
<ul>
    <li>We have weekly meetings to catch everyone up on what we&#8217;re doing, but I want to present on an actual topic during at least 2 of these - and present in a way where the other development teams will see the use in picking up the presented tool/technique.&nbsp;&nbsp;&nbsp; <em>Meetings still halted - no progress.</em></li>
    <li>Have at least one meeting of the <a href="http://www.cantonalt.net">CantonALT.NET</a> group and see if it can ever get up on its feet.&nbsp;&nbsp; <em>No progress, and more than likely dead in the water <img src='http://darrell.mozingo.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </em></li>
    <li>The two blog posts per month target John put out there seems doable, so I&#8217;ll borrow it :)&nbsp;&nbsp;&nbsp; <em>Spot on, but no posts during October or November. I&#8217;ll consider this goal missed, then.</em></li>
    <li><strike>At least 3 feature/patch submissions to open source projects.</strike>&nbsp; <em>Done.</em></li>
</ul>
<h3>Productivity</h3>
<ul>
    <li><strike>Trim RSS feed size by 1/3 (presently at 127, so trim it to at least 85) and cut time viewing it by half. I&#8217;ve gotten much better this past year at cutting through the useless stuff, but there&#8217;s still a lot there and it sucks up too much of my time.</strike>&nbsp; <em>Done.</em></li>
</ul>
<p>So, overall I completed 4 of my 13 goals - 31%. Ouch. To be fair, 2 of the goals were out of my hands (didn&#8217;t need a messaging framework at work yet and didn&#8217;t want to think up some rinky-dink example project to try it on, and our weekly meetings were cancelled for me to present - though I could have found another venue to present). I was also <i>pretty</i> close on the blog posting goal, only missing a few because I ran short on ideas for a couple of months there.</p>
<p>Ah well, at least I gave them all a shot and completed at least one from each category. I also knocked off a lot of my personal goals for the year, so they sort of balanced out. I&#8217;ll post my code related goals for the upcoming year later this week.</p>
<p>How&#8217;d everyone else do? Have any new ones for this year?</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2010/01/02/2009-goals-year-end-update/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2010/01/02/2009-goals-year-end-update/</feedburner:origLink></item>
		<item>
		<title>My attempt at demystifying dependency injection</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/CWAFlfDE6DE/</link>
		<comments>http://darrell.mozingo.net/2009/12/23/my-attempt-at-demystifying-dependency-injection/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 16:22:57 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Design Principles]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=418</guid>
		<description><![CDATA[I think I can safely say I finally &#8220;get&#8221; dependency injection (DI) and the need for a framework (such as StructureMap, Ninject, Windsor, or any other). More importantly, I think I finally get the best way to use it in an application. Its taken me a bit to get to this point, and almost everything [...]]]></description>
			<content:encoded><![CDATA[<p>I think I can safely say I finally &#8220;get&#8221; dependency injection (DI) and the need for a framework (such as <a href="http://structuremap.sourceforge.net">StructureMap</a>, <a href="http://ninject.org/">Ninject</a>, <a href="">Windsor</a>, or <a href="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx">any other</a>). More importantly, I think I finally get the best way to use it in an application. Its taken me a bit to get to this point, and almost everything I&#8217;ve read and heard on the subject was very <i>hand-wavy</i>, at least to me. So here&#8217;s my attempt at demystifying the subject along with a straight forward way to go about using it in your application, something I wish I could have heard a while ago.</p>

<h2>What is it?</h2>

<p>Many objects have an outside dependency of some sort. Instead of creating the dependency inside your class (by doing something like <code>myDependency = new Dependency()</code>), you want these dependencies to be &#8220;injected&#8221; in, usually by the constructor:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OrderProcessingService
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IRepository _repository<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> OrderProcessingService<span style="color: #000000;">&#40;</span>IRepository repository<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_repository <span style="color: #008000;">=</span> repository<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>




<p>That&#8217;s it. Seriously. It&#8217;s not hard to grasp, and you&#8217;re probably already doing it, but the trick for me was figuring out how to actually go about using this in any sort of sane and recommended way, as you&#8217;ll notice the requirement is now on the caller to provide an instance of <code>IRepository</code>. If you want more details on this pattern, <a href="http://www.google.com/search?q=dependency+injection">there&#8217;s plenty out there</a>.</p>

<h2>Why should I bother using it?</h2>

<p>
<ol>
<li><strong>It takes handling the dependency&#8217;s life cycle out of your hands.</strong> Perhaps you want your database access class to stick around for a whole web request, another object to be a singleton, and another to be per thread when used in a Windows app, but per request in a Web app? Using a proper DI framework/container, you don&#8217;t have to worry about writing anything to support that, and changing the lifespan of a given object is a one line (and usually even enumeration value) change.</li>
<li><strong>It loosens up your code.</strong> You&#8217;re no longer &#8220;newing&#8221; up your data access or web service classes right in the middle of your operation. Swapping out implementations of, say, an interface, is a simple operation that&#8217;s located in one place. Just stick the dependency in your constructor, and you&#8217;re good to go. I&#8217;ve actually used this in quite a few places beyond the academic and largely unused-in-the-real-world &#8220;switching from Ms SQL to Oracle&#8221; examples, too.</li>
<li><strong>Greatly eases and simplifies unit testing.</strong> In many cases, using dependency injection is the only way to unit test portions of your code base (unless you use <a href="http://site.typemock.com/index2/">a certain tool to do it for you</a>). By taking your dependencies in your constructor, you&#8217;re giving your unit tests a seam to inject fake implementation of these dependencies. This lets you skip actually hitting the database, web service, hard drive, or anything else that would kill the running time of a unit test or be almost impossible to setup and control in a repeatable manner.</li>
</ol>

If you&#8217;re looking for more detailed reasons, you&#8217;ll again want to refer to the <a href="http://www.google.com/search?q=dependency+injection">gobs of information</a> already out there.
</p>

<h2>How can I use it?</h2>

<p>Ah, now for the juicy part I know you&#8217;re all dying to hear: how the hell to actually use the pattern in conjunction with one of the tools I mentioned at the beginning of the post. Before giving away the answer, let&#8217;s quickly go over the three primary ways to use a DI container in your app:
<ol>
<li>
<strong>Service locator:</strong> this pattern is generally considered a no-no, as it still burys your dependencies deep inside your code. Sure, you can swap them out when needed for unit testing, but they&#8217;re still very opaque and will almost certainly get very hard to work with, very fast:
<br /><br />


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ProcessOrder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var repository <span style="color: #008000;">=</span> IoC.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Do stuff with repository.</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>



In the above example, <code>IoC.Resolve</code> is a simple static method that delegates to whatever DI framework you&#8217;re using. Callers won&#8217;t know about this dependency, though, and without fully boot strapping your framework in your unit test (icky) or injecting a fake into your container, the call will either throw or return null, neither of which you want to be checking for everywhere.<br /><br />
</li>
<li>
<strong>Poor-man&#8217;s dependency injection:</strong> This is a slight twist on normal constructor DI. You have one empty constructor for most of the program to use, which delegates to a &#8220;loaded&#8221; constructor that unit tests use. While making the dependencies clear, this removes lifetime management from the container&#8217;s hands, and also gets ugly when you start changing dependencies around. This is usually used in conjunction with the service locator pattern above:
<br /><br />


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OrderProcessingService
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IRepository _repository<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> OrderProcessingService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span>IoC.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> OrderProcessingService<span style="color: #000000;">&#40;</span>IRepository repository<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_repository <span style="color: #008000;">=</span> repository<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>



</li>
<li><strong>True dependency injection:</strong> Classes generally have only one constructor which takes in all the required dependencies (see the first code snippet at the top of the post).</li>
</ol>
#3, true dependency injection, is the one I had no idea how to go about setting up in my app. Everyone said not to use the service locator pattern or poor-man&#8217;s dependency injection, but how was I supposed to <i>not</i> use them and still get everything injected in? It seems like I was never supposed to call my DI container&#8217;s <code>Resolve</code> method. So what gives? Every time someone got close to answering it, it seemed like they&#8217;d blow off the question. Ugh.
</p>

<p>After enough playing around, reading, and looking at other open source projects, though, it finally clicked: <strong>only call Resolve at the furthest edges of your application, and as few times as possible</strong>. So what does that mean and where should you be calling it in your app? Well&#8230; it depends.</p>

<h4>Wait, isn&#8217;t that just another cop-out?</h4>

<p>Well, yes and no. Yes in the fact that I&#8217;m not giving a solid answer, no in the sense that it really does depend on your application: what frameworks you&#8217;re using, how you have its architecture setup, etc.</p>

<p>Just so you can&#8217;t say I&#8217;m not providing anything solid, here&#8217;s how I&#8217;m using it in our current app:
<ol>
<li>We&#8217;re using ASP.NET MVC &#038; StructureMap, so we&#8217;re using a <a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/08/15/setting-up-ioc-di-for-your-controllers-in-asp-net-mvc.aspx">custom controller factory</a> that creates controllers from the container. This means we can create an <code>EmployeeService</code>, extract an interface for it named <code>IEmployeeService</code>, put it as a requirement in the controller&#8217;s constructor, and it&#8217;s satisfied magically at run time. Even cooler, everything down the object graph from <code>EmployeeService</code> (say, an <code>EmployeeRepository</code>, or <code>LoggingService</code>, or <code>EmailService</code>, or anything else you need) gets their dependencies all satisfied automagically, too. We can stick a constructor argument in virtually anywhere and it&#8217;s taken care of for us, without giving it a second thought! Each web request, this all gets built out.</li>
<li>We have a basic home brewed scheduled task framework that, given the name of a task you want to run (say <code>/run Emailer</code> runs the <code>EmailerTask</code>), instantiates the requested task class, and runs it. We use the container at the point of instantiation, effectively treating each &#8220;task&#8221; class as a controller from above, filling all the dependencies it needs down the object graph.</li>
<li>We also fire off the scheduled task app by doing an <code>IoC.Resolve&lt;IApplication&gt;().Run()</code> in the console app&#8217;s <code>Main</code> method, giving the app everything it needs.</li>
</ol>
In all, <strong>we call <code>IoC.Resolve</code> only 4 times in our app</strong>, and it handles everything for us. We usually forget it&#8217;s even there, and take its services for granted when working with legacy applications that don&#8217;t have it.
</p>

<p>Now, what if you&#8217;re using WebForms? Well, you&#8217;re not *completely* out of luck. It&#8217;s a pain, to be sure, but <a href="http://www.google.com/search?q=webforms+ioc">still doable</a>.</p>

<h2>Wrapping up</h2>

<p>I hope this helped cleared up dependency injection for you a bit. Just remember to use the actual <code>Resolve</code> call of your container in as few places as possible in your application, and only on the &#8220;outside edges&#8221; of the app. Look at where you do all your main object creation (your web forms, Windows forms, controllers, WCF factories, Silverlight pages, etc). <strong>Stick the call in there, and forget about it.</strong></p>

<p>Good luck.</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2009/12/23/my-attempt-at-demystifying-dependency-injection/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2009/12/23/my-attempt-at-demystifying-dependency-injection/</feedburner:origLink></item>
		<item>
		<title>Branch-Per-Feature in Subversion</title>
		<link>http://feeds.mozingo.net/~r/TallerCode/~3/itqCEXru1AQ/</link>
		<comments>http://darrell.mozingo.net/2009/12/04/branch-per-feature-in-subversion/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 21:59:12 +0000</pubDate>
		<dc:creator>Darrell Mozingo</dc:creator>
		
		<category><![CDATA[Source Control]]></category>

		<guid isPermaLink="false">http://darrell.mozingo.net/?p=406</guid>
		<description><![CDATA[Preamble

Disclaimer: My team and I have only been doing this for a short time, so please, for the love of all that is holy, don&#8217;t take my word as gospel on any of this. We&#8217;re learning more about this each day, but we&#8217;re still far from proficient or experienced. I&#8217;m just putting what we do [...]]]></description>
			<content:encoded><![CDATA[<h3>Preamble</h3>

<p><img class="alignright" style="float: right;" title="Branch-Per-Feature" width="400" height="300" src="http://darrell.mozingo.net/wp-content/uploads/2009/11/branch-per-feature.jpg" alt="Branch-Per-Feature" /><strong>Disclaimer:</strong> My team and I have only been doing this for a short time, so please, for the love of all that is holy, don&#8217;t take my word as gospel on any of this. We&#8217;re learning more about this each day, but we&#8217;re still far from proficient or experienced. I&#8217;m just putting what we do and what we&#8217;ve learned out there to possibly help others, as I know we would have loved to hear this when we started. If you blow up your repository and lose all your code, it&#8217;s not my fault.</p>

<p>That said, lets discuss what Branch-Per-Feature is and how it <i>typically</i> works. I won&#8217;t get into much detail on why you should do it or what it is here, as <a href="http://www.lostechies.com/blogs/derickbailey">Derick Bailey</a> has an excellent, though unfortunately unfinished at the moment, <a href="http://www.lostechies.com/blogs/derickbailey/archive/2009/07/15/branch-per-feature-source-control-introduction.aspx">series of posts</a>  on the subject and they&#8217;re a <strong>must</strong> read to get a solid background on this. In a nut shell, though, Branch-Per-Feature is basically what the name implies - you create a branch in your source control system for each &#8220;feature&#8221; you work on (where the &#8220;feature&#8221; can be defined as big or as small as you want - we&#8217;ll skip doing this for typos and the like). You can then work on that feature in isolation in your own branch, committing your changes as often as you like and not screwing up your other teammates or being affected by them, until you&#8217;re all finished, at which point you merge the completed feature back into the trunk. Using this methodology, the trunk should always be kept in a &#8220;shippable&#8221; state.</p>

<p>Sounds easy enough, right? As is often the case, however, the devil is in the details. I&#8217;m sure the first thing that jumps out at you in reading about this is &#8220;how the hell does this work with multiple developers?&#8221;. Well, the theory goes that as you&#8217;re working on your feature branch, you continuously merge changes from the trunk (i.e. features other developer&#8217;s have finished) into your branch. This means instead of merging after 2 weeks of work and finding a crap load of conflicts, you have a few here and a few there. In practice it generally works out that way, too, unless you or a team mate goes on a refactoring rampage and you need to merge those changes in (I&#8217;m usually guilty of this myself - sorry guys!). Following <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">SRP</a> and the small class sizes that result from it helps in the fact that multiple people usually aren&#8217;t working in the same class, too.</p>

<h3>Simple scenario</h3>

<p>I&#8217;ll walk through the steps of a simplified &#8220;feature&#8221; branch from start to finish. Simplified here means you&#8217;re either working by yourself, or you <i>know</i> nobody else has changed the trunk while you&#8217;re working on your feature. As the title of this post alludes to, I&#8217;ll be showing this using Subversion coupled with <a href="http://tortoisesvn.net/downloads">TortoiseSVN</a>, so I&#8217;m not terribly sure how it&#8217;ll work with similar source control systems. Also, while all the tools and practices that I&#8217;m going to show here do help with merge problems and the like, they&#8217;re <strong>not a substitute for using actual communication</strong> on your team inorder to avoid problems. Sorry, but you just can&#8217;t get away from talking to people <img src='http://darrell.mozingo.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><strong>Note</strong>: all of this is shown with a version of <strong>Subversion &amp; TortoiseSVN &gt; 1.6</strong>, as many of these features have only been included since then. Make sure the server, client, <i>and</i> your repository are upgraded to at least this version (especially check the repository, which needs upgraded separate from the server, which we learned the hard way). Ask your admin if you&#8217;re unsure about any of this.</p>

<p>Now, say we have a repository named <code>BranchPerFeature</code> with the standard <code>/trunk</code>, <code>/branches</code>, and <code>/tags</code> folders. It has a simple console app that displays a welcome message. Call me weird, but I&#8217;ve never liked the SVN switch command, so I just setup a local directory structure similar to the repository&#8217;s:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-1.png" alt="Local folder structure" />

<p>We&#8217;ll start by creating a branch for our new feature. Right-click your checked out trunk folder, then select TortoiseSVN, then Branch/Tag. In the <code>To URL</code> field, we&#8217;ll enter a path in the branches folder in the repository, usually named after the feature we&#8217;re working on. In this case we&#8217;ll create a branch in <code>branches/AddUserInput</code> (we&#8217;re adding a Console.ReadLine() in this feature - exciting, huh?):</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-2.png" alt="Creating feature branch" />

<p>Now you can checkout that new branch in your local branches folder and start working on the feature, committing as often as you&#8217;d like knowing you&#8217;re not affecting anyone else. When you&#8217;re all done and everything is committed to it, we&#8217;ll merge it back into the local copy of your trunk. Go to where you have the trunk checked out, right-click and select TortoiseSVN, then Merge. Of the three options you&#8217;re presented with, select the second, <code>Reintegrate a branch</code>:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-3.png" alt="Integrating branch" />

<p>We&#8217;re merging from our branch, so enter its repository URL in the <code>From URL</code> and hit next:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-4.png" alt="Integrate branch #2" />

<p>Hit Merge on the final screen (or use the Test Merge button to do a dry-run that won&#8217;t affect anything). Remember, at this point all changes made from your branch, and applied to the trunk, are only local. Now open the updated trunk, run all your unit tests and whatnot, then commit these changes to the repository:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-5.png" alt="Commit merged branch" />

<p>Congrats! Your first feature branch is finished and merged back into the trunk, hopefully with no problems. One major caveat of this procedure is <strong>you must not use your branch again after it&#8217;s merged back into the trunk</strong>. Why? Well, Subversion keeps some properties set on all your files/folders so it knows what revisions have been merged back into them, and continuing to work on your now stale branch and trying to merge it back into the trunk again at a latter point would be bad ju-ju. To make sure nobody accidentally uses your branch, be sure to &#8220;delete&#8221; it from your repository (don&#8217;t worry, the history is still available, just not readily available). To delete the branch, right-click on your checked out trunk folder, select TortoiseSVN, then Repo-browser. Navigate to your branches folder in the left pane, then right-click and delete your old branch:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-6.png" alt="Deleting old branch" />

<h3>A bit more complicated scenario</h3>

<p>So you created your branch and you&#8217;re happily committing away when you see someone else committed to the trunk. Uh oh. Fortunately, it&#8217;s really not a big deal. We&#8217;ll just merge the new revisions from the trunk into our branch every now and then to keep it &#8220;current&#8221; with the trunk. Then when you&#8217;re done with your branch and go to merge it back into the trunk, it should <i>theoretically</i> merge without problems, as it&#8217;s the same as the trunk except for the changes you&#8217;ve made.</p>

<p>To show this, we&#8217;ll create a new branch to add a prime number calculator to our app. Same steps as above. After we&#8217;re working on the branch, though, we&#8217;ll commit a change to the trunk to simulate it being a co-worker&#8217;s change (it can be anything, even a conflicting change if you want). Now it&#8217;s time to update you branch!</p>

<p>First, check-in any changes you&#8217;ve made on the branch (this allows you to roll back the coming updates if they blow up, among other things). Then right-click on your checked out branch folder and select TortoiseSVN, then Merge. Choose the first option this time, <code>Merge a range of revisions</code>:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-9.png" alt="Updating branch" />

<p>Make sure the trunk is specified in the <code>URL to merge from</code> field on the next screen. Leave the <code>Revision range to merge</code> field blank, as Subversion tracks which revisions it already merged in automatically and won&#8217;t try to re-merge them (which is new in version 1.6 - for older versions you&#8217;ll have to track this and specify revision ranges by hand, and trust me, it gets messy real quick):</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-10.png" alt="Updating branch #2" />

<p>If you hit the Show Log button on this screen, it should show revisions that have already been merged in gray:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-13.png" alt="Showing merged revisions" />

<p>As a quick side note, when everything is done, you can do a diff on the files and see how Subversion tracks previously merged revisions on each file &#038; folder:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-12.png" alt="Diff on updated file" />

<p>After hitting next, you can hit either Merge or Test Merge (just like before), and make sure everything gets updated. Deal with any file or tree conflicts here, and run all your unit tests to make sure these new changes are still hunky-dory with your stuff. Then simply commit these new updates to your branch:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-11.png" alt="Committing updated branch" />

<p>Keep up on these trunk updates while you&#8217;re working on your feature, and when you&#8217;re done and it&#8217;s time to merge back into the trunk, you shouldn&#8217;t have any (or at least not many!) issues or conflicts with it.</p>

<h3>Monitoring the trunk</h3>

<p>How can you know when changes are made to the trunk? Well, you can have everyone yell out when they commit to the trunk if you work close enough, you can email everyone, or you can get a tool to monitor the repository for you, such as <a href="http://www.svnmonitor.com/default.shtml">SVN-Monitor</a> which is quite awesome and totally free.</p>

<p>I set this up to monitor our trunk so I know when changes are made, with both a nice pop-up balloon and an email. Here&#8217;s a view of this sample&#8217;s trunk:</p>

<img src="http://darrell.mozingo.net/wp-content/uploads/2009/12/sshot-8.png" alt="SVN-Monitor" />

<h3>Complex scenarios</h3>

<p>You&#8217;re bound to run into issues now and again with this workflow, especially on larger teams, having a more complicated branching setup, or if you&#8217;re doing a lot of file shuffling in the trunk or your branch (as its been my experience that, in general, SVN doesn&#8217;t deal well with file moves/renames beyond the simple cases). I&#8217;m also working on a 3 person team, so I can&#8217;t speak as to how well this scales up to a 15 person team size.</p>

<p>I&#8217;m not pretending to cover every scenario or issue you&#8217;ll run into in this post, either, so accept that fact that you might, and probably will, have to either Google around for an answer to some weird error and drop down to the command line to fix something. You might as well know that going in.</p>

<h3>Issues?</h3>

<p>The biggest issue I have with this workflow so far is the time taken to create a new branch, mostly comprised of the time spent checking-out and updating R#:</p>

<ul>
  <li>Check-out time: as I mentioned, I don&#8217;t like the switch command (though it would probably solve this issue). We&#8217;re work on a large, and rapidly expanding, code base with over two dozen dependencies and lots of code files. It simply takes a while to check out a new branch (ok, in reality it&#8217;s only a few minutes, but still annoying).</li>
  <li>Updating R#: I <strong>love</strong> the solution wide analysis R# offers, but it takes a while to scan everything when you open a newly created &#038; checked out branch. This is on a quad-core machine with 15k RPM drives, too.</li>
</ul>

<p>These issues, while not show stoppers by any means, are still annoying to me none-the-less, and I thought I&#8217;d mention it.</p>

<h3>Wrapping up</h3>

<p>So there you go. Common usage scenarios when using a branch-per-feature workflow with Subversion. Not too complicated. As I noted in the begining, we&#8217;ve been doing this for a short while now and it&#8217;s working quite nice, as before we&#8217;d just make one branch for each iteration, all work in that, and merge with the trunk when done. That meant if one of us was making breaking changes, we&#8217;d simply not commit until we were all done. That&#8217;s pretty risky. What happens if our machine crashes after 4 days of work, or we wanted a file back from a few days ago? Not cool.</p>

<p>It&#8217;s worth noting that, from what I understand, <a href="http://git-scm.com/">Git</a>&#8217;s workflow follows this pattern by default. I&#8217;m planning to dive into that soon.</p>

<p>If I messed anything up in here, or you know a better way of doing it, please let me know!</p>]]></content:encoded>
			<wfw:commentRss>http://darrell.mozingo.net/2009/12/04/branch-per-feature-in-subversion/feed/</wfw:commentRss>
		<feedburner:origLink>http://darrell.mozingo.net/2009/12/04/branch-per-feature-in-subversion/</feedburner:origLink></item>
	</channel>
</rss>
