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

<channel>
	<title>dev{shaped} &#187; code</title>
	<atom:link href="http://devshaped.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://devshaped.com</link>
	<description></description>
	<lastBuildDate>Mon, 13 Jul 2009 14:47:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- podcast_generator="podPress/8.8" -->
		<copyright>&#xA9; </copyright>
		<managingEditor>derek@webradius.com ()</managingEditor>
		<webMaster>derek@webradius.com()</webMaster>
		<category></category>
		<itunes:keywords></itunes:keywords>
		<itunes:subtitle></itunes:subtitle>
		<itunes:summary></itunes:summary>
		<itunes:author></itunes:author>
		<itunes:category text="Society &amp; Culture"/>
		<itunes:owner>
			<itunes:name></itunes:name>
			<itunes:email>derek@webradius.com</itunes:email>
		</itunes:owner>
		<itunes:block>No</itunes:block>
		<itunes:explicit>no</itunes:explicit>
		<itunes:image href="http://devshaped.com/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<image>
			<url>http://devshaped.com/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
			<title>dev{shaped}</title>
			<link>http://devshaped.com</link>
			<width>144</width>
			<height>144</height>
		</image>
		<item>
		<title>Getting Started with Continuous Integration</title>
		<link>http://devshaped.com/2009/04/getting-started-with-continuous-integration/</link>
		<comments>http://devshaped.com/2009/04/getting-started-with-continuous-integration/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 09:00:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[MAXoutput]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[continuous integration]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/04/getting-started-with-continuous-integration/</guid>
		<description><![CDATA[by Sondre Bjellås
 Continuous Integration is a development practice that can help improve your company&#8217;s ability to deliver quality software. It can reduce the time to market and the deployment cycle from functional complete products to having the solutions deployed on desktop or servers.
Continuous Integration, or CI, can start with a lone developer desktop and [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://sondreb.com/blog/" target="_blank">Sondre Bjellås</a></p>
<p><img style="border-right-width: 0px; margin: 0px 0px 22px 22px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sondre-bjellas" border="0" alt="sondre-bjellas" align="right" src="http://devshaped.com/wp-content/uploads/2009/03/sondrebjellas.jpg" width="85" height="57" /> Continuous Integration is a development practice that can help improve your company&#8217;s ability to deliver quality software. It can reduce the time to market and the deployment cycle from functional complete products to having the solutions deployed on desktop or servers.</p>
<p>Continuous Integration, or CI, can start with a lone developer desktop and mature closer towards production. The ultimate goal is to have a CI environment that can take you from checked-in code to production ready installation packages.</p>
<p><strong>The instructions in this article will help you get started with CI even if you&#8217;re a single developer or a small team.</strong></p>
<p>This is a process that takes time, but don&#8217;t be afraid of the road ahead, I will walk you through the first baby-steps towards a well-tuned machine. CI-practice is one of many important elements for the software development industry to mature to levels of other types of manufacturing.</p>
<p><span id="more-159"></span></p>
<h2><b>Introduction to Continuous Integration</b></h2>
<p>Martin Fowler has written a very good introduction to CI which you can read this on his website: <a href="http://martinfowler.com/articles/continuousIntegration.html">http://martinfowler.com/articles/continuousIntegration.html</a> [1]</p>
<p><i>Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily &#8211; leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly. This article is a quick overview of Continuous Integration summarizing the technique and its current usage.</i></p>
<p>The clean and simple essence of CI is to avoid the situation <i>“It works on my machine”</i>. This has caused a lot of pain between developer and users, but also between developers. Most of us have experienced that our projects stops compiling properly after a <i>Get Latest</i> operation on the source control system. Another important aspect of CI is to have a cycles of automatic compilation and testing of your projects. We will get deeper into automatic testing later in this article.</p>
<h2><b>Source Control Systems</b></h2>
<p>If you are not familiar with source control systems (SCM), I suggest reading a good introduction by Eric Sink. While the first CI example I will give in this article doesn’t use SCM, it is an essential piece of the puzzle if you&#8217;re doing computer software development. Even as a single developer, it&#8217;s advisable to use a SCM. SCM will give you better control and management of your projects, with history and the ability to restore deleted code.</p>
<p>Introduction to SCM: <a href="http://www.ericsink.com/scm/scm_intro.html">http://www.ericsink.com/scm/scm_intro.html</a> [2]</p>
<p>There are many providers of SCM software; for the purpose of this article we will use Microsoft Visual SourceSafe. When your team grows and you have the finances to purchase licenses, I suggest upgrading to Microsoft Visual Studio Team System, which is a complete Application Life-Cycle Management (ALM) suite.</p>
<h2><b>Setting up your environment</b></h2>
<p>There are various CI solutions available for .NET developers, one of the most popular is Cruise Control .NET (<a href="http://ccnet.thoughtworks.com/">http://ccnet.thoughtworks.com/</a> [3]).</p>
<p>[Please note that all examples are from a 64-bit system where the Program Files paths includes <i>(x86)</i> for 32-bit applications. If you have a 32-bit environment, please make sure you remove any reference to <i>(x86)</i>.]</p>
<p>Cruice Control .NET (CC.NET) comes in various forms, for beginners the setup alternative is the simplest to get you started (CruiseControl.NET-1.4.2-Setup.exe).</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0021.jpg" width="300" height="232" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0041.jpg" width="300" height="232" /></p>
<p>After the installation has completed, a new folder will appear in the Windows start menu. From here we can access the main configuration file and launch the console application.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0061.jpg" width="270" height="119" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0081.jpg" width="500" height="206" /></p>
<p>Before we continue with updates to our configuration file, we need to learn a little bit about build tools…</p>
<h2><b>Build Tools</b></h2>
<p>To compile the source code, you will need a tool that does the build for you. One alternative is NAnt, which is a free tool (<a href="http://nant.sourceforge.net/">http://nant.sourceforge.net/</a> [4]).</p>
<p>With Visual Studio 2005 Microsoft introduced MSBuild, which is a standalone build engine for .NET. This has made it more accessible for third parties and made it simpler to automate and script.</p>
<p>The simplest way to get started with a build process is to utilize MSBuild, which is already installed if you have .NET Framework 2.0. We will configure CC.NET to start the MSBuild process which takes care of the compilation of our Visual Studio Solution.</p>
<h2><b>New Project or Existing Solutions</b></h2>
<p>It is much less effort to include guidance, frameworks, and tools in the beginning of a new project. This applies to CI as well. The further you get in the development process, the harder it gets to make everything work properly in a CI-environment.</p>
<h2><b>Sample Projects</b></h2>
<p>For the purpose of this article, we’ll create a new Visual Studio solution called <i>IntegrationSample</i>. Within this solution we will create 3 projects: <i>IntegrationSample</i> (Windows Forms), <i>Integration.Common</i> (Class Library) and <i>Integration.Test</i> (Class library).</p>
<p>Add a <i>Person.cs</i> class to the Common project with the following code:</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">namespace</span> Integration.Common     <br />{     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">class</span> Person     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> Firstname { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">string</span> Lastname { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     <br />&#160;&#160;&#160; <span style="color: blue">public</span> DateTime Birthdate { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }     </p>
<p>&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">bool</span> IsOlderThan60()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> (Birthdate &lt; DateTime.Now.AddYears(-<span style="color: maroon">60</span>));     <br />&#160;&#160;&#160; }     </p>
<p>&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">override</span>&#160;<span style="color: blue">string</span> ToString()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>&#160;<span style="color: blue">string</span>.Format(<span style="color: maroon">&quot;{0} {1}&quot;</span>, Firstname, Lastname);     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </div>
<p>This class represents part of our business domain model and has certain business logic applied to it that is important to our business, namely the method that tells us if a person is more than 60 years old. The next step is to apply a test for our domain object.</p>
<h2><b>Automated Testing with Unit Tests</b></h2>
<p>Being able to automate the testing process of your projects is an important feature of a CI-environment. Having source code that works as tests for your application logic will help you when you’re later going back to modify, extend and fix existing code. With good tests you can ensure that your code changes won&#8217;t break the whole application. There are different frameworks available for unit testing, the most widely used with .NET is NUnit (<a href="http://www.nunit.org">www.nunit.org</a> [5]). To learn more about test-driven development, visit Wikipedia: <a href="http://en.wikipedia.org/wiki/Test-driven_development">http://en.wikipedia.org/wiki/Test-driven_development</a> [6]</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0101.jpg" width="300" height="233" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image012" border="0" alt="clip_image012" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0121.jpg" width="300" height="234" /></p>
<p>When the installation of NUnit is complete, you can explore the included samples to learn more about writing unit tests. You can use the NUnit GUI to run your unit tests outside of the CI-environment.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image014" border="0" alt="clip_image014" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image0141.jpg" width="244" height="176" /></p>
<p>To start writing unit tests, you need to add a reference to the following assembly in your <i>Integration.Test</i> project: &quot;C:\Program Files (x86)\NUnit 2.4.8\bin\nunit.framework.dll&quot;. Additionally you should create a reference between the Test and the Common project, so we can access our business domain model in the tests project.</p>
<p>When you’re done adding the reference, let’s create a class named <i>PersonTest.cs</i> which will contain all the unit tests for our specific domain object.</p>
<div style="font-family: courier new; font-size: 9pt"><span style="color: blue">using</span> System;     <br /><span style="color: blue">using</span> NUnit.Framework;     <br /><span style="color: blue">using</span> Integration.Common;     </p>
<p><span style="color: blue">namespace</span> Integration.Test     <br />{     <br />&#160; [TestFixture]     <br />&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">class</span> PersonTest     <br />&#160; {     <br />&#160;&#160;&#160; [Test]     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">void</span> IsOlderThan60Test()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; Person person = <span style="color: blue">new</span> Person();     <br />&#160;&#160;&#160;&#160;&#160; person.Firstname = <span style="color: maroon">&quot;Sondre&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; person.Lastname = <span style="color: maroon">&quot;Bjellås&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; person.Birthdate = <span style="color: blue">new</span> DateTime(<span style="color: maroon">1905</span>, <span style="color: maroon">4</span>, <span style="color: maroon">4</span>);     <br />&#160;&#160;&#160;&#160;&#160; Assert.IsTrue(person.IsOlderThan60());     </p>
<p>&#160;&#160;&#160;&#160;&#160; person.Birthdate = DateTime.Now;     <br />&#160;&#160;&#160;&#160;&#160; Assert.IsFalse(person.IsOlderThan60());     </p>
<p>&#160;&#160;&#160;&#160;&#160; person.Birthdate = DateTime.MaxValue;     <br />&#160;&#160;&#160;&#160;&#160; Assert.IsFalse(person.IsOlderThan60());     <br />&#160;&#160;&#160; }     </p>
<p>&#160;&#160;&#160; [Test]     <br />&#160;&#160;&#160; <span style="color: blue">public</span>&#160;<span style="color: blue">void</span> ToStringTest()     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160; Person person = <span style="color: blue">new</span> Person();     <br />&#160;&#160;&#160;&#160;&#160; person.Firstname = <span style="color: maroon">&quot;Sondre&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; person.Lastname = <span style="color: maroon">&quot;Bjellås&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160; Assert.AreEqual(person.ToString(), <span style="color: maroon">&quot;Sondre Bjellås&quot;</span>);     <br />&#160;&#160;&#160; }     <br />&#160; }     <br />}     </div>
<p>Tests are done through assertions. There are many types of assert methods included in the NUnit framework and you should use them to validate your business logic. Writing good unit tests is hard and it&#8217;s a practice in which you will continually improve as you get more experienced with it.</p>
<p>You can now begin to run and validate the unit tests on your local machine, open the NUnit GUI application and open the Integration.Test.dll file.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image0161.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image016" border="0" alt="clip_image016" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image016-thumb1.jpg" width="500" height="290" /></a></p>
<p>As you can see in my screenshot, both of the tests for the Person object validates. When we later go back to change the business logic, we can use the unit tests as a means to validate that our changes doesn’t break anything.</p>
<h2><b>Bringing it all together</b></h2>
<p>Let’s go back to the CC.NET configuration file and start modifying it so we get our CI-environment up and running. It&#8217;s advisable to have a dedicate computer that act as a build-computer, but it&#8217;s not a requirement.</p>
<p>The first thing we’re going to do is modify the default configuration file to run NUnit and our tests, here is the script so far:</p>
<pre style="font-size: 9pt"><span style="color: blue">&lt;</span><span style="color: maroon">cruisecontrol</span> <span style="color: red">xmlns:cb</span>=&quot;<span style="color: blue">urn:ccnet.config.builder</span>&quot;<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">project</span> <span style="color: red">name</span>=&quot;<span style="color: blue">IntegrationSample</span>&quot;<span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span><span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">path</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\NUnit 2.4.8\bin\nunit-console.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">path</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
     <span style="color: blue">&lt;</span><span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Integration.Test\bin\Debug\Integration.Test.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span>/<span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span>/<span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span>/<span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">cruisecontrol</span><span style="color: blue">&gt;</span></pre>
<p>Save the configuration and start the CruiceControl.NET application in the start menu. This will open a console application that will run your configuration. This is useful for beginners and when you’re setting up a new environment. When the configuration is complete, you should use the Windows Service for a more stable runtime.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image0181.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image018" border="0" alt="clip_image018" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image018-thumb1.jpg" width="500" height="252" /></a></p>
<p>When you ran the CC.NET installation, it should have created a virtual directory for you on the local Internet Information Services. If you happen to have any problems with this, make sure you create a virtual directory called webdashboard and map it to <i>C:\Program Files (x86)\CruiseControl.NET\webdashboard</i>.</p>
<p>Opening up the web dashboard should show us our IntegrationSample project and hopefully a green status, indicating that our unit tests has completed successfully. With the current configuration, we need to click the <i>Force</i> button to run tests.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image020.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image020" border="0" alt="clip_image020" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image020-thumb.jpg" width="500" height="246" /></a></p>
<p>This dashboard will be your viewport into the status and history of integration builds. You can drill down into the details of a build and you can have multiple CC.NET projects specified in the configuration file.</p>
<p>Our next step is to make CC.NET compile our source code using MSBuild.exe. </p>
<p>Within the &lt;tasks&gt; element in the configuration, add the following script above the NAnt task, which will tell CC.NET to execute MSBuild.exe with our Visual Studio solution file.</p>
<pre><span style="color: blue">&lt;</span><span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>IntegrationSample.sln<span style="color: blue">&lt;</span>/<span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>/noconsolelogger /p:Configuration=Debug /v:m<span style="color: blue">&lt;</span>/<span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">targets</span><span style="color: blue">&gt;</span>Build<span style="color: blue">&lt;</span>/<span style="color: maroon">targets</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>720<span style="color: blue">&lt;</span>/<span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">logger</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">logger</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span></pre>
<p>To validate that CC.NET is working correctly, we can go ahead and make a quick modification to the source code. If you reverse the operator for the age method, we should see the IntegrationSample fail on the dashboard. Drilling down into the details, we should see that our unit test fails.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image022.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image022" border="0" alt="clip_image022" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image022-thumb.jpg" width="504" height="244" /></a></p>
<p>The last and final step is to add support for a source control system. As you have now seen, you can setup an CI-environment without source control, but it makes sense to use source control. A source control system adds history and backup support and the ability to make heavy and local changes and not disrupt the active codebase which is inside the source control database.</p>
<h2><b>Visual SourceSafe 2005</b></h2>
<p>If you don&#8217;t have a source control system yet, an easy way to get started is with Microsoft Visual SourceSafe 2005. With an MSDN Subscription, you can download it from the downloads page.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image024" border="0" alt="clip_image024" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image024.jpg" width="300" height="230" /></p>
<p>When you go beyond a small team of programmers I suggest upgrading the source control with a more modern provider, like Visual Studio Team System or SourceGear [7].</p>
<p>Going back to Visual Studio and our <i>IntegrationSample</i> project, we will go ahead and add it to our source control provider of choice.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image026" border="0" alt="clip_image026" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image026.jpg" width="304" height="98" /></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image028" border="0" alt="clip_image028" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image028.jpg" width="300" height="238" /></p>
<p>When the solution and all the projects is added to the source control, we can open the Visual SourceSafe Explorer and see how our newly added project is added and we can begin to check-out and check-in code changes.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image030" border="0" alt="clip_image030" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image030.jpg" width="500" height="248" /></p>
<p>We’re now ready for the final changes to our ccnet.config file. Open up the configuration file from the start menu and add the following new elements to the top beneath the &lt;project&gt; element.</p>
<pre><span style="color: blue">&lt;</span><span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">intervalTrigger</span> <span style="color: red">seconds</span>=&quot;<span style="color: blue">60</span>&quot; /<span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>

<span style="color: blue"> &lt;</span><span style="color: maroon">sourcecontrol</span> <span style="color: red">type</span>=&quot;<span style="color: blue">vss</span>&quot; <span style="color: red">autoGetSource</span>=&quot;<span style="color: blue">true</span>&quot; <span style="color: red">applyLabel</span>=&quot;<span style="color: blue">true</span>&quot;<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\Microsoft Visual SourceSafe\ss.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">project</span><span style="color: blue">&gt;</span>$/IntegrationSample<span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">username</span><span style="color: blue">&gt;</span>build<span style="color: blue">&lt;</span>/<span style="color: maroon">username</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">password</span><span style="color: blue">&gt;</span>Password123<span style="color: blue">&lt;</span>/<span style="color: maroon">password</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span>C:\Source\VSS<span style="color: blue">&lt;</span>/<span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span> <span style="color: blue">&lt;</span>!-- Path to your SourceSafe database --<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>false<span style="color: blue">&lt;</span>/<span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">sourcecontrol</span><span style="color: blue">&gt;</span></pre>
<p>Make sure you use a different working directory than your normal project folder, if you are running CC.NET on the same machine that you&#8217;re developing on. You also need to modify the &lt;workingDirectory&gt; and &lt;assembly&gt; elements we created earlier. See the bottom of the article for a full version of our build script.</p>
<p>When you start CruiceControl.NET again, you should see something similar to this output. Please beware that it can take a short while for the process to start.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image032.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image032" border="0" alt="clip_image032" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image032-thumb.jpg" width="500" height="238" /></a></p>
<p>As you can see, the script finds that there has been modification in the project inside our source control, which triggers a get process of the source code and later compilation and test-run.</p>
<p>You can specify your own trigger interval in the configuration file, depending on the size of your development team and your software, you can tune it to make sure you build often, but not so much that your build server starts having trouble.</p>
<p>You should create a special user account that your build process can run as and access the source control, as you can see in the example above I created a user called <i>build</i>.</p>
<p>Now it&#8217;s time to test the whole cycle of editing source code, check-in, watch the build server figure out there has been a modification and trigger a compilation and test-run. Let&#8217;s check out the Person.cs file and modify the age check to make our unit tests fail.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image034.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image034" border="0" alt="clip_image034" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image034-thumb.jpg" width="244" height="176" /></a></p>
<div style="font-family: courier new"><span style="color: blue">public</span>&#160;<span style="color: blue">bool</span> IsOlderThan60() </p>
<p>{ </p>
<p>&#160; <span style="color: blue">return</span> (Birthdate &gt; DateTime.Now.AddYears(-<span style="color: maroon">60</span>)); </p>
<p>} </p>
<p></div>
<p>When you’re checking code back into the source control system, it’s good practice to add an informative comment. This is useful for historical purposes and for other team members to get an overview of changes in the history view.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image036" border="0" alt="clip_image036" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image036.jpg" width="500" height="269" /></p>
<p>After checking in our changes, we only need to wait a few seconds to see that CC.NET discovers our change and runs our script.</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/clip-image038.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image038" border="0" alt="clip_image038" src="http://devshaped.com/wp-content/uploads/2009/03/clip-image038-thumb.jpg" width="500" height="123" /></a></p>
<p>Congratulations, you now have a CI-environment up and running!</p>
<h2><b>Going Forward</b></h2>
<p>We have looked at the base tools in a CI-environment, going forward I suggest taking a look at frameworks and tools that works good together with CC.NET. FxCop [8] is a tool from Microsoft that analyzes your assemblies and reports anything that is not according to the industry standards for naming conventions, etc. NCover [9] is a tool which will generate code coverage reports (that means it will highlight which application logic in your application is being covered by unit tests). If you’re a component shop, API documentation is important. Using tools like SandCastle [10] you can automatically generate help files from your XML comments.</p>
<p>There is a whole world to explore and the extension ability to your CI-environment is endless and everything is there to help you improve the process of building good software with high quality.</p>
<p>You should now understand how to setup your own CI-environment using free tools. Best of luck in setting up your own environment! </p>
<h2><b>References:</b></h2>
<p>[1] <a href="http://martinfowler.com/articles/continuousIntegration.html">http://martinfowler.com/articles/continuousIntegration.html</a></p>
<p>[2]<a href="http://www.ericsink.com/scm/scm_intro.html">http://www.ericsink.com/scm/scm_intro.html</a></p>
<p>[3] <a href="http://ccnet.thoughtworks.com/">http://ccnet.thoughtworks.com/</a></p>
<p>[4] <a href="http://nant.sourceforge.net/">http://nant.sourceforge.net/</a></p>
<p>[5] <a href="http://www.nunit.org/">http://www.nunit.org/</a></p>
<p>[6] <a href="http://en.wikipedia.org/wiki/Test-driven_development">http://en.wikipedia.org/wiki/Test-driven_development</a></p>
<p>[7] <a href="http://www.sourcegear.com/">http://www.sourcegear.com/</a></p>
<p>[8] <a href="http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx">http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx</a></p>
<p>[9] <a href="http://www.ncover.com/">http://www.ncover.com/</a></p>
<p>[10] <a href="http://www.codeplex.com/Sandcastle">http://www.codeplex.com/Sandcastle</a></p>
<h2><b>Sample Soure Code</b> (includes the ccnet.config build script):</h2>
<p><a href="http://devshaped.com/wp-content/uploads/2009/03/IntegrationSample.zip" target="_blank">IntegrationSample.zip</a></p>
<h2><b>Build Script</b></h2>
<pre><span style="color: blue">&lt;</span><span style="color: maroon">cruisecontrol</span> <span style="color: red">xmlns:cb</span>=&quot;<span style="color: blue">urn:ccnet.config.builder</span>&quot;<span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span><span style="color: maroon">project</span> <span style="color: red">name</span>=&quot;<span style="color: blue">IntegrationSample</span>&quot;<span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span><span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">intervalTrigger</span> <span style="color: red">seconds</span>=&quot;<span style="color: blue">60</span>&quot; /<span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span>/<span style="color: maroon">triggers</span><span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span><span style="color: maroon">sourcecontrol</span> <span style="color: red">type</span>=&quot;<span style="color: blue">vss</span>&quot; <span style="color: red">autoGetSource</span>=&quot;<span style="color: blue">true</span>&quot; <span style="color: red">applyLabel</span>=&quot;<span style="color: blue">true</span>&quot;<span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\Microsoft Visual SourceSafe\ss.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">project</span><span style="color: blue">&gt;</span>$/IntegrationSample<span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">username</span><span style="color: blue">&gt;</span>build<span style="color: blue">&lt;</span>/<span style="color: maroon">username</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">password</span><span style="color: blue">&gt;</span>Password123<span style="color: blue">&lt;</span>/<span style="color: maroon">password</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span>C:\Source\VSS<span style="color: blue">&lt;</span>/<span style="color: maroon">ssdir</span><span style="color: blue">&gt;</span> <span style="color: blue">&lt;</span>!-- Path to your SourceSafe database --<span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span><span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>false<span style="color: blue">&lt;</span>/<span style="color: maroon">cleanCopy</span><span style="color: blue">&gt;</span>
  <span style="color: blue">&lt;</span>/<span style="color: maroon">sourcecontrol</span><span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span><span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>

   <span style="color: blue">&lt;</span><span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">executable</span><span style="color: blue">&gt;</span>C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">executable</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\IntegrationSample\<span style="color: blue">&lt;</span>/<span style="color: maroon">workingDirectory</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>IntegrationSample.sln<span style="color: blue">&lt;</span>/<span style="color: maroon">projectFile</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>/noconsolelogger /p:Configuration=Debug /v:m<span style="color: blue">&lt;</span>/<span style="color: maroon">buildArgs</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">targets</span><span style="color: blue">&gt;</span>Build<span style="color: blue">&lt;</span>/<span style="color: maroon">targets</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>720<span style="color: blue">&lt;</span>/<span style="color: maroon">timeout</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">logger</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">logger</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span>/<span style="color: maroon">msbuild</span><span style="color: blue">&gt;</span>

   <span style="color: blue">&lt;</span><span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">path</span><span style="color: blue">&gt;</span>C:\Program Files (x86)\NUnit 2.4.8\bin\nunit-console.exe<span style="color: blue">&lt;</span>/<span style="color: maroon">path</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
     <span style="color: blue">&lt;</span><span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>C:\Source\Samples\IntegrationSample\Build\IntegrationSample\Integration.Test\bin\Debug\Integration.Test.dll<span style="color: blue">&lt;</span>/<span style="color: maroon">assembly</span><span style="color: blue">&gt;</span>
    <span style="color: blue">&lt;</span>/<span style="color: maroon">assemblies</span><span style="color: blue">&gt;</span>
   <span style="color: blue">&lt;</span>/<span style="color: maroon">nunit</span><span style="color: blue">&gt;</span>

  <span style="color: blue">&lt;</span>/<span style="color: maroon">tasks</span><span style="color: blue">&gt;</span>
 <span style="color: blue">&lt;</span>/<span style="color: maroon">project</span><span style="color: blue">&gt;</span>
<span style="color: blue">&lt;</span>/<span style="color: maroon">cruisecontrol</span><span style="color: blue">&gt;</span></pre>
<p>&#8212;&#8211;</p>
<p>Sondre is a <a href="http://www.microsoftregionaldirectors.com/">Microsoft Regional Director</a> with 11 years of experience in software and web development. He currently works as Technology Leader for the Microsoft department at <a href="http://www.capgemini.no/">Capgemini</a> in Oslo, Norway. Sondre is additionally the leader of the Norwegian .NET User Group in Oslo and a speaker at various technology and software development events. Active blogger, writer and technology enthusiast with a special passion for robotics, intelligent systems and open source.</p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/04/getting-started-with-continuous-integration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tamir Khason on Vista Battery Saver</title>
		<link>http://devshaped.com/2009/01/tamir-khason-on-vista-battery-saver/</link>
		<comments>http://devshaped.com/2009/01/tamir-khason-on-vista-battery-saver/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 05:10:38 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[codeplex]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[vista]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/01/tamir-khason-on-vista-battery-saver/</guid>
		<description><![CDATA[ Our CodePlex Project of the Month for January 2009 is Vista Battery Saver.
&#34;This tiny program will save up to 70% of your battery by disabling those nice, but greedy Vista features.&#34;
When you switch from running on AC power to running on battery, Vista Battery Saver will disable the Windows Sidebar and Aero features and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://devshaped.com/wp-content/uploads/2009/01/vistabatterysaver.jpg"><img title="vistabatterysaver" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 22px 22px; border-left: 0px; border-bottom: 0px" height="57" alt="vistabatterysaver" src="http://devshaped.com/wp-content/uploads/2009/01/vistabatterysaver-thumb.jpg" width="85" align="right" border="0" /></a> Our CodePlex Project of the Month for January 2009 is <a href="http://www.codeplex.com/vistabattery">Vista Battery Saver</a>.</p>
<p>&quot;This tiny program will save up to 70% of your battery by disabling those nice, but greedy Vista features.&quot;</p>
<p>When you switch from running on AC power to running on battery, Vista Battery Saver will disable the Windows Sidebar and Aero features and switch to a more conservative power plan (e.g., from High Performance to Power Saver). When you reconnect to AC power, Vista Battery Saver switches everything back to the full Vista experience.</p>
<p>The Vista Battery Saver project was started by <a href="http://khason.net/blog/">Tamir Khason</a>. Listen to our interview with Tamir using the embedded audio player or download the MP3.</p>
</p>
<p>&#8212;&#8211;</p>
<p><em>CodePlex is Microsoft&#8217;s open source project hosting web site. Start a new project, join an existing one, or download software created by the community. </em><a href="http://www.codeplex.com/CodePlex"><em>Learn More</em></a></p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/tamir-khason-on-vista-battery-saver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
			<enclosure url="http://devshaped.com/audio/vistabattery.mp3" length="4017109" type="audio/mpeg"/>
<itunes:duration>8:22</itunes:duration>
		<itunes:subtitle>Our CodePlex Project of the Month for January 2009 is Vista Battery Saver.  #34;This tiny program will save up to 70% of your ...</itunes:subtitle>
		<itunes:summary>Our CodePlex Project of the Month for January 2009 is Vista Battery Saver.  #34;This tiny program will save up to 70% of your battery by disabling those nice, but greedy Vista features.#34;  When you switch from running on AC power to running on battery, Vista Battery Saver will disable the Windows Sidebar and Aero features and switch to a more conservative power plan (e.g., from High Performance to Power Saver). When you reconnect to AC power, Vista Battery Saver switches everything back to the full Vista experience.  The Vista Battery Saver project was started by Tamir Khason. Listen to our interview with Tamir using the embedded audio player or download the MP3.    -----  CodePlex is Microsoft's open source project hosting web site. Start a new project, join an existing one, or download software created by the community. Learn More</itunes:summary>
		<itunes:keywords>Audio,,codeplex</itunes:keywords>
		<itunes:author>derek@webradius.com</itunes:author>
		<itunes:explicit>no</itunes:explicit>
		<itunes:block>No</itunes:block>
	</item>
		<item>
		<title>Donald Belcham on Brownfield Development</title>
		<link>http://devshaped.com/2009/01/donald-belcham-on-brownfield-development/</link>
		<comments>http://devshaped.com/2009/01/donald-belcham-on-brownfield-development/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 04:18:05 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[belcham]]></category>
		<category><![CDATA[brownfield]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://devshaped.com/?p=22</guid>
		<description><![CDATA[In this audio interview, Donald Belcham discusses brownfield development, technical debt, and some useful tools for developers.

Be sure to read Donald&#8217;s article &#8220;Working with Brownfield Code&#8220;.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://devshaped.com/wp-content/uploads/2009/01/donald-belcham.jpg"><img title="Donald_Belcham" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 22px 22px; border-left: 0px; border-bottom: 0px" height="57" alt="Donald_Belcham" src="http://devshaped.com/wp-content/uploads/2009/01/donald-belcham-thumb.jpg" width="85" align="right" border="0" /></a>In this audio interview, Donald Belcham discusses brownfield development, technical debt, and some useful tools for developers.
</p>
<p>Be sure to read Donald&#8217;s article &#8220;<a href="http://devshaped.com/2009/01/working-with-brownfield-code/">Working with Brownfield Code</a>&#8220;.</p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/donald-belcham-on-brownfield-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
			<enclosure url="http://devshaped.com/audio/dbelcham-brownfield.mp3" length="6199939" type="audio/mpeg"/>
<itunes:duration>12:54</itunes:duration>
		<itunes:subtitle>In this audio interview, Donald Belcham discusses brownfield development, technical debt, and some useful tools for developers.


Be sure to read Donald's article "Working with Brownfield ...</itunes:subtitle>
		<itunes:summary>In this audio interview, Donald Belcham discusses brownfield development, technical debt, and some useful tools for developers.


Be sure to read Donald's article "Working with Brownfield Code".</itunes:summary>
		<itunes:keywords>Audio</itunes:keywords>
		<itunes:author>derek@webradius.com</itunes:author>
		<itunes:explicit>no</itunes:explicit>
		<itunes:block>No</itunes:block>
	</item>
		<item>
		<title>Accelerate Your Coding with Code Snippets</title>
		<link>http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/</link>
		<comments>http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 03:44:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[MAXoutput]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[noyes]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/</guid>
		<description><![CDATA[by Brian Noyes
 One of the most underutilized productivity features in Visual Studio 2005 and Visual Studio 2008 is Code Snippets. Even though most developers have heard about them, I find when working with consulting customers that there are few developers who use them on a regular basis. Even if they do use them, they [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://briannoyes.net" target="_blank">Brian Noyes</a></p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/brian-noyes.jpg"><img title="Brian_Noyes" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 22px 22px; border-right-width: 0px" height="57" alt="Brian_Noyes" src="http://devshaped.com/wp-content/uploads/2009/01/brian-noyes-thumb.jpg" width="85" align="right" border="0" /></a> One of the most underutilized productivity features in Visual Studio 2005 and Visual Studio 2008 is Code Snippets. Even though most developers have heard about them, I find when working with consulting customers that there are few developers who use them on a regular basis. Even if they do use them, they vastly underutilize them by only using a couple of them and never create their own. In this article, I give you a quick intro into what code snippets are, how they work, what is available out of the box, and how to go beyond that by quickly creating your own code snippets.</p>
<h2>Code Snippets Overview</h2>
<p>Code snippets were introduced in Visual Studio 2005 and allow you to emit a chunk of code into your editor by typing a few keystrokes (a shortcut mnemonic). I&#8217;m going to use C# for the examples in this article, but just realize that code snippets work in Visual Basic and other .NET languages as well. That chunk of code can just be a static chunk of code, but what really makes code snippets powerful is that they can have placeholders that you can quickly overtype when you invoke the code snippet.</p>
<p> <span id="more-21"></span>
<p>To try them out, the best one to start with is one you can probably use the most often: the <b><i>prop</i></b> snippet. Code snippets have both a title and a shortcut, although these will often both just be set to the shortcut value. The <b><i>prop</i></b> snippet emits a property with a backing member variable in Visual Studio 2005, or an auto-implemented property in Visual Studio 2008. Code snippets show up in the IntelliSense list as well, so if a snippet has a longer shortcut than a few letters, often you can just invoke it by typing a few of the letters and select it out of IntelliSense.</p>
<p>For example, if I go inside a class declaration and type <b><i>prop</i></b> and hit <b><i>Tab</i></b> twice (once to dismiss the IntelliSense list, the second time to invoke the code snippet), I get the following in the editor:</p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="background: lime; color: blue">int</span> <span style="background: lime">MyProperty</span> { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p> 
<p>The type and property name will be highlighted in green, indicating that they are the placeholders defined in the snippet. For this snippet, the type is the first placeholder and will be selected so that if you start typing, you will overtype what is there. So for example, say I want a string property. I type <b><i>string</i></b> and hit <b><i>Tab</i></b>. Tab moves between the placeholders, and when the focus is placed on the next placeholder, it selects it as well so you can overtype with the value you want. So if I type <b><i>Name</i></b> and hit <b><i>Enter</i></b>, I leave the placeholder mode and my changes are accepted. If I keep hitting Tab, it revisits the placeholders in order in case you accidently tabbed past one or changed your mind as you were filling things in.</p>
<p>This one may not be stunning in its time savings, but when you add up the keystrokes (4 + 1 + 6 + 1 + 4 + 1) you have 17 versus the 30 for typing out the whole property yourself. Wham! You just became almost twice as fast at writing code! OK, don&#8217;t tell your manager just yet, you need to actually get used to doing this all the time first… </p>
<p>Now consider setting a switch case statement for an enumeration with a number of values. For example, say you needed to set up a switch/case on the states for System.Windows.Input.MouseAction. If you had a local variable of that type with something like the following method:</p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="color: blue">void</span> HandleMouseInput(<span style="color: #2b91af">MouseAction</span> action)     <br />{     </p>
<p>} </p>
<p> 
<p>If I go inside that method and type <b><i>switch Tab Tab</i></b>, I get the following snippet with a placeholder on the variable <b><i>switch_on</i></b>. </p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="color: blue">void</span> HandleMouseInput(<span style="color: #2b91af">MouseAction</span> action)     <br />{     <br />&#160; switch (<span style="background: lime">switch_on</span>)     <br />&#160; {     <br />&#160;&#160;&#160; <span style="color: blue">default</span>:     <br />&#160; }     <br />}     </p>
<p> 
<p>If I type in <b><i>action</i></b> for the placeholder (the local variable of enum type MouseAction) and hit <b><i>Enter</i></b>, the snippet fills in all the enum values as case statements:</p>
<p style="font-family: courier new, courier"><span style="color: blue">public</span> <span style="color: blue">void</span> HandleMouseInput(<span style="color: #2b91af">MouseAction</span> action)     <br />{     <br />&#160;&#160;&#160; <span style="color: blue">switch</span> (action)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.LeftClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.LeftDoubleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.MiddleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.MiddleDoubleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.None:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.RightClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.RightDoubleClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">case</span> <span style="color: #2b91af">MouseAction</span>.WheelClick:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">default</span>:     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">break</span>;     <br />&#160;&#160;&#160; }     <br />}     </p>
<p> 
<p>That is a huge keystroke savings filling all the tedious structure code! You just need to fill in what to do for each case.</p>
<h2>Built-in Code Snippets</h2>
<p>The list of built-in code snippets for C# is actually fairly short. There is really only a small number included out of the box. You can find the full list here with a short description of each for the core C# language snippets: (<a href="http://msdn.microsoft.com/en-us/library/z41h7fat(VS.80).aspx)">http://msdn.microsoft.com/en-us/library/z41h7fat(VS.80).aspx)</a>. There are some additional ones in other categories that ship with Visual Studio 2008, but the list is still fairly short for C#.&#160; Visual Basic actually ships with hundreds. But this shouldn&#8217;t make you feel shorted as a C# developer. You can actually find a greatly expanded library of C# code snippets that shipped after VS 2005 here: <a href="http://msdn.microsoft.com/en-us/vs2005/aa718338.aspx">http://msdn.microsoft.com/en-us/vs2005/aa718338.aspx</a>. You can also find libraries of other code snippets out on CodePlex and other places on the net.&#160; <b><i>do</i></b>, <b><i>while</i></b>, <b><i>for</i></b>, <b><i>foreach</i></b>, and a number of other common control structures are all in the basic set, along with everything from data access patterns to file IO patterns in the expanded sets. <b><i>ctor</i></b> is another one that is handy for declaring a default constructor for a class.</p>
<p>There is a Code Snippet Manager in the Tools menu of Visual Studio that will let you browse all the built in snippets, import new ones, and search online. However, if you understand what is going on under the covers, it is actually easier to just manipulate them as files.</p>
<h2>Inside Code Snippets</h2>
<p>Code snippets themselves are nothing more than XML files with a particular schema that reside in some directories that Visual Studio is aware of. Specifically, snippet files end with a (not surprising) extension of .snippet. The built-in code snippets that ship with Visual Studio reside under the folder C:\Program Files\Microsoft Visual Studio 9.0 under VC# or VB respectively. Visual Studio is the default editor for that file type, so you can just browse to those folders and double click on the .snippet files and edit the XML in Visual Studio. If you want to create your own snippets, put them in your Documents folder under Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets (or similar path for VB or 2005). Visual Studio watches this file all the time, so changes or additions to snippets take effect immediately without needing to restart Visual Studio.</p>
<p>There are a lot of elements supported in the schema but the key ones to focus on are Shortcut in the Header element, and the Declarations and Code elements under the Snippet element. Say I wanted to emit a code snippet like the following with a placeholder over the <b><i>Boo!</i></b> string so I could overwrite it:</p>
<p style="font-family: courier new, courier">System.<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Boo!&quot;</span>); </p>
<p>To do so, I would have a code snippet that looks like this inside: </p>
<p style="font-family: courier new, courier"><span style="color: blue">&lt;?</span><span style="color: #a31515">xml</span><span style="color: blue"> </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot;<span style="color: blue"> </span><span style="color: red">encoding</span><span style="color: blue">=</span>&quot;<span style="color: blue">utf-8</span>&quot;<span style="color: blue"> ?&gt;</span>     <br /><span style="color: blue">&lt;</span><span style="color: #a31515">CodeSnippets</span><span style="color: blue">&#160; </span><span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</span>&quot;<span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">CodeSnippet</span><span style="color: blue"> </span><span style="color: red">Format</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0.0</span>&quot;<span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;</span>cwboo<span style="color: blue">&lt;/</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Declarations</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>output<span style="color: blue">&lt;/</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>Boo!<span style="color: blue">&lt;/</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Declarations</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Code</span><span style="color: blue"> </span><span style="color: red">Language</span><span style="color: blue">=</span>&quot;<span style="color: blue">csharp</span>&quot;<span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;![CDATA[</span><span style="color: gray">System.Console.WriteLine(&quot;$output$&quot;$end$);</span><span style="color: blue">]]&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Code</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&#160; &lt;/</span><span style="color: #a31515">CodeSnippet</span><span style="color: blue">&gt;</span>     <br /><span style="color: blue">&lt;/</span><span style="color: #a31515">CodeSnippets</span><span style="color: blue">&gt;</span>     </p>
<p>In this case, the IntelliSense shortcut to invoke it is <b><i>cwboo</i></b>. The code snippet declares one literal variable named <b><i>output</i></b> with a default value of <b><i>Boo!</i></b>. The actual code of the snippet has to be declared inside an XML CDATA section because some legal characters in C #and other languages could cause XML parsing errors. The <b><i>output</i></b> literal is a named placeholder in the emitted code which will be replaced with the default value and highlighted for overtyping as discussed earlier. You can see that when you use a literal in the snippet, you enclose it within leading and trailing <b><i>$</i></b> delimiters. Everything else in the snippet (except other variables) is emitted as plain text in the editor when the snippet is invoked. You can see I am also using a built-in literal <b><i>$end$</i></b>, which specifies where the input cursor is placed when you hit <b><i>Enter</i></b> to leave the placeholder mode of snippets.</p>
<h2>Creating Your Own Snippets</h2>
<p>By now you are probably emitting some form of &quot;Eeewwww!!!&quot; sound at the thought of editing the XML whenever you want to create your own snippets. The good news is that you don&#8217;t have to. There are a couple of free tools out there to help you create and edit snippets without ever needing to touch the XML directly. One I have been using for years as a C# guy is Snippy (<a href="http://www.codeplex.com/snippy">http://www.codeplex.com/snippy</a>). It puts a very simple dialog interface on top of the XML schema for you to create your snippets (see Figure 1).</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/snippy.png"><img title="snippy" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="480" alt="snippy" src="http://devshaped.com/wp-content/uploads/2009/01/snippy-thumb.png" width="482" border="0" /></a> </p>
<p><strong><em>Figure 1: Snippy</em></strong></p>
<p>A great way to start many custom snippets is to start with an existing one and just edit it. Just remember to do a &quot;Save As…&quot; and save it to your Documents code snippets folder mentioned earlier so you don&#8217;t overwrite the built-in ones.</p>
<p>Another choice is the Code Snippet Editor for Visual Basic 2008 (</span><a href="http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx">http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx</a>). For those language bigots, you will have to get over the title: it actually supports multiple languages (see Figure 2).</p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/csevb.png"><img title="CSEVB" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="400" alt="CSEVB" src="http://devshaped.com/wp-content/uploads/2009/01/csevb-thumb.png" width="482" border="0" /></a> </p>
<p><strong><em>Figure 2: Code Snippet Editor for Visual Basic 2008</em></strong></p>
<p>Either one of these will allow you to quickly create your own snippets and stop typing the same code over and over again.</p>
<h2>Code Snippet Functions</span></h2>
<p>I showed you the switch snippet earlier in the article, and that may lead you down the path of thinking you can pull off anything with a snippet. Unfortunately that is one of the most powerful ones you will find. There is a whopping three functions in the C# library for expanded capabilities in your snippets (</span><a href="http://msdn.microsoft.com/en-us/library/ms242312.aspx"><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">http://msdn.microsoft.com/en-us/library/ms242312.aspx</span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">). The <b><i>GenerateSwitchCases</i></b> function is the one that did the magic for the <b><i>switch</i></b> code snippet. The <b><i>ClassName</i></b> function will emit the class name of the class in which the snippet is invoked. This is used by the <b><i>ctor</i></b> snippet I mentioned earlier to declare a default constructor. And lastly there is the <b><i>SimpleTypeName</i></b> function, which will take a fully qualified type name, and will emit both a <b><i>using</i></b> statement in C# (<b><i>Imports</i></b> in VB) and the root class name so that you don&#8217;t have fully qualified types in the emitted code. This is used by the built-in snippet <b><i>cw</i></b>, which emits a Console.WriteLine statement. Take a look at that built-in snippet if you want to see the way it is used. Unfortunately there is no way to add your own custom functions. If you want more power to do more custom things in your snippets, you might want to take a look at third party tools such as Developer Express CodeRush (</span><a href="http://www.devexpress.com/CodeRush"><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">http://www.devexpress.com/CodeRush</span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">) or JetBrains ReSharper (</span><a href="http://www.jetbrains.com/resharper/">http://www.jetbrains.com/resharper/</span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;">). They have things called templates that are like code snippets on steroids and are also extensible.</span> </p>
<h2>Wrap Up</h2>
</p>
<p>Now you should have a good idea what code snippets are, how they work, and how to go beyond what you get out of the box. Let me share with you my approach to code snippets: If I find myself typing some code and I get that &quot;déjà vu&quot; feeling of &quot;I know I&#8217;ve written this little chunk of code before&quot;, I stop myself, write it as a code snippet, and then I won&#8217;t ever have to write it again. Too often developers take the longer path because they think they will be &quot;wasting time&quot; by going and doing something like authoring a code snippet for the code they are writing. Many times, it is not authoring the code snippet that is the real waste of time. </p>
<p>&#8212;-</p>
</p>
</p>
<p><em>Brian Noyes is chief architect of IDesign (</span></em><a href="http://www.idesign.net"><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;"><em>www.idesign.net</em></span></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;"><em>), a Microsoft Regional Director and MVP, writer and speaker. He is the author of Developing Applications with Windows Workflow Foundation, Smart Client Deployment with ClickOnce, and Data Binding with Windows Forms 2.0. He is also a frequent speaker at conferences worldwide including Microsoft TechEd, DevConnections, VSLive!, DevTeach and others. You can contact him through his blog at </em></span><a href="http://briannoyes.net"><em>http://briannoyes.net</span></em></a><span style="font-family: &quot;Arial&quot;,&quot;sans-serif&quot;"><em>. </em></span></p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/accelerate-your-coding-with-code-snippets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Practical Programming: Sending Email</title>
		<link>http://devshaped.com/2009/01/practical-programming-sending-email/</link>
		<comments>http://devshaped.com/2009/01/practical-programming-sending-email/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 01:33:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[Practical Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/01/practical-programming-sending-email/</guid>
		<description><![CDATA[By Derek Hatchard
 Sending email from a .NET application is incredibly simple:
using System.Net.Mail;     &#8230;     SmtpClient _smtp = new SmtpClient(&#34;smtp.test.com&#34;);     _smtp.Send(&#34;from@test.com&#34;,&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;derek@devshaped.com&#34;,&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;Subject&#34;,&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;Body&#34;); 
With just these few lines of code, you are sending an email from from@test.com to derek@devshaped.com. Pretty [...]]]></description>
			<content:encoded><![CDATA[<p>By <a href="http://derekh.com/" target="_blank">Derek Hatchard</a></p>
<p><img style="border-right-width: 0px; margin: 0px 0px 22px 22px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Derek_Hatchard" border="0" alt="Derek_Hatchard" align="right" src="http://devshaped.com/wp-content/uploads/2009/01/derek-hatchard.jpg" width="85" height="57" /> Sending email from a .NET application is incredibly simple:</p>
<div style="font-family: courier new"><span style="color: blue">using</span> System.Net.Mail;     <br />&#8230;     <br />SmtpClient _smtp = <span style="color: blue">new</span> SmtpClient(<span style="color: maroon">&quot;smtp.test.com&quot;</span>);     <br />_smtp.Send(<span style="color: maroon">&quot;from@test.com&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;derek@devshaped.com&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Subject&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Body&quot;</span>); </div>
<p>With just these few lines of code, you are sending an email from from@test.com to derek@devshaped.com. Pretty easy stuff.</p>
<p>But what if you need something a little more complicated? The example above assumes the standard SMTP port for email with no authentication and an unencrypted connection. Fortunately these “complications” are quite easy to deal with in .NET.</p>
<p> <span id="more-31"></span><br />
<h2>Different Port</h2>
<p>If you need to send email on a different port, simply add the following line:</p>
<div style="font-family: courier new">_smtp.Port = <span style="color: maroon">587</span>;</div>
<h2>SMTP Authentication</h2>
<p>If you are not sending through a local SMTP server, your app will likely need to authenticate itself. If you need to pass credentials to the SMTP server, use the Credentials property on the SmtpClient class:</p>
<div style="font-family: courier new"><span style="color: blue">using</span> System.Net;     <br />&#8230;     <br />_smtp.Credentials = <span style="color: blue">new</span> NetworkCredential(<span style="color: maroon">&quot;username&quot;</span>, <span style="color: maroon">&quot;pwd&quot;</span>);</div>
<h2>Encrypted Connection</h2>
<p>Since you are passing credentials to the server, you really should be using a secure connection (many mail providers <em>require</em> you to use a secure connection). In code, it’s just one line:</p>
<div style="font-family: courier new">_smtp.EnableSsl = <span style="color: maroon">true</span>;     </div>
<h2>Sending Email On Behalf of Someone</h2>
<p>A final practical consideration when sending email is the distinction between From and Sender. In a simple scenario, you only need a From address. But if you are writing code to send email, chances are good that there is something not-quite-so-simple happening. </p>
<p>Perhaps you have received an email with a header like this in Outlook:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="fromsender" border="0" alt="fromsender" src="http://devshaped.com/wp-content/uploads/2009/01/fromsender.jpg" width="382" height="88" /> </p>
<p>This is an example of one email account sending an email on behalf of someone else. This is often used by automated systems and it is trivial to add to your application. To add a Sender you cannot pass values directly to an SmtpClient object; you must create a new instance of the MailMessage class, set the properties, and then pass the MailMessage object to the SmtpClient object:</p>
<div style="font-family: courier new">MailMessage _msg = <span style="color: blue">new</span> MailMessage(<span style="color: maroon">&quot;from@test.com&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;derek@devshaped.com&quot;</span>,&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Subject&quot;</span>,&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Body&quot;</span>);&#160; <br />_msg.Sender = <span style="color: blue">new</span> MailAddress(<span style="color: maroon">&quot;sender@test.com&quot;</span>);</div>
<div style="font-family: courier new">_smtp.Send(_msg); </div>
<p>Keep in mind that your message is going through an SMTP server that might not let you set arbitrary From and Sender values.</p>
<h2>Bringing it All Together</h2>
<p>Here is what the final code looks like that incorporates an alternate port, credentials, encrypted transport, and a sender:</p>
<div style="font-family: courier new"><span style="color: blue">using</span> System.Net;     <br /><span style="color: blue">using</span> System.Net.Mail;     <br />&#8230;     <br />SmtpClient _smtp = <span style="color: blue">new</span> SmtpClient(<span style="color: maroon">&quot;smtp.test.com&quot;</span>);     <br />_smtp.Port = <span style="color: maroon">587</span>;     <br />_smtp.Credentials = <span style="color: blue">new</span> NetworkCredential(<span style="color: maroon">&quot;username&quot;</span>, <span style="color: maroon">&quot;pwd&quot;</span>);     <br />_smtp.EnableSsl = <span style="color: maroon">true</span>;     </p>
<p>MailMessage _msg = <span style="color: blue">new</span> MailMessage(<span style="color: maroon">&quot;from@test.com&quot;</span>,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;derek@devshaped.com&quot;</span>,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Subject&quot;</span>,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Body&quot;</span>);     <br />_msg.Sender = <span style="color: blue">new</span> MailAddress(<span style="color: maroon">&quot;sender@test.com&quot;</span>);     <br />_smtp.Send(_msg); </div>
</p>
<p>&#8212;-</p>
<p>Derek Hatchard is the content editor for <a href="http://microsoft.com/youshapeit/msdn">http://microsoft.com/youshapeit/msdn</a> and <a href="http://devshaped.com">http://devshaped.com</a>. He is also the founder of <a href="http://crowdspace.net/" target="_blank">Crowd Space</a>. You can find him online at <a href="http://derekh.com">http://derekh.com</a>, <a href="http://ardentdev.com">http://ardentdev.com</a>, and <a href="http://twitter.com/derekhat">http://twitter.com/derekhat</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/practical-programming-sending-email/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Working with Brownfield Code</title>
		<link>http://devshaped.com/2009/01/working-with-brownfield-code/</link>
		<comments>http://devshaped.com/2009/01/working-with-brownfield-code/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 06:03:43 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[belcham]]></category>
		<category><![CDATA[brownfield]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/01/working-with-brownfield-code/</guid>
		<description><![CDATA[By Donald Belcham
During our careers as developers we sit in training courses, conference sessions and vendor demos in which we hear about how easy and great technology XYZ is for creating applications. And almost every one of them assumes that we&#8217;re working on new development. I&#8217;m not sure about you, but I find that new [...]]]></description>
			<content:encoded><![CDATA[<p>By <a href="http://www.igloocoder.com" target="_blank">Donald Belcham</a></p>
<p><a href="http://devshaped.com/wp-content/uploads/2009/01/donald-belcham.jpg"><img title="Donald_Belcham" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 22px 22px; border-left: 0px; border-bottom: 0px" height="57" alt="Donald_Belcham" src="http://devshaped.com/wp-content/uploads/2009/01/donald-belcham-thumb.jpg" width="85" align="right" border="0" /></a>During our careers as developers we sit in training courses, conference sessions and vendor demos in which we hear about how easy and great technology XYZ is for creating applications. And almost every one of them assumes that we&#8217;re working on new development. I&#8217;m not sure about you, but I find that new (or greenfield) application development has been fairly rare in my career.</p>
<p>More often than not, we developers are working in companies that already have a suite of custom software. If you&#8217;re brought in to work on a specific project, it&#8217;s rare that the project hasn&#8217;t had something already started on it. As a result, most developers spend a large part of their careers working with or on someone else&#8217;s code. I don&#8217;t think this is a bad thing. Instead I think that it is something that we should embrace with good practices, strong tooling and a strong desire to succeed. </p>
<p> <span id="more-5"></span>If these projects aren&#8217;t classified as greenfield, how should we refer to them? One of the most common terms in application development is &#8216;legacy&#8217;. In his highly regarded book &quot;Working Effectively with Legacy Code&quot;, Michael Feathers defines legacy code as:
</p>
<p style="font-style: italic">&quot;&#8230;code from the past maintained because it works&quot;</p>
<p>I completely agree with this definition, but it does leave us with a gap. Partially completed applications fall squarely in between legacy (maintained and working) and greenfield (not inhibited by past technical decisions or debt). They have incurred some amount of technical debt and are probably constrained by some design or architectural decisions. They also are only partially completed so we&#8217;re not working with the code base solely to maintain an already functioning application. Heck, none of the application may function at all yet. It&#8217;s these applications that we developers often find ourselves working on.</p>
<h2>What is Brownfield?</h2>
<p>Brownfield is an increasingly common term being used to define application code bases that are neither greenfield nor legacy. My definition of brownfield is:</p>
<p style="font-style: italic">&quot; a project, or codebase, that was previously created and may be contaminated by poor practices, structure, and design but has the potential to be revived through comprehensive and directed refactoring&quot;.</p>
<p>Brownfield applications exist all over the world, at all companies and in most project teams. Unfortunately they&#8217;re the norm, not the exception in our industry. The developer across the world, sitting next to you, and even you and I are all creating brownfield applications on a daily basis.</p>
<p>The fact is that as soon as we write a line of code, we have accepted that we are going to maintain it in that form. No matter how well you write code, you are, at the point of finishing that line of code, incurring some level of technical debt and some level of technical constraint going forward. Every single line of code you write is adding to or otherwise modifying the level at which those debts exist.</p>
<p>These statements shouldn&#8217;t scare you. With today&#8217;s tools (Visual Studio, Resharper, CodeRush and others) modifying code after it has been created is easier than ever. Refactoring code to lessen the debt or constraints that it places on the project and team is something that is actively encouraged in development circles. There are some things that are important to ensuring that you can do this efficiently and effectively.</p>
<h2>Confidence</h2>
<p>The largest factor that you need to take into account when working with brownfield code is the confidence level that you, your testers and your client will have with the act of modifying code, without breaking functionality. Nothing scares testers and clients more than the prospect of failing regression tests and bugs introduced to already functional code. It&#8217;s not just frustrating for them; it&#8217;s also very demoralizing for a development team. As soon as defects are being introduced from what should be innocuous code refactorings, the development team starts to lose confidence in their abilities to manage the code, their abilities to work with the code in its current state, and the their ability to deliver a product to the client. Those things are a well baked recipe for disaster.</p>
<p>I have seen this on projects which I&#8217;ve joined part way through their completion. In one case I was working with a team where the developers and the business analysts would physically break into cold sweats when there was mention of having to alter one component within the application. They did anything that they could to avoid having to fix bugs or rework that code. That was a team that had completely lost its confidence.</p>
<p>There are some tools that you can use to build or maintain a team&#8217;s confidence. One of them is automated testing. Even if your team is currently confident about its ability to refactor code, creating a suite of unit and/or integration tests will prove to them that their confidence isn&#8217;t misplaced. When a team is lacking confidence, adding tests prior to making changes and ensuring that they&#8217;re still passing after the changes have been effected will work to rebuild that confidence.</p>
<p>I know that some of you will now be saying that you don&#8217;t have the time, resources or budget to build a comprehensive test suite for your brownfield application. You&#8217;re probably right. Creating tests after the code has been completed is a laborious task, more so if your project has a significant amount of code. Instead of worrying about comprehensive application-wide test suites, let&#8217;s think about them in a more localized and granular fashion.</p>
<p>If you&#8217;re looking to rework a small portion of an application to increase its maintainability, work first at creating a good test suite for that small portion of the application. This approach of creating confidence-building tests only for the portions of the application that are going to be changed will allow you to minimize the effort required for creating tests while maximizing the confidence the team has in changes it makes. This approach will possibly never see your application have a comprehensive test suite, but any changes that you have made will be covered by tests.</p>
<p>The advice for creating tests prior to refactoring code also stands true when the team is faced with modifying code to fix defects or to add new functionality. In the case of fixing defects, first creating a test that exposes the bug (and fails as a result of that bug) allows you to have full confidence that you&#8217;ve fixed it. Likewise creating tests that confirm new functionality prior to creating the code to implement those capabilities within the application will give you a strong understanding of when you&#8217;ve completed the required task.</p>
<h2>All Shall Build</h2>
<p>Confidence in implementing, fixing and refactoring code is only one part of the brownfield project experience. Confidence on projects comes in many forms. For instance, do you have full confidence that anyone on your project team could correctly compile and deploy your application to any of your testing, staging or production environments? My experience is that there is usually one person on the team who can perform this task and it usually involves a combination of black magic, voodoo and chicken sacrifices.</p>
<p>Why not work towards a scenario where any one of the developers on the team can, and does, regularly create a release package? Traditional methods of compiling applications in the .NET sphere have been limited to &quot;Rebuild All&quot; within the Visual Studio IDE. While there can be some automation included through the use of pre- and post-build events, &quot;Rebuild All&quot; is a largely manual process when you include tasks such as test execution and release package construction. While this can work, it doesn&#8217;t necessarily offer the consistency, or flexibility, of using a build script. There are many build scripting tools and languages available to .NET developers. Each offers the same fundamental features, but each also offers a twist.</p>
<p>If you create a compilation, testing and release script that is available to every developer on the team, it becomes one of your projects mechanisms for creating confidence in release management. If the script is available to all developers at all times, they should be encouraged, through simple tooling and fast execution, to run it as many times per day as possible. Every time that you run a script like this you are testing a technically complex portion of your release management process. If that process is being executed tens of times per day, the confidence level of the team and its management and clients should soar.</p>
<h2>Social Considerations</h2>
<p>We could continue this article with a long and detailed discussion on the merits of using fundamental OO code practices to ensure that your code is flexible, decoupled and robust. Instead I&#8217;d like to finish with a brief discussion on the social aspect of working with brownfield applications.</p>
<p>Joining an already started project can present interesting issues both in personal and team dynamics. We all join a new project or job thinking that we can bring greatness to the team or job. As a result we often head into these new engagements bringing suggestions about changes to techniques, tools or practices that we&#8217;ve had success with in the past. Unfortunately this usually happens without us taking the time to properly and completely assess the current situation. This lack of understanding around past decisions and current situations, combined with the vocal desire to try to improve things, often leads new team members to be seen in a negative light.</p>
<p>When joining a team we also have to be aware that the currently serving team members probably have an emotional attachment to the current code base. While many developers believe that they are able to openly take constructive criticism of their past or present endeavours, often they can&#8217;t. When suggesting changes to a code base, new team members need to be cognizant of the potential negative impact that they may be inflicting on the team&#8217;s morale. While this is a delicate situation to have to step around while working to improve code, it is one that is vitally important if the team, as a whole, is going to improve productivity, maintainability and quality going forward. Getting team members to believe in the reasons behind the changes that you&#8217;re proposing instead of the changes themselves can be an effective technique.</p>
<p>Many projects that we developers encounter should be categorized as brownfield. While each of these projects encounters its own set of unique issues, there are some underlying problems that are seen on many. Projects might suffer from a lack of confidence in code, releases and quality. They can also have severe maintainability issues caused by poor coding practices. Those technical issues can be some of the easiest to solve, but each technical problem can expose a series of potentially debilitating social and team cohesion issues.</p>
<p>Don&#8217;t let any of these issues scare you though. You&#8217;re probably already working on a brownfield project and having to deal with many of them. Remember that much of what we do in this industry is deal in areas of confidence between developers and management, developers and clients, and developers amongst themselves.</p>
<p>&#8212;</p>
<p><em>Donald Belcham is an independent contractor in Edmonton, Alberta, Canada who specializes in software development with the .NET platform.&#160; With more than 7 years experience delivering Web and Smart Client applications to government and Fortune 100 clients in North America and the South Pacific, he has experience and knowledge in the entire software development life cycle.&#160; Combining that experience with his passion for agile development practices and solid OO fundamentals, Donald works to provide the client with software that works for their business.</em></p>
<p><em>Recognized by Microsoft for his technical skill and community contribution with the Microsoft MVP award in C#, Donald is a notable leader in the developer community.&#160; In addition to being a founding member, and current President, of the Edmonton .NET User Group, Donald regularly speaks for .NET User Groups and Code Camps across North America on topics ranging from development practices to the intricacies of different technologies.</em></p>
<p><em>His thoughts on software development can be found on his blog at <a href="http://www.igloocoder.com" target="_blank">www.igloocoder.com</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/working-with-brownfield-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
