<?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:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Journal of a software dev</title>
	<atom:link href="http://journalofasoftwaredev.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://journalofasoftwaredev.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sat, 12 Sep 2009 18:52:14 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='journalofasoftwaredev.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/a8ba7105a8b13690b2edefbf38c69ebc?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Journal of a software dev</title>
		<link>http://journalofasoftwaredev.wordpress.com</link>
	</image>
			<item>
		<title>Microsoft Sync Framework</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/09/12/microsoft-sync-framework/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/09/12/microsoft-sync-framework/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 18:52:14 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[file replication]]></category>
		<category><![CDATA[file synchronization]]></category>
		<category><![CDATA[FileSyncProvider]]></category>
		<category><![CDATA[microsoft sync framework]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=395</guid>
		<description><![CDATA[I recently had a requirement to move backups from a server&#8217;s local filesystem over to a network location now my first thoughts were of a windows service and a FileSystemWatcher watching the local directory and upon a folder/file being changed copy the file over to the network, however once you start digging into the details [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=395&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I recently had a requirement to move backups from a server&#8217;s local filesystem over to a network location now my first thoughts were of a windows service and a <code>FileSystemWatcher</code> watching the local directory and upon a folder/file being changed copy the file over to the network, however once you start digging into the details you find it isn&#8217;t as straight forward for one thing if a backup file is deleted on the server to free up space (ony the last month&#8217;s worth for example) this should be reflected on the network end plus there&#8217;s also file renames, locked files etc&#8230;</p>
<p>Anyhow I googled around and found the <a href="http://msdn.microsoft.com/en-us/sync/">MS Sync Framework</a> which is specially created to deal with these scenario&#8217;s and out of the box comes with providers for dealing with file synchronization great stuff! The framework is pretty big as it has extension points to let you do stuff with custom providers (for other transport synchronization), custom data transformation, metadata stored items etc&#8230; however for what I wanted I barely needed to scratch the surface.</p>
<p>In it&#8217;s most basic form you end up with the following code:</p>
<pre class="brush: csharp;">
FileSyncProvider sourceProvider = null ;
FileSyncProvider destinationProvider = null ;

try
{
    sourceProvider = new FileSyncProvider(
        sourceReplicaId, sourceReplicaRootPath, filter, options);
    destinationProvider = new FileSyncProvider(
        destinationReplicaId, destinationReplicaRootPath, filter, options);

    destinationProvider.AppliedChange +=
        new EventHandler &lt;AppliedChangeEventArgs&gt;(OnAppliedChange);
    destinationProvider.SkippedChange +=
        new EventHandler &lt;SkippedChangeEventArgs&gt;(OnSkippedChange);

    SyncOrchestrator orchestrator = new SyncOrchestrator ();
    orchestrator.LocalProvider = sourceProvider;
    orchestrator.RemoteProvider = destinationProvider;
    orchestrator.Direction = SyncDirection.Upload; // Sync source to destination

    Console .WriteLine( &quot;Synchronizing changes to replica: &quot; +
        destinationProvider.RootDirectoryPath);
    orchestrator.Synchronize();
}
finally
{
    // Release resources
    if (sourceProvider != null ) sourceProvider.Dispose();
    if (destinationProvider != null ) destinationProvider.Dispose();
}
</pre>
<p>There is one gotcha that caught me out, the <a href="http://msdn.microsoft.com/en-us/sync/">MS Sync Framework</a> will not do any of the scheduling of when the synchronization is performed that is completely dealt with by you, so in my case I was back to using a <code>FileSystemWatcher</code> to watch for file changes in the source folder and once a change was detected I would then tell the <code>SyncOrchestrator</code> to synchronize this seemed to work fine and was not that much extra work. The only other step was to wrap this functionality up to be used from a windows service and that was it! Overall I would recommend anyone looking to perform synchronization to take a look to see if either there&#8217;s something out of the box the framework gives you or if it can give you a head start.</p>
Posted in Uncategorized Tagged: file replication, file synchronization, FileSyncProvider, microsoft sync framework <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/395/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=395&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/09/12/microsoft-sync-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding Controls to a Server Control</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/08/08/adding-controls-to-a-server-control/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/08/08/adding-controls-to-a-server-control/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 10:54:08 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code example]]></category>
		<category><![CDATA[server control]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/2009/08/08/adding-controls-to-a-server-control/</guid>
		<description><![CDATA[Usually looks like this:

var row = new TableRow();
var cell = new TableCell()
    {
         Height = Unit.Pixel(30)
    });
cell.Controls.Add( new Button()
    {
        Text = &#34;Go&#34;,
        CommandName [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=389&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Usually looks like this:</p>
<pre class="brush: csharp;">
var row = new TableRow();
var cell = new TableCell()
    {
         Height = Unit.Pixel(30)
    });
cell.Controls.Add( new Button()
    {
        Text = &quot;Go&quot;,
        CommandName = &quot;Go&quot;,
        ID = &quot;btnGo&quot;
    });
row.Controls.Add(cell);
</pre>
<p>How about this instead:</p>
<pre class="brush: csharp;">
var row = new TableRow();
var cell = row.AppendControl&lt;TableCell&gt;(x =&gt;
                  {
                      x.Height = Unit.Pixel(30);
                   });
cell.AppendControl&lt;Button&gt;(x =&gt;
    {
        x.Text = &quot;Go&quot;;
        x.CommandName = &quot;Go&quot;;
        x.ID = &quot;btnGo&quot;;
    });
 </pre>
<p>Implemented with this:</p>
<pre class="brush: csharp;">
public static ControlExtensions
{
    public static TControl AppendControl&lt;TControl&gt;(this TControl control, Action&lt;tcontrol&gt; initialization)
        where TControl : Control, new()
    {
        var controlToAppend = Activator.CreateInstance&lt;TControl&gt;();
        intialization(controlToAppend);
        control.Controls.Add(controlToAppend);
        return controlToAppend;
    }
} </pre>
<p>I think this reads better especially if you end up appending lots of child controls, thoughts or any other solutions?</p>
Posted in asp.net, c#, code example Tagged: asp.net, server control <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/389/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/389/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/389/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/389/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/389/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/389/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/389/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/389/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/389/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/389/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=389&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/08/08/adding-controls-to-a-server-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
		<item>
		<title>Should we enforce constructor args?!</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/06/27/should-we-enforce-constructor-args/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/06/27/should-we-enforce-constructor-args/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 10:53:56 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[unit testing]]></category>
		<category><![CDATA[DbC]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=370</guid>
		<description><![CDATA[ Context
Say we have a Car object:

public class Car
{
    protected Engine engine;
    protected Door[] doors;
    protected Wheel[] wheels;

    public Car(Engine engine, Door[] doors, Wheel[] wheels)
    {
        Check.PreCondition(engine != null, &#34;engine must not be null&#34;);
 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=370&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3> Context</h3>
<p>Say we have a <code>Car</code> object:</p>
<pre class="brush: csharp;">
public class Car
{
    protected Engine engine;
    protected Door[] doors;
    protected Wheel[] wheels;

    public Car(Engine engine, Door[] doors, Wheel[] wheels)
    {
        Check.PreCondition(engine != null, &quot;engine must not be null&quot;);
        Check.PreCondition(doors != null, &quot;doors must not be null&quot;);
        Check.PreCondition(wheels != null, &quot;wheels must not be null&quot;);

        this.engine = engine;
        this.doors = doors;
        this.wheels = wheels;
    }

    public void EnableCentralLocking()
    {
        doors.ForEach(door =&gt; door.Lock());
    }

    public void Start()
    {
        engine.TurnOver();
    }
}</pre>
<p>The code above is used to demonstrate a context wereby we have an object that takes a number of other collabarators which are then delegated to when certain behaviour is required this is a common event in OO, You can see I use a custom assertion class to make sure the collabarators are not null.</p>
<p>One thing we pick up on is that different behaviours that the <code>Car</code> object is instructed to perform may effect only one collabarator or may effect them all for instance <code>EnableCentralLocking</code> effects the doors collabarating object but has no effect on the <code>wheels</code> or <code>engine</code>.</p>
<h3>Unit testing the above</h3>
<p>Ok so now we want to unit test the <code>Car</code> object:</p>
<pre class="brush: csharp;">
[TestFixture]
public class when_central_locking_enabled
{
    [Test]
    public void should_tell_all_doors_to_lock()
    {
        var engine = new Engine();
        var wheels = new Wheel[4];
        var doors = createStubDoors();

        var sut = new Car(engine, doors, wheels);
        sut.EnableCentralLocking();
        doors.All(door =&gt; door.Locked);
    }
}
</pre>
<p>Here I use a state based way of testing the doors have all been locked. One thing is apparent in order to create the <code>Car</code> due to the assertion checks at the beginning I have to create all the collabarators now if I was testing a behaviour that required all of them this is a must however to test the central locking the doors is the only collabarator I need, the creation of the others is just a wasted overhead.</p>
<p>Now this is how I currently create my objects that require collabarators and I was ok with this however recently I was told by a friend to check out a blog by <a href="http://misko.hevery.com/" target="_blank">Misko Hevery</a> I was astonished by how well written and the information surrounding good OO and unit testing was, anyhow I came across this <a href="http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/" target="_blank">post</a> which made me think about whether we should be asserting the collabarators in the constructor or not.</p>
<p>If we remove the assertions our test becomes:</p>
<pre class="brush: csharp;">
[TestFixture]
public class when_central_locking_enabled
{
    [Test]
    public void should_tell_all_doors_to_lock()
    {
        var doors = createStubDoors();
        var sut = new Car(null, doors, null);
        sut.EnableCentralLocking();
        doors.All(door =&gt; door.Locked);
    }
}
</pre>
<p><img class="alignright size-medium wp-image-379" title="hmmm..." src="http://journalofasoftwaredev.files.wordpress.com/2009/06/columbo.jpg?w=273&#038;h=300" alt="hmmm..." width="273" height="300" /><br />
We removed the overhead of creating the collabarators not needed for the test however our code is open to <code>NullReferenceException</code>s being thrown if were passed a null collabarator, hmmm&#8230; a real head scratcher! </p>
<h3>What are the options?</h3>
<ol>
<li>Make the responsibility of making sure objects are created be assigned to a factory object or on an IoC container</li>
<li>Initialize the fields with NullObject values, and use a property to access the field so checks can be made (this would not be enforcable)</li>
<li>Keep the assertions but allow your assertion library to be context aware, at runtime in your unit tests allow it to be in a fall through mode</li>
<li>Keep the assertions and accept the overhead, use Test Data Builder pattern / Object Mother to cut down on repeated code</li>
<li>Move the assertions into the methods, this could cause a lot of DRY violation and extra overhead.</li>
</ol>
<p>As with most of these things it&#8217;s a balance between acheiving 100% defence and overhead required in our unit tests, after looking at the options I could come up with I&#8217;m inclined to favour 1 or 3.</p>
<p>What would you use? Do you have any different options about how to balance the 2 out?</p>
Posted in unit testing Tagged: DbC, unit testing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/370/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/370/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/370/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/370/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/370/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/370/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/370/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/370/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/370/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/370/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=370&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/06/27/should-we-enforce-constructor-args/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>

		<media:content url="http://journalofasoftwaredev.files.wordpress.com/2009/06/columbo.jpg?w=273" medium="image">
			<media:title type="html">hmmm...</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 3.0 Language Features Presentation</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/06/24/c-3-0-language-features-presentation/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/06/24/c-3-0-language-features-presentation/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 07:54:42 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[devG]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=364</guid>
		<description><![CDATA[Update: Source code now available from here http://code.google.com/p/mcromwell/source/browse/#svn/trunk/3_0_Features get hold a good SVN client to checkout, such as TortoiseSVN
Yesterday evening I put together a presentation for the Isle of Man devG, I think it went ok overall considering this was my first presentation.
I have yet to make the source code available but once I get round [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=364&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>Update: Source code now available from here <a href="http://code.google.com/p/mcromwell/source/browse/#svn/trunk/3_0_Features" target="_blank">http://code.google.com/p/mcromwell/source/browse/#svn/trunk/3_0_Features</a> get hold a good SVN client to checkout, such as <a href="http://tortoisesvn.tigris.org/" target="_blank">TortoiseSVN</a></strong></p>
<p>Yesterday evening I put together a presentation for the <a href="http://fuzzelogicsolutions.com/wordpress/index.php/2009/06/01/devg-round-2-c-language-features/" target="_blank">Isle of Man devG</a>, I think it went ok overall considering this was my first <a href="http://docs.google.com/Presentation?id=dd6fjrp7_1c9jcdtcs" target="_blank">presentation</a>.</p>
<p><del datetime="2009-06-26T21:00:29+00:00">I have yet to make the source code available but once I get round to uploading it I will update this post,</del> If anyone attending has any question’s feel free to drop me an email or leave a comment.</p>
Posted in Uncategorized Tagged: devG <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/364/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=364&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/06/24/c-3-0-language-features-presentation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
		<item>
		<title>Command Query Separation (CQS)</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/05/24/command-query-separation-cqs/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/05/24/command-query-separation-cqs/#comments</comments>
		<pubDate>Sun, 24 May 2009 16:58:15 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[best practices]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[command query separation]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/2009/05/24/command-query-separation-cqs/</guid>
		<description><![CDATA[Introduction
CQS was a term that was coined by Bertrand Meyer and presented itself in his book Object Oriented Software Construction (I haven’t read the book, yet!) the principle states that every method should either be a command that changes state or a query in which case it does not change any state but not both.
In [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=361&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h4>Introduction</h4>
<p>CQS was a term that was coined by <a href="http://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a> and presented itself in his book <a href="http://www.amazon.co.uk/gp/product/0136291554/ref=s9_simx_gw_s0_p14_t1?pf_rd_m=A3P5ROKL5A1OLE&amp;pf_rd_s=center-1&amp;pf_rd_r=1KM9CRN903RCXN8MAE4E&amp;pf_rd_t=101&amp;pf_rd_p=467198433&amp;pf_rd_i=468294" target="_blank">Object Oriented Software Construction</a> (I haven’t read the book, yet!) the principle states that every method should either be a command that changes state or a query in which case it does not change any state but not both.</p>
<p>In all I agree with the principle I can think of some valid examples that are valid to break it but on the whole it’s makes sense to follow it, however what I want to show in this post is to take this principle and show how it can be applied at various levels rather than just at the method level.</p>
<h4>Architectural Level</h4>
<p>If we imagine a system which manages lots of amounts of data and the business would like to perform analysis on this data while still providing transactional throughput it would not be uncommon to split the data into separate <a href="http://en.wikipedia.org/wiki/OLTP" target="_blank">Online Transaction Processing</a> (OLTP) and <a href="http://en.wikipedia.org/wiki/OLAP" target="_blank">Online Analytical Processing</a> (OLAP), this&#160; is a good example of separating the data that will be changing often (OLTP) from the stagnant data that can be analysed (OLAP) I feel this matches strongly to the principle, the benefits gained typically result in better performance and data that reflects the needs of the operations applied to it.</p>
<h4>Object Design Level</h4>
<p>When we have a complex domain that require numerous behaviours we want to take advantage of domain modelling in our objects by utilising something like <a href="http://en.wikipedia.org/wiki/Domain-driven_design" target="_blank">DDD</a> to come up with entities, value objects etc… that we can use to represent the domain were working in. </p>
<p>We use these objects to perform transactional operations such as “Submitting an Order” or “Creating a New User” as these represent where we are going to using the behaviours and rules we put together using our objects. What we don’t want to start doing is then using these same objects for displaying result grids on a search screen, this represents a very different usage pattern and if we try to use our objects for this purpose and also for transactional processing we get into a mess quite quickly.</p>
<p>Instead it’s best to take onboard the CQS principle and separate our domain model objects that will be used for out transaction processing from what I think of as reporting or querying objects that are only interested in grabbing data for things such as search screens &amp; reports. So if we were in a issue tracking domain we would probably end up with something like this:</p>
<pre class="brush: csharp;">
// uses ORM such as nHibernate concerned only with using objects in a transactional
// context does not do any reporting or querying
public class IssueRepository : IIssueRepository
{
	public Issue FindBy(int id)
	{
		// use ORM to get by id
	} 

	public Issue Save(Issue issue)
	{
		// use ORM to save only used when new as object will be tracked
		// for updates automatically
	} 

	public void Delete(Issue issue)
	{
		// use ORM to delete
	}
} 

// probably just uses ADO.NET raw or a light facade on top of ADO.NET
// may use nHibernate but DOES NOT return any transactional objects only
// projections
public class IssueQuerying : IIssueQuerying
{
	public DataTable FindMatchingCriteria(int priorityId, int pageNo, int pageCount, out int virtualCount)
	{
		// use ADO.NET to fill datatable of results, possibly using SP to support paging
		// for older DB
	} 

	public IssueDisplayModel FindAssignedTo(string username)
	{
		// use nHibernate to fill projection object IssueDisplayModel
	}
}
</pre>
<p>The benefits we get from separating these 2 concerns:</p>
<ol>
<li>Our domain model objects are not polluted with lots of getters just to be used for reporting/querying </li>
<li>We can utilise databinding because our querying object returns objects that ASP.NET controls know what to do with (<strong>one way databinding only</strong>) </li>
<li>Our mapping is simplified as we only need to map our domain model objects to other objects for transactional processing needs not querying/reporting. </li>
</ol>
<h4>Summary</h4>
<p>Hopefully this post has shown how we can take the original CQS principle and see how it applies to other levels when developing software from the high level architecture right down to the method level.</p>
Posted in best practices, design pattern, oop, software design Tagged: command query separation <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/361/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=361&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/05/24/command-query-separation-cqs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
		<item>
		<title>log4net FallbackAppender &#8211; under the hood</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/05/16/log4net-fallbackappender-under-the-hood/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/05/16/log4net-fallbackappender-under-the-hood/#comments</comments>
		<pubDate>Sat, 16 May 2009 18:51:22 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[advice]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[log4net contrib]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/2009/05/16/log4net-fallbackappender-under-the-hood/</guid>
		<description><![CDATA[Some Background
I recently put together log4net contrib project up on google code the only item currently up there (though hopefully there should be more stuff from the community up there) is a custom appender I wrote to address the need for having an appender that is clever enough to fallback to other appenders if one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=357&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h4>Some Background</h4>
<p>I recently put together <a href="http://code.google.com/p/log4netcontrib" target="_blank">log4net contrib project</a> up on google code the only item currently up there (though hopefully there should be more stuff from the community up there) is a custom appender I wrote to address the need for having an appender that is clever enough to fallback to other appenders if one fails, this originally came from a forum post of someone wondering how to do it and after some investigation I was that <a href="http://www.nlog-project.org/" target="_blank">NLog</a> has this built-in.</p>
<h4>Investigating how to add it</h4>
<p>So my first step was to grab the <a href="http://logging.apache.org/log4net/index.html" target="_blank">log4net</a> source and see how I could integrate this appender in, one thing I really didn’t want to do was to change the log4net code and instead allow the appender to be plugged in like you would do normally.</p>
<p>First step was to see how to create an appender that holds other appenders and stumbled across <code>IAppenderAttachable</code> and then the <code>ForwardingAppender</code> which implements it, this interface is what allows you to user the following xml:</p>
<pre class="brush: xml;">
&lt;appender name=&quot;FallbackAppender&quot; type=&quot;log4netContrib.Appender.FallbackAppender, log4netContrib&quot; &gt;
      &lt;appender-ref ref=&quot;FileAppender&quot; /&gt;
      &lt;appender-ref ref=&quot;ConsoleAppender&quot; /&gt;
&lt;/appender&gt;
</pre>
<p>And the configuration makes sure that the appender is populated with the referenced appenders. Looking at the behaviour of the existing <code>ForwardingAppender</code> it gave me a lot of what I was after so I inherited from this.</p>
<h4>Hitting the main problem</h4>
<p>Now I had the <code>FallbackAppender</code> which got most of it’s behaviour from the <code>ForwardingAppender</code> the next step was to setup the following logic:</p>
<ol>
<li>Get first appender in the collection</li>
<li>Try and append to it</li>
<li>If succeeded exit</li>
<li>Otherwise try and append to the next appender in the collection</li>
<li>Rinse repeat until one succeeds or all are exhausted</li>
</ol>
<p>On the face of it this seems to be quite simple, loop the appenders and put a try…catch around the call to <code>Append</code> then have some logic in the catch to use the next appender and repeat, something like this:</p>
<pre class="brush: csharp;">
while (apppenderQueue.Count &gt; 0)
{
    try
    {
        var appender = appenderQueue.Dequeue();
        appender.Append(loggingEvent);
        break;
    }
    catch (Exception thrownException)
    {
        // log to internal log about failure
    }
}
</pre>
<p><span style="font-family:Lucida Sans Unicode;">When I tried this and tested against log4net it didn’t work only the first appender was being appended to if I setup the config incorrectly to simulate an issue. </span></p>
<p>After some digging the issue became obvious the appenders don’t let exceptions bubble up through the stack and instead log to an <code>IErrorHandler</code> that they get via a property from the <code>AppenderSkeleton</code> object they inherit from and with this being the only way to track errors from appenders I had to then impose a limitation.</p>
<h4>Adding the AppenderSkeleton limitation to go forward</h4>
<p>With the <code>ErrorHandler</code> property coming from <code>AppenderSkeleton</code> being the only way to discover errors I came to the conclusion that I needed to hook into this property luckily the property can be set, this also imposed the limitation that any of the appenders being referenced had to inherit from <code>AppenderSkeleton</code> in the inheritance tree at some point in order to get access to the <code>ErrorHandler</code> property.</p>
<h4>Putting in the hooks</h4>
<p>The next step I did was to create my own <code>IErrorHandler</code> that simply recorded if an error occurred, and at appender activation I replace each of the referenced appenders <code>ErrorHandler</code> with that one, now the code can see if an error has been raised for a particular appender and move onto the next one.</p>
<h4>Summary</h4>
<p>I guess this post demonstrates that even if a third party component doesn’t necessarily provide you with an API that makes extending behaviour easy in this example if log4net appenders let the exception bubble up or the <code>Append</code> method returned a bool whether it succeeded or not, there are still tricks we can use to try and achieve it.</p>
Posted in .net, advice, log4net, log4net contrib, software design Tagged: log4net, log4net contrib <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/357/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=357&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/05/16/log4net-fallbackappender-under-the-hood/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
		<item>
		<title>Obstacles of unit testing &amp; methods to side step them &#8211; Part 2: multi-threading</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/05/02/obstacles-of-unit-testing-methods-to-side-step-them-part-2-multi-threading/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/05/02/obstacles-of-unit-testing-methods-to-side-step-them-part-2-multi-threading/#comments</comments>
		<pubDate>Sat, 02 May 2009 07:50:28 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[TDD]]></category>
		<category><![CDATA[code example]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=331</guid>
		<description><![CDATA[Introduction
Unit testing is good. Sure it’s no silver bullet but there is no such thing! Looking back I’m not sure how I managed to work without writing unit tests the number of time having tests has saved me from tracking down bugs isn’t worth thinking about 
However everything isn’t all rosy once you start unit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=331&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h5>Introduction</h5>
<p>Unit testing is good. Sure it’s no silver bullet but there is no such thing! Looking back I’m not sure how I managed to work without writing unit tests the number of time having tests has saved me from tracking down bugs isn’t worth thinking about <img alt=":)" src="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif" /></p>
<p>However everything isn’t all rosy once you start unit testing against your code, there are a number of items that can put a spanner in the works, and that’s why I have created this series of posts to try and help other people resolve some of these common issues you may find yourself up against.</p>
<ul>
<li><a href="http://journalofasoftwaredev.wordpress.com/2009/01/31/obstacles-of-unit-testing-methods-to-side-step-them-part-1-date-and-time/" target="_blank">Date &amp; Time</a> </li>
<li>Multi-threading </li>
<li>External Dependencies </li>
<li>Sharing Static items </li>
</ul>
<h5>Prerequisites</h5>
<ul>
<li>You have already been writing unit tests </li>
<li>You are familiar with state based testing and behaviour based testing &amp; when best to use either </li>
<li>Are familiar with a mocking framework (these examples use Rhino Mocks). </li>
</ul>
<h5>Multi-threading</h5>
<p>Let’s face it once we introduce multi-threading things usually get more complicated and trying to unit test multi-threaded code is no exception! Why? Well if we think about how our code is going to be executed it will be ran on the test runner thread by NUnit, MBUnit MSTest etc… And if we have code which is going to perform work on a separate thread this causes a problem because the test runner thread will not know that it should really be waiting for work on the other thread to finish before carrying on, let’s get into some code:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [TestFixture]</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> when_adding_two_ints_together</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     [Test]</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> should_give_us_the_correct_result()</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         var processor = <span style="color:#0000ff;">new</span> Processor();</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         var sut = <span style="color:#0000ff;">new</span> AddTwoIntsWorkItem(1, 2);</pre>
<p><!--CRLF--><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         processor.DoWork(sut);</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         Assert.That(sut.Result, Iz.EqualTo(3));</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>So for the example I created a simple <code>Processor</code> object and as simple command based interface and a concrete object:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:300px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;height:300px;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Processor</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> DoWork(IWorkItem workItem)</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         ThreadPool.QueueUserWorkItem((state) =&gt;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>                 workItem.Work();</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>                 Thread.Sleep(2000);</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             });</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> IWorkItem</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     <span style="color:#0000ff;">void</span> Work();</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AddTwoIntsWorkItem : IWorkItem</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">int</span> first;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">int</span> second;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">int</span> result;        </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>     <span style="color:#0000ff;">public</span> AddTwoIntsWorkItem(<span style="color:#0000ff;">int</span> first, <span style="color:#0000ff;">int</span> second)</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>         <span style="color:#0000ff;">this</span>.first = first;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>         <span style="color:#0000ff;">this</span>.second = second;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Work()</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>         result = first + second;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> Result</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  37:</span>         get { <span style="color:#0000ff;">return</span> result; }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  38:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  39:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>You could imagine that this could represent something more real such as a scheduling object that hold a list of <code>IWorkItem’s</code> and can make calls to them in a async manner, so now we have everything setup we can run the unit test:</p>
<p><a href="http://journalofasoftwaredev.files.wordpress.com/2009/05/image.png" target="_blank"><img title="image" style="display:inline;border-width:0;" height="355" alt="image" src="http://journalofasoftwaredev.files.wordpress.com/2009/05/image-thumb.png?w=644&#038;h=355" width="644" border="0" /></a> </p>
<p>Hmm… were expecting 3 but getting 0 strange, well not really let’s try to breakdown what’s happening:</p>
<p><a href="http://journalofasoftwaredev.files.wordpress.com/2009/05/image1.png" target="_blank"><img title="image" style="display:inline;border-width:0;" height="282" alt="image" src="http://journalofasoftwaredev.files.wordpress.com/2009/05/image-thumb1.png?w=644&#038;h=282" width="644" border="0" /></a> </p>
<p>So our assertion <code>Assert.That(sut.Result, Iz.EqualTo(3));</code> is being called before the work is done hence we get zero.</p>
<p>So how can we tell the test runner thread to wait till the work is done? Well there’s <a href="http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx" target="_blank">thread.Join()</a> however we A don’t want to expose the thread object outside of the <code>Processor</code> object as this breaks encapsulation and B In this case we use the threadpool so don’t actually have a new thread created.</p>
<p>With <a href="http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx" target="_blank">thread.Join()</a> out of the question we need to look for an alternative, luckily the Threading namespace gives a good way of handling this scenario via the <code>AutoResetEvent/ManualResetEvent</code> (the difference being that <code>AutoResetEvent</code> will set it’s state to false after being signalled automatically) that’s great we now have a mechanism for telling the thread to wait however how do we know when to signal the event and allow the test runner thread to carry on? at the moment we have no way of knowing when the <code>Processor</code> has processed the <code>IWorkItem</code>.</p>
<p>I don’t think it’s unreasonable to provide someway of knowing when the <code>DoWork</code> is complete, we can provide a <code>WorkItemComplete</code> event that can be subscribed to:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Processor</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> DoWork(IWorkItem workItem)</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         ThreadPool.QueueUserWorkItem((state) =&gt;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>                 workItem.Work();</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>                 Thread.Sleep(2000);</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>                 RaiseWorkComplete();</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>             });</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> RaiseWorkComplete()</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         <span style="color:#0000ff;">if</span> (WorkComplete != <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>             WorkComplete(<span style="color:#0000ff;">this</span>, EventArgs.Empty);</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>     <span style="color:#0000ff;">public</span> EventHandler WorkComplete;</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>I have introduced the event the next step is to amend our unit test to take advantage of this event and to synchronize the test runner thread with the threadpool thread doing the work:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> should_give_us_the_correct_result()</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#008000;">// this is used to synchronize between this current thread &amp;</span></pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#008000;">// the thread that performs the work in Processor</span></pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     var evt = <span style="color:#0000ff;">new</span> ManualResetEvent(<span style="color:#0000ff;">false</span>); </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     var sut = <span style="color:#0000ff;">new</span> AddTwoIntsWorkItem(1, 2);</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     var processor = <span style="color:#0000ff;">new</span> Processor();</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     processor.WorkComplete += (sender, e) =&gt; evt.Set(); <span style="color:#008000;">// signal that work is done</span></pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     processor.DoWork(sut);</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#008000;">// tells current thread to wait till work has been done in Processor</span></pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     evt.WaitOne(3000); </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     Assert.That(sut.Result, Iz.EqualTo(3));</pre>
<p><!--CRLF--></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>It shouldn’t be too hard to see what’s going on here, I create a new <code>ManualResetEvent</code> and set it’s state to unsignalled, I subscribe to the <code>WorkComplete</code> event via a lambda which simply sets the <code>ManualResetEvent</code> to signalled, after the call the DoWork is made we wait for the <code>ManualResetEvent</code> to be signalled. So let’s give the unit test another go:</p>
<p><a href="http://journalofasoftwaredev.files.wordpress.com/2009/05/image2.png" target="_blank"><img title="image" style="display:inline;border-width:0;" height="355" alt="image" src="http://journalofasoftwaredev.files.wordpress.com/2009/05/image-thumb2.png?w=644&#038;h=355" width="644" border="0" /></a>&#160;</p>
<p>Great! that worked a treat one thing to bear in mind is when waiting for the event to be signalled is what timeout to use, in the case above I used 3000 milliseconds you typically want to use something around this figure and should be used as a failover in case the event/method that your expecting to be called isn’t and you don’t get to signal the event, this will prevent the test runner thread from getting stuck.</p>
<p>You should now be more comfortable when having to unit test code that performs work in a separate thread, if you don’t want to expose an event to know when a particular task is finished you could use <a href="http://grabbagoft.blogspot.com/2007/08/legacy-code-testing-techniques-subclass.html" target="_blank">subclass and override</a> to only provide the event on the test subclass object.</p>
Posted in code example, TDD, testing, unit testing Tagged: TDD, threading, unit testing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/331/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=331&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/05/02/obstacles-of-unit-testing-methods-to-side-step-them-part-2-multi-threading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>

		<media:content url="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif" medium="image">
			<media:title type="html">:)</media:title>
		</media:content>

		<media:content url="http://journalofasoftwaredev.files.wordpress.com/2009/05/image-thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://journalofasoftwaredev.files.wordpress.com/2009/05/image-thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://journalofasoftwaredev.files.wordpress.com/2009/05/image-thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Extending the ASP.NET MVC HtmlHelper</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/04/24/extending-the-aspnet-mvc-htmlhelper/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/04/24/extending-the-aspnet-mvc-htmlhelper/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 21:14:14 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[code example]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=310</guid>
		<description><![CDATA[The HtmlHelperobject in MVC is great if we want to have a link to an action on a controller we simply do:

&#60;%=Html.ActionLink(&#34;my link&#34;, &#34;ViewItem&#34;) %&#62;


And this will create us a link with the text &#8216;my link&#8217; and will automatically route the href to the ViewItem action on the controller in context. However the HtmlHelper object [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=310&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <code>HtmlHelper</code>object in MVC is great if we want to have a link to an action on a controller we simply do:</p>
<pre class="brush: xml;">
&lt;%=Html.ActionLink(&quot;my link&quot;, &quot;ViewItem&quot;) %&gt;
</pre>
<p><img class="alignright size-full wp-image-337" title="htmlhelper extension manual, dont leave home without one" src="http://journalofasoftwaredev.files.wordpress.com/2009/04/htmlhelper.png?w=273&#038;h=314" alt="htmlhelper extension manual" width="273" height="314" /></p>
<p>And this will create us a link with the text &#8216;my link&#8217; and will automatically route the href to the ViewItem action on the controller in context. However the <code>HtmlHelper</code> object doesn&#8217;t have everything and there are going to be times when you want to have your own HTML rendered on screen, so how can we go about doing that.</p>
<p>The answer lies with extension methods, we can use these to extend the <code>HtmlHelper</code> object and provide us with what were after, in my case it was that I wanted a link but an image enclosed instead of just text.</p>
<p>So here&#8217;s the code to achieve this:</p>
<pre class="brush: csharp;">
public static class HtmlHelperExtensions
{
    public static string ImageActionLink(this HtmlHelper helper, string imagePath, string actionName, string controllerName, object routeValues, object imageAttributes, object linkAttributes)
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
        var anchortagBuilder = new TagBuilder(&quot;a&quot;);

        anchortagBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes));
        anchortagBuilder.MergeAttribute(&quot;href&quot;, urlHelper.Action(actionName, controllerName, routeValues));

                var imageTagBuilder = new TagBuilder(&quot;img&quot;);
        imageTagBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes));
        imageTagBuilder.MergeAttribute(&quot;src&quot;, imagePath);

        anchortagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing);
        return anchortagBuilder.ToString(TagRenderMode.Normal);
    }
}
</pre>
<p>The good thing is that we get a few objects to help us build up the html and to handle the routing side of things rather than having deal with concatenating strings.</p>
<p>Notice that I use the <code>RouteValueDictionary</code> object so that I can use the <a href="http://journalofasoftwaredev.wordpress.com/2009/04/11/routevaluedictionary-and-why-we-need-hash-literals" target="_blank">anonymous type to dictionary trick</a>, the <code>UrlHelper</code> deals with returning the routed URL I just need to provide the controller and what action to invoke, and the <code>TagBuilder</code> makes  light work of creating HTML.</p>
<p>So now from my view I can use it like this:</p>
<pre class="brush: xml;">
&lt;%=Html.ImageActionLink(Html.Image(&quot;pencil.png&quot;), &quot;UpdateIssue&quot;, &quot;ManageIssue&quot;, new { id = Model.Id }, new {border = 0, alt = &quot;Edit Issue&quot;}, new { }) %&gt;
</pre>
<p>I could provide some overloads so that I don&#8217;t need to pass empty objects and to accept Dictionaries instead of just object, but you get the idea.</p>
<p>One other thing that&#8217;s worth doing is changing the web.config do that your namespace is added automatically to your views that saves having to put the Import on each view that wants to use your extensions, this can be found in the pages section in namespaces.</p>
Posted in asp.net mvc, code example Tagged: asp.net mvc <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/310/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=310&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/04/24/extending-the-aspnet-mvc-htmlhelper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>

		<media:content url="http://journalofasoftwaredev.files.wordpress.com/2009/04/htmlhelper.png" medium="image">
			<media:title type="html">htmlhelper extension manual, dont leave home without one</media:title>
		</media:content>
	</item>
		<item>
		<title>RouteValueDictionary and why we need Hash Literals</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/04/11/routevaluedictionary-and-why-we-need-hash-literals/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/04/11/routevaluedictionary-and-why-we-need-hash-literals/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 12:26:40 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[hash literals]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=323</guid>
		<description><![CDATA[When working with ASP.NET MVC you will find quite a lot of code that needs key value pairs, especially for routing and providing html attributes, and in most of these places you will see a trend whereby no Dictionary type is created but instead an anonymous type is used:

routes.MapRoute(
    &#34;Default&#34;,
    &#34;{controller}/{action}/{id}&#34;,
    new [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=323&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When working with ASP.NET MVC you will find quite a lot of code that needs key value pairs, especially for routing and providing html attributes, and in most of these places you will see a trend whereby no Dictionary type is created but instead an anonymous type is used:</p>
<pre class="brush: csharp;">
routes.MapRoute(
    &quot;Default&quot;,
    &quot;{controller}/{action}/{id}&quot;,
    new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; }
</pre>
<p>So how can it take our anonymous type and create a dictionary from it? Well the object that does the work resides in System.Web.Routing and is called <code>RouteValueDictionary</code> (I think this should be called something like <code>AnonymousTypeDictionary</code> and placed somewhere else as it doesn&#8217;t really have anything to do with routing and is used in other sections), this object implements <code>IDictionary&lt;string, object&gt;</code> and has an overloaded constructor that takes a plain <code>object</code> type, it then uses reflection to grab the properties associated with the object and uses the property name as the key and the property value as the value.</p>
<p>So when any of the ASP.NET MVC objects want to create a key value pair they simply pass the anonymous type to the <code>RouteValueDictionary</code> object&#8217;s constructor:</p>
<pre class="brush: csharp;">
// routeValues and htmlAttributes come in as object type.

return htmlHelper.ActionLink(linkText, actionName, null, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes));
</pre>
<p>And voila instant <code>IDictionary&lt;string, object&gt;</code>!</p>
<p>So if this works what&#8217;s the problem&#8230; well in IMO it&#8217;s hack to get round the issue that C# doesn&#8217;t have support for hash literals and it would seem <a href="http://devlicio.us/blogs/sergio_pereira/archive/2009/01/05/language-envy-hash-literals.aspx" target="_blank">I&#8217;m not alone</a>, it suffers from a few problems:</p>
<ol>
<li>Because it uses anonymous types this means the parameter to the method has to be of type <code>object</code>, and from an API point of view that gives us nothing to go as to what needs to be passed in.</li>
<li>It uses reflection to build the key value pairs which takes a performance hit.</li>
</ol>
<p>For instance take Boo, we could write the following:</p>
<pre class="brush: python;">
routes.MapRoute(
    &quot;Default&quot;,
    &quot;{controller}/{action}/{id}&quot;,
    { &quot;controller&quot; = &quot;Home&quot;, &quot;action&quot; = &quot;Index&quot;, &quot;id&quot; = &quot;&quot; }
</pre>
<p>Or by using the open compiler wizardry:</p>
<pre class="brush: python;">
routes.MapRoute(
    &quot;Default&quot;,
    &quot;{controller}/{action}/{id}&quot;,
    { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; }
</pre>
<p>Maybe this is asking too much from the compiler and this is as close as were going to get but that would be a shame given the problems above this approach suffers from.</p>
Posted in .net, c# Tagged: c#, hash literals <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/323/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=323&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/04/11/routevaluedictionary-and-why-we-need-hash-literals/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Windsor for Controller Creation in ASP.NET MVC</title>
		<link>http://journalofasoftwaredev.wordpress.com/2009/04/07/using-windsor-for-controller-creation-in-aspnet-mvc/</link>
		<comments>http://journalofasoftwaredev.wordpress.com/2009/04/07/using-windsor-for-controller-creation-in-aspnet-mvc/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 20:10:31 +0000</pubDate>
		<dc:creator>Michael Cromwell</dc:creator>
				<category><![CDATA[Castle Windsor]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>

		<guid isPermaLink="false">http://journalofasoftwaredev.wordpress.com/?p=311</guid>
		<description><![CDATA[Given it&#8217;s reached v1.0 I finally decided to do some work with the ASP.NET MVC framework and what better way than to take the Issue Tracker which currently runs on Rhino Igloo and refactor it to run on ASP.NET MVC, so expect the next few posts to be related to my findings of how to get things [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=311&subd=journalofasoftwaredev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Given it&#8217;s reached v1.0 I finally decided to do some work with the ASP.NET MVC framework and what better way than to take the Issue Tracker which currently runs on <a href="http://ayende.com/Blog/archive/2007/09/03/Rhino-Igloo-ndash-MVC-Framework-for-Web-Forms.aspx" target="_blank">Rhino Igloo</a> and refactor it to run on ASP.NET MVC, so expect the next few posts to be related to my findings of how to get things done with it.</p>
<p>First thing is that <a href="http://ayende.com/Blog/archive/2007/09/03/Rhino-Igloo-ndash-MVC-Framework-for-Web-Forms.aspx" target="_blank">Rhino Igloo</a> out of the box handles controller creation and uses Castle Windsor to do this ASP.NET MVC by default will use it&#8217;s own controller builder, because of this it expects your controllers to have a default no-arg constructor this is no good as I have controllers that need their dependencies passed in, so we need to replace the default controller builder with a version that we can hook up to Castle Windsor.</p>
<p>Now I could go and write my own version this is because the framework is very flexible and was built to allow you to plugin your own  objects, in this case an object that implements <code>IControllerFactory</code> will suffice however why re-invent the wheel when the guy&#8217;s over at <a href="http://mvccontrib.codeplex.com/" target="_blank">MvcContrib</a> have done most of the work for us.</p>
<h3>Getting the MvcContrib Objects</h3>
<p>Head over to to the <a href="http://mvccontrib.codeplex.com/" target="_blank">MvcContrib</a> project on codeplex and grab the <strong>extras</strong> release, now copy the MvcContrib.dll and MvcContrib.Castle.dll into your external references folder and add a reference where you will be doing the app startup.</p>
<h3>Setting the Controller Factory</h3>
<p>Inside your <code>global.asax </code>or wherever you do app startup you need to add the following code:</p>
<pre>
<pre class="brush: csharp;">
ControllerBuilder.Current.SetControllerFactory(
                new WindsorControllerFactory(this.Container));
</pre>
</pre>
<p>Notice that I&#8217;m passing the current instance of the windsor container which I get for free because I&#8217;m using the <code>UnitOfWorkApplication</code> base class from <code>Rhino.Commons</code>, you will probably have some custom way of managing your container, in which case it just needs to be passed in to the constructor of <code>WindsorControllerFactory</code>.</p>
<h3>Wiring up the Controllers</h3>
<p>Next we need to make Windsor aware of the controllers, I use <a href="http://ayende.com/Blog/archive/2006/09/16/IntroductingBinsorTheBooDSLForWindsor.aspx" target="_blank">Binsor</a> for my Windsor configuration this gives me a nice way to autowire new controllers to Windsor without making any changes:</p>
<pre>
<pre class="brush: python;">
for type in AllTypes(&quot;IssueTracker.Controllers&quot;):
    continue unless typeof(IController).IsAssignableFrom(type)
    component type.Name, type:
        @lifestyle=&quot;transient&quot;
</pre>
</pre>
<p>Gotta love the conditional expression support <a href="http://boo.codehaus.org/" target="_blank">Boo</a> has to offer it almost reads like a sentence <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  You could also use the XML configuration or programmatic configuration to configure your controllers, and that&#8217;s all there is to it!</p>
<p>Kudos to the ASP.NET team for having the foresight to allow these kind of extensions to be plugged in so easily and also to the MvcContrib contributer&#8217;s for creating this and support for other IoC containers out there.</p>
Posted in asp.net, asp.net mvc, Castle Windsor Tagged: asp.net mvc, Castle Windsor <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/journalofasoftwaredev.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/journalofasoftwaredev.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/journalofasoftwaredev.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/journalofasoftwaredev.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/journalofasoftwaredev.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/journalofasoftwaredev.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/journalofasoftwaredev.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/journalofasoftwaredev.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/journalofasoftwaredev.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/journalofasoftwaredev.wordpress.com/311/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=journalofasoftwaredev.wordpress.com&blog=2567678&post=311&subd=journalofasoftwaredev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://journalofasoftwaredev.wordpress.com/2009/04/07/using-windsor-for-controller-creation-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a779cab73bd28249be3597433f31709a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Mike Cromwell</media:title>
		</media:content>
	</item>
	</channel>
</rss>