Friday, January 14, 2011

Visual Studio Code Kata Project Template

I just attended CodeMash, and I was really impressed by the pre-compiler all-day session "Software Craftsmanship" by Steve Smith (@ardalis) and Brendan Enrick (@brendoneus). During the session, we did several coding katas. The process for each kata was the same: start by writing a single test, then, as simply as possible, make the test pass. For example, don't do any logic, just return the expected result for the test. Then write another test, and, again, as simply as possible, make it (and the other one) pass. Continue until all use cases have corresponding tests, and all tests pass. This was probably my favorite session at CodeMash: I've known what TDD was in an abstract way, but until now, I didn't "get" it. Kudos to Steve and Branden.

However, there was one thing that slightly annoyed me during this session: when we started a new kata, I first had to create a new project/solution in Visual Studio (a class library), then delete Class1.cs, then add a reference to NUnit, then add a new class (the one that performs the kata), then add another class (the test class), and finally, decorate the test class with [TestFixture]. After all of this, I was ready to start.

I am far too lazy to go through all of these steps for each kata. So I created a Visual Studio project template to handle these routine, boring steps for me.

This was my first attempt at creating a VS project templat, so there was some fumbling around. I googled around and found these two MSDN articles: How to: Create Project Templates and Template Parameters. These two articles got me most of the way there.

After creating a tedious kata project as described above, I used Visual Studio's Export Template (File > Export Template...). After extracting the resulting .zip file, I hand modified the two .cs files, the .vstemplate, and the .csproj file so that the name of the project would be used for the kata and kata test classes. I then re-zipped the files and copied the .zip to the Visual Studio templates directory (located at C:\Users\<UserName>\Documents\Visual Studio 2010\Templates\ProjectTemplates for VS 2010 on Windows 7).

That's it! Now, when I get the urge to do a code kata, I can get started in seconds instead of minutes.

Kata Template.zip

EDIT: I've made the template version-agnostic for NUnit (it had been looking for a specific version of NUnit). This change should make usable for any version of NUnit. However, this will probably cause problems for those who have multiple versions of NUnit installed. In that case, unzip the template and edit KataProject.csproj. Change the NUnit reference to point to the version that you need. Rezip the files and put it in the ProjectTemplates directory and you should be good to go.

Wednesday, November 10, 2010

XML Serializable Immutable Objects and Lists

In my current project, I'm using a client/server architecture, where, among other things, the client tells the server what its configuration should be. Because multiple clients can change the server's configuration, I found it necessary to maintain the history of all configurations that have been applied to the server. I modeled the workflow after Subversion, where clients have the ability to update and commit (but no branching or merging). In order to keep things simple, I decided to store the history as a flat file - an XML serialization of the classes that represent the history. This file looks something like this:

<History>
<Version>
<VersionInfo Number="1" Timestamp="2010-02-11 13:38:04" />
<Commit>
<User>MyComputerName\MyUsername</User>
<Name>Brian</Name>
<Notes>Initial commit.</Notes>
</Commit>
<Configuration>
<Something>Initial Value</Something>
</Configuration>
</Version>
<Version>
<VersionInfo Number="2" Timestamp="2010-02-11 14:16:52" />
<Commit>
<User>MyComputerName\MyUsername</User>
<Name>Brian</Name>
<Notes>Changed something.</Notes>
</Commit>
<Configuration>
<Something>Changed Value</Something>
</Configuration>
</Version>
</History>

The original version of these classes look as you would expect (versions were stored in a generic list). The client would serialize a Commit object and a changed Configuration object and send them to the server. The server would create a Version object, give it a VersionInfo object with a Number and a Timestamp, add it to the History, then save it to disk. The client would then update its history.

This would have been fine and dandy if it weren't for my paranoia. I feared that some future developer would modify the history after it was committed. This completely violates the idea of a history. So I set about devising a way to prevent that. What I needed were mostly-immutable objects - objects, whose properties could be set only once.

What I came up with was a pair of generic classes: Immutable<T> and ImmutableList<T>.

public class Immutable<T>
{
private bool isSet;
private T value;

public T Value
{
get
{
return this.value;
}
set
{
if (isSet)
{
throw new InvalidOperationException();
}

this.value = value;
isSet = true;
}
}
}

public class ImmutableList<T> : IList<T>
{
private readonly List<T> list = new List<T>();

public void Add(T item)
{
this.list.Add(item);
}

public void Clear()
{
throw new InvalidOperationException();
}

public bool Remove(T item)
{
throw new InvalidOperationException();
}

public void Insert(int index, T item)
{
this.list.Insert(index, item);
}

public void RemoveAt(int index)
{
throw new InvalidOperationException();
}

public T this[int index]
{
get
{
return this.list[index];
}
set
{
throw new InvalidOperationException();
}
}

<snip>
}

Immutable<T> only lets you set its value once, and ImmutableList<T> only lets you add or insert items - never clearing, removing, or replacing. If you attempt to violate these rules, a big, nasty InvalidOperationException is thrown.

public class History
{
private readonly Immutable<ImmutableList<Version>> versions = new Immutable<ImmutableList<Version>>();

public ImmutableList<Version> Versions
{
get
{
return versions.Value;
}
set
{
versions.Value = value;
}
}
}

As you can see, History can only have its Versions property set once, which is all you need when XML deserializing. You'll also only be able to add to a Version - never remove or replace.

public class Version
{
private readonly Immutable<VersionInfo> versionInfo = new Immutable<VersionInfo>();

private readonly Immutable<Commit> commit = new Immutable<Commit>();

private readonly Immutable<Config> configuration = new Immutable<Config>();

public VersionInfo VersionInfo
{
get
{
return this.versionInfo.Value;
}
set
{
this.versionInfo.Value = value;
}
}

public Commit Commit
{
get
{
return this.commit.Value;
}
set
{
this.commit.Value = value;
}
}

public Configuration Configuration
{
get
{
return this.configuration.Value;
}
set
{
this.configuration.Value = value;
}
}
}

One downside to this is I can't use my beloved automatic properties for dumb data object like these. But, I suppose they're not quite as dumb now, are they?