<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Lambada Calculus - Erik Öjebo.se</title><description>A Programming Blog by Erik Öjebo</description><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/erikojebo" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="erikojebo" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">346</guid><link>http://www.erikojebo.se/Post.aspx?id=346</link><author>webmaster@erikojebo.se</author><title>Quick Tip: Copying Files that do not Exist in the Target Folder with XCOPY</title><description>Here is how you can copy only the files that do not already exist in the target directory from a given source directory. It will also copy all files that are newer in the source directory than in the target directory.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;C:\&gt;xcopy c:\temp\testcopy\source c:\temp\testcopy\target /e /d&lt;/pre&gt;&lt;br /&gt;</description><pubDate>Wed, 25 Jan 2012 13:22:46 +0100</pubDate><a10:updated>2012-01-25T13:22:46+01:00</a10:updated></item><item><guid isPermaLink="false">345</guid><link>http://www.erikojebo.se/Post.aspx?id=345</link><author>webmaster@erikojebo.se</author><title>Lazy Regex in Visual Studio</title><description>For some reason everyone who implements a regex engine feels the need to tweak the syntax so that nobody feels at home. Worst of all regex implementations is the Visual Studio one.&lt;br /&gt;&lt;br /&gt;Here is a little reminder of the VS lazy regex syntax.&lt;br /&gt;&lt;pre class="prettyprint"&gt;/*&lt;br /&gt;&lt;br /&gt;| Posix | Visual studio |&lt;br /&gt;|-------+---------------|&lt;br /&gt;| .*?   | .@            |&lt;br /&gt;| .+?   | .#            |&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;</description><pubDate>Thu, 19 Jan 2012 16:37:22 +0100</pubDate><a10:updated>2012-01-19T16:37:22+01:00</a10:updated></item><item><guid isPermaLink="false">344</guid><link>http://www.erikojebo.se/Post.aspx?id=344</link><author>webmaster@erikojebo.se</author><title>Inheritance is not an "is a" Relationship</title><description>One of the popular ways to explain inheritance in object oriented programming is that is represents an "is a" relationship. This can be true, but often it isn't.&lt;br /&gt;&lt;br /&gt;An example that Robert C. Martin likes to use is that in the real world a square is a rectangle, but it makes no sense to model that relationship with inheritance in an object oriented system.&lt;br /&gt;&lt;br /&gt;I came across another example the other day when working one of my hobby projects. The project is an ORM and I was adding basic convention support. You can specify your own conventions by implementing an IConvention interface. However, often you might not want to redefine all conventions. Instead you want to override only the ones you need to.&lt;br /&gt;&lt;br /&gt;To enable this there is a DefaultConvention class. You can derrive from this class and override the conventions you need to modify. So, inheritance is used as a means to solve a problem, but if you think of that inheritance relationship as an "is a" then it makes no sense.&lt;br /&gt;&lt;br /&gt;An overriding convention is NOT a default convention. It is actually the opposite of that.&lt;br /&gt;&lt;br /&gt;So, be careful when thinking about inheritance and think of it as a programming specific mechanism for coupling classes rather than a model of an "is a" relationship from the real world.</description><pubDate>Wed, 28 Dec 2011 23:43:06 +0100</pubDate><a10:updated>2011-12-28T23:43:06+01:00</a10:updated></item><item><guid isPermaLink="false">343</guid><link>http://www.erikojebo.se/Post.aspx?id=343</link><author>webmaster@erikojebo.se</author><title>Getting a ListBoxItem to stretch horizontally in Silverlight</title><description>When you specify an ItemTemplate for a ListBox in silverlight, you can't get it to stretch horizontally in an easy way. Setting the HorizontalAlignment of the container in the template to stretch has no effect. What you have to do is to specify an ItemContainerStyle for the ListBox and set the HorizontalContentAlignment to Stretch:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;ListBox ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser, Mode=TwoWay}"&amp;gt;&lt;br /&gt;   &amp;lt;ListBox.ItemContainerStyle&amp;gt;&lt;br /&gt;       &amp;lt;Style TargetType="ListBoxItem"&amp;gt;&lt;br /&gt;           &amp;lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&amp;gt;&lt;br /&gt;       &amp;lt;/Style&amp;gt;&lt;br /&gt;   &amp;lt;/ListBox.ItemContainerStyle&amp;gt;&lt;br /&gt;   &amp;lt;ListBox.ItemTemplate&amp;gt;&lt;br /&gt;     &amp;lt;DataTemplate&amp;gt;&lt;br /&gt;       &amp;lt;Border BorderThickness="1" BorderBrush="#CECECE" Padding="5" CornerRadius="3" &lt;br /&gt;               Background="{Binding Background}"&amp;gt;&lt;br /&gt;             &amp;lt;!-- Your stuff here --&amp;gt;&lt;br /&gt;       &amp;lt;/Border&amp;gt;&lt;br /&gt;     &amp;lt;/DataTemplate&amp;gt;&lt;br /&gt;   &amp;lt;/ListBox.ItemTemplate&amp;gt;&lt;br /&gt;&amp;lt;/ListBox&amp;gt;&lt;/pre&gt;</description><pubDate>Tue, 29 Nov 2011 08:05:53 +0100</pubDate><a10:updated>2011-11-29T08:05:53+01:00</a10:updated></item><item><guid isPermaLink="false">342</guid><link>http://www.erikojebo.se/Post.aspx?id=342</link><author>webmaster@erikojebo.se</author><title>Removing the Suck from XML with Gosu.Commons: DynamicXmlParser</title><description>Ever been bored by writing yet another XML parser? Been annoyed by all the string conversions? Let's take a look at the DynamicXmlParser in &lt;a href='https://github.com/erikojebo/gosu.commons/wiki'&gt;Gosu.Commons&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So, let's say we have an XML document containing a book catalog:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;&amp;lt;?xml version='1.0'?&amp;gt;&lt;br /&gt;    &amp;lt;Catalog&amp;gt;&lt;br /&gt;        &amp;lt;Book Id='123'&amp;gt;&lt;br /&gt;            &amp;lt;Title&amp;gt;XML Developer's Guide&amp;lt;/Title&amp;gt;&lt;br /&gt;            &amp;lt;Author FirstName='Matthew' LastName='Gambardella' /&amp;gt;&lt;br /&gt;            &amp;lt;Price&amp;gt;44.95&amp;lt;/Price&amp;gt;&lt;br /&gt;            &amp;lt;PublishDate&amp;gt;2000-10-01&amp;lt;/PublishDate&amp;gt;&lt;br /&gt;            &amp;lt;IsBetaRelease&amp;gt;false&amp;lt;/IsBetaRelease&amp;gt;&lt;br /&gt;            &amp;lt;BookType&amp;gt;Ebook&amp;lt;/BookType&amp;gt;&lt;br /&gt;        &amp;lt;/Book&amp;gt;&lt;br /&gt;        &amp;lt;Book Id='456'&amp;gt;&lt;br /&gt;            &amp;lt;Title&amp;gt;Build Awesome Command-Line Applications in Ruby&amp;lt;/Title&amp;gt;&lt;br /&gt;            &amp;lt;Author FirstName='David' LastName='Copeland' /&amp;gt;&lt;br /&gt;            &amp;lt;Price&amp;gt;20.00&amp;lt;/Price&amp;gt;&lt;br /&gt;            &amp;lt;PublishDate&amp;gt;2012-03-01&amp;lt;/PublishDate&amp;gt;&lt;br /&gt;            &amp;lt;IsBetaRelease&amp;gt;true&amp;lt;/IsBetaRelease&amp;gt;&lt;br /&gt;            &amp;lt;BookType&amp;gt;Hardcover&amp;lt;/BookType&amp;gt;&lt;br /&gt;        &amp;lt;/Book&amp;gt;&lt;br /&gt;    &amp;lt;/Catalog&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We want to cram that XML document into our domain objects:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;public class Author&lt;br /&gt;{&lt;br /&gt;    public string FirstName { get; set; }&lt;br /&gt;    public string LastName { get; set; }&lt;br /&gt;&lt;br /&gt;    public override string ToString()&lt;br /&gt;    {&lt;br /&gt;        return FirstName + " " + LastName;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Book&lt;br /&gt;{&lt;br /&gt;    public string Id { get; set; }&lt;br /&gt;    public Author Author { get; set; }&lt;br /&gt;    public decimal Price { get; set; }&lt;br /&gt;    public bool IsBetaRelease { get; set; }&lt;br /&gt;    public BookType BookType { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public enum BookType&lt;br /&gt;{&lt;br /&gt;    Ebook, Paperback, Hardcover&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, usually we start hacking away with an XmlDocument or XDocument, try to dig our way down into the document models and then convert the strings into the correct datatype to be able to store them in our objects.&lt;br /&gt;&lt;br /&gt;That code is kind of boring. Instead, let's take advantage of the dynamic features of C# 4 to do away with that stuff. Gosu.Commons has an XML parser that does just that: DynamicXmlParser.&lt;br /&gt;&lt;br /&gt;Here is what the code looks like when using the DynamicXmlParser:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;var parser = new DynamicXmlParser();&lt;br /&gt;&lt;br /&gt;var xmlCatalog = parser.Parse(xml);&lt;br /&gt;&lt;br /&gt;// Access the child elements of the catalog just as an ordinary collection property&lt;br /&gt;foreach (var xmlBook in xmlCatalog.Books)&lt;br /&gt;{&lt;br /&gt;    var book = new Book&lt;br /&gt;        {&lt;br /&gt;            // Read attributes or element values as properties on an element&lt;br /&gt;            // Values are automatically and implicitly converted to the appropriate type&lt;br /&gt;            Id = xmlBook.Id, // int&lt;br /&gt;            Author = new Author&lt;br /&gt;                {&lt;br /&gt;                    FirstName = xmlBook.Author.FirstName, // string&lt;br /&gt;                    LastName = xmlBook.Author.LastName, // string&lt;br /&gt;                },&lt;br /&gt;            Price = xmlBook.Price, // decimal&lt;br /&gt;            IsBetaRelease = xmlBook.IsBetaRelease, // bool&lt;br /&gt;            BookType = xmlBook.BookType // BookType enum&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;    Console.WriteLine("Book id: {0}, Author: {1}, Price: ${2}, IsBetaRelease: {3}, Book type: {4}", book.Id, book.Author, book.Price, book.IsBetaRelease, book.BookType);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Thanks to dynamic we can use ordinary property access syntax to find child elements of our catalog. Attributes or values of an element can be accessed the same way.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Accessing child element collections&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;If you expect there to be multiple child elements with a given element name, those elements can be accessed as a collection property. In the example there are multiple Book elements in the catalog, so you can access them through xmlCatalog.Books.&lt;br /&gt;&lt;br /&gt;In the example, the Book elements are accessed by adding a plural 's' to the element name, i.e. "Books". However this kind of access work with other plural forms as well:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Collections_can_be_accessed_with_multiple_kinds_of_pluralization()&lt;br /&gt;{&lt;br /&gt;    var xml = @"&lt;br /&gt;&amp;lt;Bag&amp;gt;&lt;br /&gt;    &amp;lt;Car /&amp;gt;&lt;br /&gt;    &amp;lt;Glas /&amp;gt;&lt;br /&gt;    &amp;lt;Glas /&amp;gt;&lt;br /&gt;    &amp;lt;Category /&amp;gt;&lt;br /&gt;    &amp;lt;Category /&amp;gt;&lt;br /&gt;    &amp;lt;Category /&amp;gt;&lt;br /&gt;    &amp;lt;Octopus /&amp;gt;&lt;br /&gt;    &amp;lt;Octopus /&amp;gt;&lt;br /&gt;    &amp;lt;Octopus /&amp;gt;&lt;br /&gt;    &amp;lt;Octopus /&amp;gt;&lt;br /&gt;&amp;lt;/Bag&amp;gt;&lt;br /&gt;";&lt;br /&gt;    var parser = new DynamicXmlParser();&lt;br /&gt;&lt;br /&gt;    var bag = parser.Parse(xml);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(1, bag.Cars.Count); // ...s&lt;br /&gt;    Assert.AreEqual(2, bag.Glasses.Count); // ...es&lt;br /&gt;    Assert.AreEqual(3, bag.Categories.Count); // ...ies&lt;br /&gt;    Assert.AreEqual(4, bag.OctopusElements.Count);  // worst case, just postfix the word Elements&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As the example shows, you can use a couple of differend pluralization forms. If none of them match your specific scenario, just use the element name and postfix it with 'Elements'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Automatic conversions&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;If you try to set a typed variable or property to a value read from the parsed XML document that value is automatically, implicitly converted to the type of the variable or property that you are trying to assign to. The requirement is that the type you are assigning to has a defined conversion in the parser.&lt;br /&gt;&lt;br /&gt;Currently, default conversions exist for int, double, float, decimal, bool, TimeSpan, DateTime and enums. New conversions can easily be added and just as easily you can override the default conversions with your own.&lt;br /&gt;&lt;br /&gt;Here is an example of how to change the default conversion for boolean values so that it accepts "0" or "1" instead of "false" and "true":&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Conversion_can_be_customized_for_any_type()&lt;br /&gt;{&lt;br /&gt;    var xml = @"&amp;lt;User Username='SomeName' Password='secret' IsAdmin='1' /&amp;gt;";&lt;br /&gt;&lt;br /&gt;    var parser = new DynamicXmlParser();&lt;br /&gt;&lt;br /&gt;    parser.SetConverter(x =&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        if (x == "1")&lt;br /&gt;            return true;&lt;br /&gt;&lt;br /&gt;        return false;&lt;br /&gt;    });&lt;br /&gt;&lt;br /&gt;    var user = parser.Parse(xml);&lt;br /&gt;&lt;br /&gt;    Assert.IsTrue((bool)user.IsAdmin);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Implicit conversions can be done when using the value in a context where the expected type can be inferred, such as assigning to a variable or using the value in a method call. If you want to convert the value when the expected type cannot be inferred you can use an explicit cast.&lt;br /&gt;&lt;br /&gt;An example of this is shown in the example above where the value is used in an assertion. If the value was not explicitly cast in the call to Assert.IsTrue, then no conversion would be triggered and the value returned would actually be an instance of the class DynamixXmlElement.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Namespaces&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Every now and then you have to parse an XML document where someone has been so kind as to use the wonderful concept of XML namespaces. How do you tackle that one with this dynamic-schynamic thingie? The answer is quite simple, just add an alias for the namespace and which URI it represents. You can then access the properties and collections just as before, by prefixing the property name with the namespace alias.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Elements_in_different_namespaces_can_be_accessed_by_prefixing_element_name_with_namespace()&lt;br /&gt;{&lt;br /&gt;    var xml =&lt;br /&gt;        @"&amp;lt;?xml version='1.0' encoding='UTF-8' ?&amp;gt;&lt;br /&gt;&amp;lt;!--  Here comes some XML --&amp;gt;&lt;br /&gt;&amp;lt;Book xmlns='http://www.somesite.org/xml/DefaultNamespace' &lt;br /&gt;      xmlns:NS='http://www.somesite.org/xml'&amp;gt;&lt;br /&gt;    &amp;lt;Title&amp;gt;The title&amp;lt;/Title&amp;gt;&lt;br /&gt;    &amp;lt;NS:Author&amp;gt;&lt;br /&gt;        &amp;lt;NS:FirstName&amp;gt;Steve&amp;lt;/NS:FirstName&amp;gt;&lt;br /&gt;        &amp;lt;NS:LastName&amp;gt;Sanders&amp;lt;/NS:LastName&amp;gt;&lt;br /&gt;    &amp;lt;/NS:Author&amp;gt;&lt;br /&gt;&amp;lt;/Book&amp;gt;&lt;br /&gt;";p&lt;br /&gt;        var _parser = new DynamicXmlParser();&lt;br /&gt;        &lt;br /&gt;        _parser.SetNamespaceAlias("http://www.somesite.org/xml", "NS");&lt;br /&gt;&lt;br /&gt;        var book = _parser.Parse(xml);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual("The title", (string)book.Title);&lt;br /&gt;    Assert.AreEqual("Steve", (string)book.NSAuthor.NSFirstName);&lt;br /&gt;    Assert.AreEqual("Sanders", (string)book.NSAuthor.NSLastName);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Conclusion / Show me teh codez!&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;There you have it. Thanks to Microsoft for adding some dynamic love and care to C#. &lt;br /&gt;&lt;br /&gt;Gosu.Commons is an open source project of mine that is up at &lt;a href='https://github.com/erikojebo/gosu.commons/wiki'&gt;GitHub&lt;/a&gt;. Feel free to poke around or even contribute. If you just want to use the thing, Gosu.Commons is also available on &lt;a href='http://nuget.org/List/Packages/Gosu.Commons'&gt;NuGet&lt;/a&gt;. To add a reference, just open the package manager console and type:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;PM&amp;gt; Install-Package Gosu.Commons&lt;/pre&gt;&lt;br /&gt;</description><pubDate>Thu, 17 Nov 2011 22:56:02 +0100</pubDate><a10:updated>2011-11-17T22:56:02+01:00</a10:updated></item><item><guid isPermaLink="false">341</guid><link>http://www.erikojebo.se/Post.aspx?id=341</link><author>webmaster@erikojebo.se</author><title>Changing Character Encoding for File in Emacs</title><description>Character encoding is a pain... If you find yourself wanting to change the encoding of a file, here is how to do it in emacs:&lt;br /&gt;&lt;br /&gt;C-x RET f utf-8 RET&lt;br /&gt;&lt;br /&gt;When you then save the file, it is written with the specified encoding. If you can't remember the exact name of the encoding type:&lt;br /&gt;&lt;br /&gt;C-x RET f TAB&lt;br /&gt;&lt;br /&gt;This will give you a list of all available encodings in the help buffer.</description><pubDate>Tue, 15 Nov 2011 15:42:22 +0100</pubDate><a10:updated>2011-11-15T15:42:22+01:00</a10:updated></item><item><guid isPermaLink="false">340</guid><link>http://www.erikojebo.se/Post.aspx?id=340</link><author>webmaster@erikojebo.se</author><title>Emacs replace-regexp-fu</title><description>The time has come to take a deeper look at the super useful emacs function &lt;em&gt;replace-regexp&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;The scenario:&lt;br /&gt;&lt;br /&gt;Let's say you have an XML document that looks something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&amp;lt;Person FirstName='Steve' LastName='Smith' Phone='555-12345' Title='Mr.' BirthDate='1950-01-01' /&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and you want to turn it into C# code, like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "Steve",&lt;br /&gt;   LastName = "Smith",&lt;br /&gt;   Phone = "555-12345",&lt;br /&gt;   Title = "Mr.",&lt;br /&gt;   BirthDate = DateTime.Parse("1950-01-01"),&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, perfect time to put our regex skills to the test. Since emacs is my editor of choice, replace-regexp is what I'll use to get the job done. The regex for extracting the values we are interested in will look something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&amp;lt;Person FirstName='\(.*?\)' LastName='\(.*?\)' Phone='\(.*?\)' Title='\(.*?\)' BirthDate='\(.*?\)' /&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note that you have to escape the parens to create a capture group and &lt;strong&gt;not&lt;/strong&gt; be literal. This is kind of backwards compared to most other regex implementations, but comes in handy when performing search &amp; replace on Lisp code :)&lt;br /&gt;&lt;br /&gt;The replace string will look like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "\1",&lt;br /&gt;   LastName = "\2",&lt;br /&gt;   Phone = "\3",&lt;br /&gt;   Title = "\4",&lt;br /&gt;   BirthDate = DateTime.Parse("\5")&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Ok, so mission accomplished. However, a week later the format is extended to include the address of the person as well:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;&amp;lt;Person FirstName='Steve' LastName='Smith' Phone='555-12345' Title='Mr.' BirthDate='1950-01-01'&amp;gt;&lt;br /&gt;  &amp;lt;Address PostalCode='12345' State='Florida' City='Jacksonville' Street='Some street' /&amp;gt;&lt;br /&gt;&amp;lt;/Person&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and the desired C# code:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "Steve",&lt;br /&gt;   LastName = "Smith",&lt;br /&gt;   Phone = "555-12345",&lt;br /&gt;   Title = "Mr.",&lt;br /&gt;   BirthDate = DateTime.Parse("1950-01-01"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "12345",&lt;br /&gt;     State = "Florida",&lt;br /&gt;     City = "Jacksonville",&lt;br /&gt;     Street = "Some street"&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Multi-line time! The new regex now includes line breaks. To enter these, you can either write the expression outside of the mini-buffer and yank it in when executing the replace-regex command, or you can enter a newline in the minibuffer by typing C-q C-j. Here is the regex:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&amp;lt;Person FirstName='\(.*?\)' LastName='\(.*?\)' Phone='\(.*?\)' Title='\(.*?\)' BirthDate='\(.*?\)'&amp;gt;&lt;br /&gt;  &amp;lt;Address PostalCode='\(.*?\)' State='\(.*?\)' City='\(.*?\)' Street='\(.*?\)' /&amp;gt;&lt;br /&gt;&amp;lt;/Person&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and the replace expression:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "\1",&lt;br /&gt;   LastName = "\2",&lt;br /&gt;   Phone = "\3",&lt;br /&gt;   Title = "\4",&lt;br /&gt;   BirthDate = DateTime.Parse("\5"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "\6",&lt;br /&gt;     State = "\7",&lt;br /&gt;     City = "\8",&lt;br /&gt;     Street = "\9"&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Still quite straight forward, as long as you get the newlines right in the search expression.&lt;br /&gt;&lt;br /&gt;Ok, so yet another week goes by, and now there is one small addition to the format: there should be a "MiddleName" attriute added to the person element:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;&amp;lt;Person FirstName='Steve' MiddleName='F.' LastName='Smith' Phone='555-12345' Title='Mr.' BirthDate='1950-01-01'&amp;gt;&lt;br /&gt;  &amp;lt;Address PostalCode='12345' State='Florida' City='Jacksonville' Street='Some street' /&amp;gt;&lt;br /&gt;&amp;lt;/Person&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here is the matching C# code:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "Steve",&lt;br /&gt;   MiddleName = "F."&lt;br /&gt;   LastName = "Smith",&lt;br /&gt;   Phone = "555-12345",&lt;br /&gt;   Title = "Mr.",&lt;br /&gt;   BirthDate = DateTime.Parse("1950-01-01"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "12345",&lt;br /&gt;     State = "Florida",&lt;br /&gt;     City = "Jacksonville",&lt;br /&gt;     Street = "Some street"&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Only a minor change, so the regex should only need a small tweak. However, adding this field brings us up to 10 match groups. If we continue with the same pattern and just add the tenth group and reference, like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "\1",&lt;br /&gt;   MiddleName = "\2",&lt;br /&gt;   LastName = "\3",&lt;br /&gt;   Phone = "\4",&lt;br /&gt;   Title = "\5",&lt;br /&gt;   BirthDate = DateTime.Parse("\6"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "\7",&lt;br /&gt;     State = "\8",&lt;br /&gt;     City = "\9",&lt;br /&gt;     Street = "\10"&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The output from the replace will then be:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "Steve",&lt;br /&gt;   MiddleName = "F.",&lt;br /&gt;   LastName = "Smith",&lt;br /&gt;   Phone = "555-12345",&lt;br /&gt;   Title = "Mr.",&lt;br /&gt;   BirthDate = DateTime.Parse("1950-01-01"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "12345",&lt;br /&gt;     State = "Florida",&lt;br /&gt;     City = "Jacksonville",&lt;br /&gt;     Street = "Steve0"&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you take a closer look at the Street value, you see that it is actually "Steve0" which is not remotely what you would have wanted it to be. Instead of referencing the 10:th capture group it is actually a reference to the 1:st capture group immediately followed by a zero. The reason for this is that emacs only allows a single digit following the backslash.&lt;br /&gt;&lt;br /&gt;What to do now? We'll have to bring out the big guns. It's Lisp time!&lt;br /&gt;&lt;br /&gt;Emacs lets you embed lisp code within your replace expression, by escaping it with "\,". A useful function for this case is match-string which takes an integer specifying the capture group number to reference. The new expression will then be:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "\,(match-string 1)",&lt;br /&gt;   MiddleName = "\,(match-string 2)",&lt;br /&gt;   LastName = "\,(match-string 3)",&lt;br /&gt;   Phone = "\,(match-string 4)",&lt;br /&gt;   Title = "\,(match-string 5)",&lt;br /&gt;   BirthDate = DateTime.Parse("\,(match-string 6)"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "\,(match-string 7)",&lt;br /&gt;     State = "\,(match-string 8)",&lt;br /&gt;     City = "\,(match-string 9)",&lt;br /&gt;     Street = "\,(match-string 10)"&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Tada! Now we're up and rolling again.&lt;br /&gt;&lt;br /&gt;For the fun of it, let's say that another week goes by and yet another attribute is added, this time it is NickName:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&amp;lt;Person FirstName='Steve' MiddleName='F.' LastName='Smith' NickName='Stevenizzle' Phone='555-12345' Title='Mr.' BirthDate='1950-01-01'&amp;gt;&lt;br /&gt;  &amp;lt;Address PostalCode='12345' State='Florida' City='Jacksonville' Street='Some street' /&amp;gt;&lt;br /&gt;&amp;lt;/Person&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Since we've removed the limitation of 9 capture groups we can just modify the regex to add the new capture group and reference.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&amp;lt;Person FirstName='\(.*?\)' MiddleName='\(.*?\)' LastName='\(.*?\)' NickName='\(.*?\)' Phone='\(.*?\)' Title='\(.*?\)' BirthDate='\(.*?\)'&amp;gt;&lt;br /&gt;  &amp;lt;Address PostalCode='\(.*?\)' State='\(.*?\)' City='\(.*?\)' Street='\(.*?\)' /&amp;gt;&lt;br /&gt;&amp;lt;/Person&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "\,(match-string 1)",&lt;br /&gt;   MiddleName = "\,(match-string 2)",&lt;br /&gt;   LastName = "\,(match-string 3)",&lt;br /&gt;   NickName = "\,(match-string 4)",&lt;br /&gt;   Phone = "\,(match-string 4)",&lt;br /&gt;   Title = "\,(match-string 5)",&lt;br /&gt;   BirthDate = DateTime.Parse("\,(match-string 6)"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "\,(match-string 7)",&lt;br /&gt;     State = "\,(match-string 8)",&lt;br /&gt;     City = "\,(match-string 9)",&lt;br /&gt;     Street = "\,(match-string 10)"&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see, since we reference the groups by reference this means that we need to increment a bunch of numbers. This is a typically boring thing to do. Since we're already in the regex mindset, let's go regex on our regex and add 1 to all the numbers that need incrementing. Fortunately we already know how to embed lisp in our replace expression, so all we need to do is to hack away at some lovely lisp code.&lt;br /&gt;&lt;br /&gt;The search expression:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;\([0-9]+\)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and the replace expression:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;\,(+ 1 (string-to-number (match-string 1)))&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;By applying this regex and the replace expression above, from the second row referencing match group 4 and downwards, we get this beauty:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "\,(match-string 1)",&lt;br /&gt;   MiddleName = "\,(match-string 2)",&lt;br /&gt;   LastName = "\,(match-string 3)",&lt;br /&gt;   NickName = "\,(match-string 4)",&lt;br /&gt;   Phone = "\,(match-string 5)",&lt;br /&gt;   Title = "\,(match-string 6)",&lt;br /&gt;   BirthDate = DateTime.Parse("\,(match-string 7)"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "\,(match-string 8)",&lt;br /&gt;     State = "\,(match-string 9)",&lt;br /&gt;     City = "\,(match-string 10)",&lt;br /&gt;     Street = "\,(match-string 11)"&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;which in turn gives us the final result:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;var p = new Person&lt;br /&gt;{&lt;br /&gt;   FirstName = "Steve",&lt;br /&gt;   MiddleName = "F.",&lt;br /&gt;   LastName = "Smith",&lt;br /&gt;   NickName = "Stevenizzle",&lt;br /&gt;   Phone = "555-12345",&lt;br /&gt;   Title = "Mr.",&lt;br /&gt;   BirthDate = DateTime.Parse("1950-01-01"),&lt;br /&gt;&lt;br /&gt;   Address = new Address&lt;br /&gt;   {&lt;br /&gt;     PostalCode = "12345",&lt;br /&gt;     State = "Florida",&lt;br /&gt;     City = "Jacksonville",&lt;br /&gt;     Street = "Some street"&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So with the ability to embed Lisp code into your regexes, you are only limited by your imagination and possibly your Lisp skills :)</description><pubDate>Mon, 14 Nov 2011 16:48:59 +0100</pubDate><a10:updated>2011-11-14T16:48:59+01:00</a10:updated></item><item><guid isPermaLink="false">310</guid><link>http://www.erikojebo.se/Post.aspx?id=310</link><author>webmaster@erikojebo.se</author><title>Search and Replace with Unprintable Characters in Emacs</title><description>Lately I've found myself doing a lot of search &amp; replace to restructure text. Quite often this involves changing whitespace in some way.&lt;br /&gt;&lt;br /&gt;For example, to include a newline character in the text to find, or in the replacement string, you simply enter a newline by using the chord C-q C-j.&lt;br /&gt;&lt;br /&gt;Here are a few useful whitespace chords:&lt;br /&gt;Tab: C-q C-i&lt;br /&gt;Linefeed: C-q C-j&lt;br /&gt;Carriage return: C-q C-m</description><pubDate>Fri, 10 Jun 2011 22:34:13 +0200</pubDate><a10:updated>2011-06-10T22:34:13+02:00</a10:updated></item><item><guid isPermaLink="false">309</guid><link>http://www.erikojebo.se/Post.aspx?id=309</link><author>webmaster@erikojebo.se</author><title>Keyboard Rectangle Selection in Visual Studio</title><description>Rectangle selection is one of the most time-saving tricks when editing large chunks of code, but the benefit is more or less lost if you have to reach for the mouse to do it.&lt;br /&gt;&lt;br /&gt;In Visual Studio you can hold down the alt button and use your mouse to select a rectangle of text. You can also use &lt;strong&gt;Control + Shift + arrow key&lt;/strong&gt; to do this with the keyboard. If those shortcuts do not work on your machine, go to &lt;em&gt;Tools-&gt;Options-&gt;Environment-&gt;Keyboard&lt;/em&gt; and add shortcuts for the following commands:&lt;br /&gt;&lt;br /&gt;Edit.LineDownExtendColumn&lt;br /&gt;Edit.LineUpExtendColumn&lt;br /&gt;Edit.CharLeftExtendColumn&lt;br /&gt;Edit.CharRightExtendColumn</description><pubDate>Thu, 19 May 2011 08:35:28 +0200</pubDate><a10:updated>2011-05-19T08:35:28+02:00</a10:updated></item><item><guid isPermaLink="false">303</guid><link>http://www.erikojebo.se/Post.aspx?id=303</link><author>webmaster@erikojebo.se</author><title>Cleaner Test Setup using Builders</title><description>One of the most common causes for messy test code is setup code that reduces&lt;br /&gt;the signal to noise ratio and makes you lose focus on the parts of the test&lt;br /&gt;code that actually are important. I've recently taken a liking to the builder&lt;br /&gt;pattern as a way to reduce this problem.&lt;br /&gt;&lt;br /&gt;In this post I'm going to compare a few different ways to write your setup&lt;br /&gt;code, to illustrate the pros and cons of the different styles.&lt;br /&gt;&lt;br /&gt;To start off, let's look at some classic object construction code:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var comment1 = new Comment();&lt;br /&gt;comment1.Body = "Comment 1 body";&lt;br /&gt;comment1.Date = new DateTime(2011, 1, 2, 3, 4, 5);&lt;br /&gt;&lt;br /&gt;var comment2 = new Comment();&lt;br /&gt;comment2.Body = "Comment 2 body";&lt;br /&gt;comment2.Date = new DateTime(2011, 1, 2, 3, 4, 5);&lt;br /&gt;&lt;br /&gt;var post = new Post();&lt;br /&gt;post.Title = "Title";&lt;br /&gt;post.Body = "Body";&lt;br /&gt;post.Date = new DateTime(2011, 1, 2, 3, 4, 5);&lt;br /&gt;post.AddComment(comment1);&lt;br /&gt;post.AddComment(comment2);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It doesn't get more basic than that, but there is a quite a lot of noise. The&lt;br /&gt;first step toward reducing that noise is to use the object initializer&lt;br /&gt;syntax:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var comment1 = new Comment&lt;br /&gt;    {&lt;br /&gt;        Body = "Comment 1 body",&lt;br /&gt;        Date = new DateTime(2011, 1, 2, 3, 4, 5)&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;var comment2 = new Comment&lt;br /&gt;    {&lt;br /&gt;        Body = "Comment 2 body",&lt;br /&gt;        Date = new DateTime(2011, 1, 2, 3, 4, 5)&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;var post = new Post&lt;br /&gt;    {&lt;br /&gt;        Title = "Title",&lt;br /&gt;        Body = "Body",&lt;br /&gt;        Date = new DateTime(2011, 1, 2, 3, 4, 5)&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;post.AddComment(comment1);&lt;br /&gt;post.AddComment(comment2);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I'd say that the this syntax makes the important information stand out a bit&lt;br /&gt;more. Another way is to use a constructor with named parameters and default&lt;br /&gt;values:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var comment1 = new Comment(&lt;br /&gt;    body: "comment 1 body",&lt;br /&gt;    date: new DateTime(2011, 1, 2, 3, 4, 5));&lt;br /&gt;&lt;br /&gt;var comment2 = new Comment(&lt;br /&gt;    body: "comment 2 body",&lt;br /&gt;    date: new DateTime(2011, 1, 2, 3, 4, 5));&lt;br /&gt;&lt;br /&gt;var post = new Post(&lt;br /&gt;    title: "Title",&lt;br /&gt;    body: "Body",&lt;br /&gt;    date: new DateTime(2011, 1, 2, 3, 4, 5));&lt;br /&gt;&lt;br /&gt;post.AddComment(comment1)&lt;br /&gt;post.AddComment(comment2);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This approach is a bit more compact than the object initializer way. Apart&lt;br /&gt;from the syntax, a major problem with both object initializers and&lt;br /&gt;constructors with named arguments is that they force you to modify the&lt;br /&gt;entities you want to create so that they work well with the test setup&lt;br /&gt;code. In the case above that is not a real problem, but it becomes a problem&lt;br /&gt;if you, for example, want to use certain default values when creating&lt;br /&gt;instances for your tests that you do not want to use in the production&lt;br /&gt;code. This is where the builder pattern comes in to the picture.&lt;br /&gt;&lt;br /&gt;A builder is a class whose sole purpose is to facilitate creation of instances&lt;br /&gt;of a specific class. In this case we would probably use a PostBuilder and a&lt;br /&gt;CommentBuilder. These classes can have all the helper methods you need so that&lt;br /&gt;you can easily get instances for your test cases.&lt;br /&gt;&lt;br /&gt;Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var post = new PostBuilder()&lt;br /&gt;    .WithTitle("Title")&lt;br /&gt;    .WithBody("Body")&lt;br /&gt;    .WithDate(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .WithComment(new CommentBuilder()&lt;br /&gt;        .WithBody("comment 1 body")&lt;br /&gt;        .WithDate(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;        .Build())&lt;br /&gt;    .WithComment(new CommentBuilder()&lt;br /&gt;        .WithBody("comment 2 body")&lt;br /&gt;        .WithDate(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;        .Build())&lt;br /&gt;    .Build();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This style of programming has been quite popular in the .NET space for the&lt;br /&gt;last two years or so. A fluent interface using daisy chaining of method calls&lt;br /&gt;and method names chosen to give a prose like reading experience. However, this&lt;br /&gt;style easily gets quite verbose and has fallen out of favor. The reason is&lt;br /&gt;simple, all those "With":s in the example above clutter up the code rather&lt;br /&gt;than makeing it easier to read. A slightly more compact version could look&lt;br /&gt;something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var post = new PostBuilder()&lt;br /&gt;    .Title("Title")&lt;br /&gt;    .Body("Body")&lt;br /&gt;    .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Comment(new CommentBuilder()&lt;br /&gt;        .Body("comment 1 body")&lt;br /&gt;        .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;        .Build())&lt;br /&gt;    .Comment(new CommentBuilder()&lt;br /&gt;        .Body("comment 2 body")&lt;br /&gt;        .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;        .Build())&lt;br /&gt;    .Build();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now we're getting somewhere. There is less noise, but there are still a couple&lt;br /&gt;of builder instantiations scattered around the code. The syntax could be&lt;br /&gt;cleaned up a bit by introducing a nicer way to create the builders. Below is&lt;br /&gt;an example with a static class which has properties for the different kinds of&lt;br /&gt;builders. The factory class is called Build to make the code read a little&lt;br /&gt;nicer.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var post = Build.Post&lt;br /&gt;    .Title("Title")&lt;br /&gt;    .Body("Body")&lt;br /&gt;    .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Comment(Build.Comment&lt;br /&gt;        .Body("comment 1 body")&lt;br /&gt;        .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;        .Build())&lt;br /&gt;    .Comment(Build.Comment&lt;br /&gt;        .Body("comment 2 body")&lt;br /&gt;        .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;        .Build())&lt;br /&gt;    .Build();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Better. Two problems remaining are the duplication of the word Comment in&lt;br /&gt;the call to the Comment method, and that annoying call to Build for the&lt;br /&gt;comments. These problems could be addressed by creating a version of the&lt;br /&gt;Comment method that takes a lambda operating on a builder as an argument:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var post = Build.Post&lt;br /&gt;    .Title("Title")&lt;br /&gt;    .Body("Body")&lt;br /&gt;    .Date(new DateTime(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Comment(c =&gt; c&lt;br /&gt;        .Body("comment 1 body")&lt;br /&gt;        .Date(new DateTime(2011, 1, 2, 3, 4, 5)))&lt;br /&gt;    .Comment(c =&gt; c&lt;br /&gt;        .Body("comment 2 body")&lt;br /&gt;        .Date(new DateTime(2011, 1, 2, 3, 4, 5)))&lt;br /&gt;    .Build();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I'd say this is even better. The only noise remaining is the duplication of&lt;br /&gt;Date/DateTime when setting the date for a comment or a post and the "c =&gt; c" part&lt;br /&gt;of the lambda. The implementation of the PostBuilder class now looks like&lt;br /&gt;this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class PostBuilder&lt;br /&gt;{&lt;br /&gt;    private readonly Post _post = new Post();&lt;br /&gt;    &lt;br /&gt;    public PostBuilder Title(string title)&lt;br /&gt;    {&lt;br /&gt;        _post.Title = title;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public PostBuilder Body(string body)&lt;br /&gt;    {&lt;br /&gt;        _post.Body = body;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public PostBuilder Date(DateTime date)&lt;br /&gt;    {&lt;br /&gt;        _post.Date = date;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public Post Build()&lt;br /&gt;    {&lt;br /&gt;        return _post;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public PostBuilder Comment(Action&lt;CommentBuilder&gt; initializer)&lt;br /&gt;    {&lt;br /&gt;        var builder = new CommentBuilder();&lt;br /&gt;        initializer(builder);&lt;br /&gt;        &lt;br /&gt;        var comment = builder.Build();&lt;br /&gt;        _post.AddComment(comment);&lt;br /&gt;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I usually find myself using the same DateTime constructor, over and over&lt;br /&gt;again. This cries out for refactoring. Now we can reap the benefits of using a&lt;br /&gt;builder class, since we easily can add any helpers we need. In this case by&lt;br /&gt;allowing the date to be set using the standard six double values for year,&lt;br /&gt;month, day, hour, minute and second:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var post = Build.Post&lt;br /&gt;    .Title("Title")&lt;br /&gt;    .Body("Body")&lt;br /&gt;    .Date(2011, 1, 2, 3, 4, 5)&lt;br /&gt;    .Comment(c =&gt; c&lt;br /&gt;        .Body("comment 1 body")&lt;br /&gt;        .Date(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Comment(c =&gt; c&lt;br /&gt;        .Body("comment 2 body")&lt;br /&gt;        .Date(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Build();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Much better! The builder now contains the following method:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public PostBuilder Date(&lt;br /&gt;    int year, int month, int day, &lt;br /&gt;    int hour, int minute, int second)&lt;br /&gt;{&lt;br /&gt;    _post.Date = new DateTime(year, month, day, hour, minute, second);&lt;br /&gt;    return this;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Ok, so now the setup code looks nice and tight, but the builder class contains&lt;br /&gt;a nasty form of duplication. For each property that is to be exposed through&lt;br /&gt;the builder there is a matching method:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public PostBuilder Title(string title)&lt;br /&gt;{&lt;br /&gt;    _post.Title = title;&lt;br /&gt;    return this;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public PostBuilder Body(string body)&lt;br /&gt;{&lt;br /&gt;    _post.Body = body;&lt;br /&gt;    return this;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This code is extremely tedious to write, especially if you have a large&lt;br /&gt;application with a lot of entities. LISP eats this kind of duplication for&lt;br /&gt;breakfast, as do Ruby, but it is often quite hard to remove in statically&lt;br /&gt;typed languages which have no pre-processor or macro facilities.&lt;br /&gt;&lt;br /&gt;Fortunately, C# 4 includes the dynamic keyword which opens up new&lt;br /&gt;possibilities for the static folks. All the dumb builder methods which set the&lt;br /&gt;property with the same name as the method on the entity could easily be&lt;br /&gt;replaced with a method missing hook:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class DynamicBuilder&lt;T&gt; : DynamicObject &lt;br /&gt;    where T : class, new()&lt;br /&gt;{&lt;br /&gt;    protected readonly T Entity = new T();&lt;br /&gt;&lt;br /&gt;    // This method is called when you invoke a method that does not exist&lt;br /&gt;    public override bool TryInvokeMember(&lt;br /&gt;        InvokeMemberBinder binder, object[] args, out object result)&lt;br /&gt;    {&lt;br /&gt;        // Remember to return self to enable daisy chaining&lt;br /&gt;        result = this;&lt;br /&gt;&lt;br /&gt;        // Get the property on the entity that has the same name&lt;br /&gt;        // as the method that was invoked&lt;br /&gt;        var property = typeof(T).GetProperty(binder.Name);&lt;br /&gt;&lt;br /&gt;        var propertyExists = property != null;&lt;br /&gt;&lt;br /&gt;        if (propertyExists)&lt;br /&gt;        {&lt;br /&gt;            property.SetValue(Entity, args[0], null);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return propertyExists;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public T Build()&lt;br /&gt;    {&lt;br /&gt;        return Entity;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Sweet! Now you can throw away most of your boring builder code, except for the&lt;br /&gt;helpers that are tailor made for the specific entity type that your are building.&lt;br /&gt;&lt;br /&gt;The post builder now looks like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class DynamicPostBuilder : DynamicBuilder&lt;Post&gt;&lt;br /&gt;{&lt;br /&gt;    public DynamicPostBuilder Date(&lt;br /&gt;        int year, int month, int day,&lt;br /&gt;        int hour, int minute, int second)&lt;br /&gt;    {&lt;br /&gt;        Entity.Date = new DateTime(year, month, day, hour, minute, second);&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public DynamicPostBuilder Comment(Action&lt;dynamic&gt; initializer)&lt;br /&gt;    {&lt;br /&gt;        var builder = new DynamicCommentBuilder();&lt;br /&gt;            &lt;br /&gt;        initializer(builder);&lt;br /&gt;            &lt;br /&gt;        var comment = builder.Build();&lt;br /&gt;        Entity.AddComment(comment);&lt;br /&gt;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The only downside to this is that you lose refactoring support and&lt;br /&gt;intellisense, which can be a big deal for many .NET developers. However, if&lt;br /&gt;you use TDD, the refactoring support should not be an issue, since you will&lt;br /&gt;instantly know what was broken when something is renamed.&lt;br /&gt;&lt;br /&gt;The code for adding a comment looks suspiciously like a bit of code that might&lt;br /&gt;get repeated in other builders. So there is another chance to, for example,&lt;br /&gt;introduce a convention that would allow that code to be pushed down and&lt;br /&gt;handled in the method missing hook of the base class. Only inconsistency and&lt;br /&gt;lack of imagination set the limits in this case.&lt;br /&gt;&lt;br /&gt;To use the builder you have to make sure that the builder instance is typed as&lt;br /&gt;dynamic, so that the compiler will get out of your way and allow you to call&lt;br /&gt;the methods you want to call, even though they are not actually declared in&lt;br /&gt;the builder class.&lt;br /&gt;&lt;br /&gt;In this case, that can be accomplished by modifying the builder factory class:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class DynamicBuild&lt;br /&gt;{&lt;br /&gt;    public static dynamic Post&lt;br /&gt;    {&lt;br /&gt;        get { return new DynamicPostBuilder(); }&lt;br /&gt;    }&lt;br /&gt;        &lt;br /&gt;    public static dynamic Comment&lt;br /&gt;    {&lt;br /&gt;        get { return new DynamicCommentBuilder(); }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, to sum up. Using the builder pattern allows you to clean up your test code&lt;br /&gt;significantly and makes it trivial to add helpers when needed.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Original setup code:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var comment1 = new Comment();&lt;br /&gt;comment1.Body = "Comment 1 body";&lt;br /&gt;comment1.Date = new DateTime(2011, 1, 2, 3, 4, 5);&lt;br /&gt;&lt;br /&gt;var comment2 = new Comment();&lt;br /&gt;comment2.Body = "Comment 2 body";&lt;br /&gt;comment2.Date = new DateTime(2011, 1, 2, 3, 4, 5);&lt;br /&gt;&lt;br /&gt;var post = new Post();&lt;br /&gt;post.Title = "Title";&lt;br /&gt;post.Body = "Body";&lt;br /&gt;post.Date = new DateTime(2011, 1, 2, 3, 4, 5);&lt;br /&gt;post.AddComment(comment1);&lt;br /&gt;post.AddComment(comment2);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Builder based setup code:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var post = Build.Post&lt;br /&gt;    .Title("Title")&lt;br /&gt;    .Body("Body")&lt;br /&gt;    .Date(2011, 1, 2, 3, 4, 5)&lt;br /&gt;    .Comment(c =&gt; c&lt;br /&gt;        .Body("comment 1 body")&lt;br /&gt;        .Date(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Comment(c =&gt; c&lt;br /&gt;        .Body("comment 2 body")&lt;br /&gt;        .Date(2011, 1, 2, 3, 4, 5))&lt;br /&gt;    .Build();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Happy building!&lt;br /&gt;</description><pubDate>Thu, 03 Feb 2011 20:35:05 +0100</pubDate><a10:updated>2011-02-03T20:35:05+01:00</a10:updated></item><item><guid isPermaLink="false">302</guid><link>http://www.erikojebo.se/Post.aspx?id=302</link><author>webmaster@erikojebo.se</author><title>Quick Tip: Finding apt-get Install Location</title><description>To find where apt-get installed a given package just run the following command:&lt;br /&gt;&lt;pre class="prettyprint"&gt;sudo dpkg -L packagename&lt;/pre&gt;</description><pubDate>Mon, 24 Jan 2011 21:00:49 +0100</pubDate><a10:updated>2011-01-24T21:00:49+01:00</a10:updated></item><item><guid isPermaLink="false">295</guid><link>http://www.erikojebo.se/Post.aspx?id=295</link><author>webmaster@erikojebo.se</author><title>Git Commit Script for the Lazy Typist</title><description>If you are a git user, you are probably a fan of the command line, and you probably use aliases to make your daily work more efficient. One of my most frequently used aliases is &lt;em&gt;gc&lt;/em&gt;, which is an alias for &lt;em&gt;git commit -m&lt;/em&gt;.&lt;br /&gt;&lt;pre class="prettyprint"&gt;alias gc='git commit -m'&lt;/pre&gt;&lt;br /&gt;However, one shortcoming of this alias is that you still have to add the double quotes, which is tedious. &lt;br /&gt;&lt;pre class="prettyprint"&gt;gc "this is a commit message"&lt;/pre&gt;&lt;br /&gt;There are several ways of solving that problem, and I chose to do it by writing a small ruby script:&lt;br /&gt;&lt;pre class="prettyprint"&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;# Performs a git commit with all the command line arguments as a message, &lt;br /&gt;# for example:&lt;br /&gt;# gcm this is a commit message =&gt; git commit -m "this is a commit message"&lt;br /&gt;&lt;br /&gt;message = ARGV.join(" ")&lt;br /&gt;%x[git commit -m '#{message}']&lt;/pre&gt;&lt;br /&gt;By saving this script with a suitable name in some directory that is part of your path you can now commit with messages without quotes.&lt;br /&gt;&lt;br /&gt;The commit shown above now becomes:&lt;br /&gt;&lt;pre class="prettyprint"&gt;gc this is a commit message&lt;/pre&gt;&lt;br /&gt;You can find this script and more &lt;a href='https://github.com/erikojebo/configuration'&gt;up at github&lt;/a&gt; where I keep my configuration, such as bash scripts and dot files.</description><pubDate>Sat, 04 Dec 2010 16:59:42 +0100</pubDate><a10:updated>2010-12-04T16:59:42+01:00</a10:updated></item><item><guid isPermaLink="false">294</guid><link>http://www.erikojebo.se/Post.aspx?id=294</link><author>webmaster@erikojebo.se</author><title>Git-removing Files Marked as Deleted</title><description>If you work in an environment where not all file operations are made through git you probably have been in the situation that files have been deleted by using just plain rm rather than git rm, so you have to manually do a git rm for each deleted file and then commit the deletion.&lt;br /&gt;&lt;br /&gt;To make this step a little bit easier you can use the following command:&lt;br /&gt;&lt;pre class="prettyprint"&gt;git rm $(git ls-files -d)&lt;/pre&gt;&lt;br /&gt;It might be a good idea to either alias this command or put it in a shell script somewhere in your path, to make it easier to use.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Edit:&lt;/strong&gt;&lt;br /&gt;There is actually a much better way to solve the problem where the deletions are not added, which is to add the --all flag to the git add:&lt;br /&gt;&lt;pre class="prettyprint"&gt;git add . --all&lt;/pre&gt;</description><pubDate>Tue, 23 Nov 2010 08:46:01 +0100</pubDate><a10:updated>2010-11-23T08:46:01+01:00</a10:updated></item><item><guid isPermaLink="false">293</guid><link>http://www.erikojebo.se/Post.aspx?id=293</link><author>webmaster@erikojebo.se</author><title>Make Alt-Tab Useful by Disabling Aero Peek in Windows 7</title><description>The default behaviour of Alt-tab in Windows 7 is simply horrible to use. Fortunately you can easily disable Aero Peek, as it is called, to go back to a more traditional style of alt-tabbing.&lt;br /&gt;&lt;br /&gt;Go to "Adjust the appearance and performance of windows", by typing it in in the start menu search, and then simply uncheck "Aero peek".</description><pubDate>Mon, 22 Nov 2010 10:17:53 +0100</pubDate><a10:updated>2010-11-22T10:17:53+01:00</a10:updated></item><item><guid isPermaLink="false">288</guid><link>http://www.erikojebo.se/Post.aspx?id=288</link><author>webmaster@erikojebo.se</author><title>Quick Tip: Useful Shortcuts for your Everyday Windows Usage</title><description>&lt;strong&gt;Windows in general:&lt;/strong&gt;&lt;br /&gt;Windows + E: Open Windows explorer&lt;br /&gt;Windows + R: Open Run dialog&lt;br /&gt;Windows + D: Minimize/restore all (show desktop)&lt;br /&gt;Ctrl + Shift + Esc: Open process explorer&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Explorer:&lt;/strong&gt;&lt;br /&gt;F10 or Alt: Show menu bar&lt;br /&gt;F4: Move focus to adress bar&lt;br /&gt;F6: Move focus to next pane&lt;br /&gt;Alt + Space: Open the system menu for the windod (minimize, maximize, etc)&lt;br /&gt;Ctrl + e or F3: Move focus to search bar&lt;br /&gt;Shift + F10: Open right click menu for current item&lt;br /&gt;Ctrl + w: Close the current window&lt;br /&gt;F11: Full screen mode&lt;br /&gt;Shift + F11: Exit full screen mode&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Windows 7:&lt;/strong&gt;&lt;br /&gt;Windows + 1 to 9: Pinned applications&lt;br /&gt;Windows + Left: Dock current window left&lt;br /&gt;Windows + Right: Dock current window right&lt;br /&gt;Windows + Up: Maximize current window&lt;br /&gt;Windows + Down: Restore current window&lt;br /&gt;Windows + Shift + Left: Move to screen to the left&lt;br /&gt;Windows + Shift + Right: Move to screen to the right</description><pubDate>Thu, 04 Nov 2010 09:33:03 +0100</pubDate><a10:updated>2010-11-04T09:33:03+01:00</a10:updated></item><item><guid isPermaLink="false">287</guid><link>http://www.erikojebo.se/Post.aspx?id=287</link><author>webmaster@erikojebo.se</author><title>Assertion Classes: Reuse and Expressiveness instead of Test Class Inheritance Hierarchies</title><description>If you have several classes which have some aspect in common, you probably have test classes that have similar tests as well. These similarities often lead to duplication that needs to be removed. One way to remove this duplication is to extract a common super class for the test classes, but inheritance can be a quite inflexible way of sharing behaviour between classes. For example, what happens when you have a test class that share behaviour with not only one other class, but two?&lt;br /&gt;&lt;br /&gt;Due to the limitations of inheritance, composition is often a better choice. So, how can you use composition to share functionality between test classes? Let's look at an example from the world of Windows Presentation Foundation and MVVM.&lt;br /&gt;&lt;br /&gt;Here is a typical view model class, which implements the interface INotifyPropertyChanged. If you are using MVVM you probably have quite a lot of classes implementing this interface.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class ViewModel : INotifyPropertyChanged&lt;br /&gt;{&lt;br /&gt;    private string _someProperty;&lt;br /&gt;&lt;br /&gt;    public string SomeProperty&lt;br /&gt;    {&lt;br /&gt;        get { return _someProperty; }&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            _someProperty = value;&lt;br /&gt;            RaisePropertyChanged("SomeProperty");&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void RaisePropertyChanged(string propertyName)&lt;br /&gt;    {&lt;br /&gt;        if (PropertyChanged != null)&lt;br /&gt;        {&lt;br /&gt;            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public event PropertyChangedEventHandler PropertyChanged;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here is a simple test class, with a single test case that verifies that the property in the view model actually raises the PropertyChanged event when changed.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[TestFixture]&lt;br /&gt;public class ViewModelTests&lt;br /&gt;{&lt;br /&gt;    private ViewModel _viewModel;&lt;br /&gt;    private string _actualPropertyName;&lt;br /&gt;    private int _eventsRaised;&lt;br /&gt;&lt;br /&gt;    [SetUp]&lt;br /&gt;    public void SetUp()&lt;br /&gt;    {&lt;br /&gt;        _viewModel = new ViewModel();&lt;br /&gt;        _eventsRaised = 0;&lt;br /&gt;        _actualPropertyname = "";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Test]&lt;br /&gt;    public void Setting_SomeProperty_raises_property_changed_event()&lt;br /&gt;    {&lt;br /&gt;        StartListeningForPropertyChangedEvent();&lt;br /&gt;&lt;br /&gt;        _viewModel.SomeProperty = "new value";&lt;br /&gt;&lt;br /&gt;        AssertSinglePropertyChangedEventWasFiredFor("SomeProperty");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void StartListeningForPropertyChangedEvent()&lt;br /&gt;    {&lt;br /&gt;        _viewModel.PropertyChanged += (s, e) =&gt;&lt;br /&gt;            {&lt;br /&gt;                _eventsRaised++;&lt;br /&gt;                _actualPropertyName = e.PropertyName;&lt;br /&gt;            };&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void AssertSinglePropertyChangedEventWasFiredFor(string expectedPropertyName)&lt;br /&gt;    {&lt;br /&gt;        Assert.AreEqual(1, _eventsRaised, "number of property changed events raised");&lt;br /&gt;        Assert.AreEqual(expectedPropertyName, _actualPropertyName);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now imagine that you write another view model class. You will probably need to write test cases for that class that are almost identical to the test case above. You could extract the helper methods to a super class, but as mentioned above, that is not an ideal solution.&lt;br /&gt;&lt;br /&gt;So, now to the good stuff. A simple way of cleaning up the test class above is to extract another class which handles everything that has to do with asserting that a property raises the property changed event. &lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class ExpectPropertyChanged&lt;br /&gt;{&lt;br /&gt;    private string _actualPropertyName;&lt;br /&gt;    private int _eventsRaised;&lt;br /&gt;    private string _expectedPropertyName;&lt;br /&gt;&lt;br /&gt;    private ExpectPropertyChanged(INotifyPropertyChanged sender)&lt;br /&gt;    {&lt;br /&gt;        sender.PropertyChanged += (s, e) =&gt;&lt;br /&gt;            {&lt;br /&gt;                _eventsRaised++;&lt;br /&gt;                _actualPropertyName = e.PropertyName;&lt;br /&gt;            };&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static ExpectPropertyChanged On(INotifyPropertyChanged sender)&lt;br /&gt;    {&lt;br /&gt;        return new ExpectPropertyChanged(sender);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public ExpectPropertyChanged ForProperty(string expectedPropertyName)&lt;br /&gt;    {&lt;br /&gt;        _expectedPropertyName = expectedPropertyName;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void During(Action action)&lt;br /&gt;    {&lt;br /&gt;        action.Invoke();&lt;br /&gt;&lt;br /&gt;        Assert.AreEqual(1, _eventsRaised, "number of property changed events raised");&lt;br /&gt;        Assert.AreEqual(_expectedPropertyName, _actualPropertyName);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This removes the need for the helper methods in the test class and enables you to use the same assertion code in any other test class without having to tie the classes together via a static type relationship. Here is the test class after the refactoring:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[TestFixture]&lt;br /&gt;public class ViewModelTests&lt;br /&gt;{&lt;br /&gt;    private ViewModel _viewModel;&lt;br /&gt;&lt;br /&gt;    [SetUp]&lt;br /&gt;    public void SetUp()&lt;br /&gt;    {&lt;br /&gt;        _viewModel = new ViewModel();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Test]&lt;br /&gt;    public void Setting_SomeProperty_raises_property_changed_event()&lt;br /&gt;    {&lt;br /&gt;        ExpectPropertyChanged&lt;br /&gt;            .On(_viewModel)&lt;br /&gt;            .ForProperty("SomeProperty")&lt;br /&gt;            .During(() =&gt; _viewModel.SomeProperty = "new value");&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, why would you bother with this stuff? First of all, it let's you reuse code much easier than through inheritance. Apart from that it also simplifies your actual test classes since you can extract lots of boring helper methods into their own classes, which improves the signal to noise ratio significantly. Finally it also gives you a new level of expressiveness by naming the assertion class and introducing methods on the assertions which explain the meaning of each value used. This last point also solves the problem with composite assertions that take several parameters. By introducing an assertion class you can set each value using a method with a self documenting name, which makes it obvious what the value means, instead of having to try your best to guess what the third integer parameter does.&lt;br /&gt;</description><pubDate>Thu, 28 Oct 2010 22:38:32 +0200</pubDate><a10:updated>2010-10-28T22:38:32+02:00</a10:updated></item><item><guid isPermaLink="false">286</guid><link>http://www.erikojebo.se/Post.aspx?id=286</link><author>webmaster@erikojebo.se</author><title>Colorizing Your Git Output</title><description>If the output from your git commands is not colorized, it is time to bring out the old git config.&lt;br /&gt;&lt;br /&gt;Either you configure git with the git config commands from a shell:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;git config --global color.branch auto&lt;br /&gt;git config --global color.diff auto&lt;br /&gt;git config --global color.interactive auto&lt;br /&gt;git config --global color.status auto&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;or you open up ~/.gitconfig in your favourite text editor and add the following section:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[color]&lt;br /&gt;    branch = auto&lt;br /&gt;    diff = auto&lt;br /&gt;    interactive = auto&lt;br /&gt;    status = auto&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Given that you have a decent shell you can now enjoy the full glory of your git output.</description><pubDate>Thu, 28 Oct 2010 20:46:45 +0200</pubDate><a10:updated>2010-10-28T20:46:45+02:00</a10:updated></item><item><guid isPermaLink="false">285</guid><link>http://www.erikojebo.se/Post.aspx?id=285</link><author>webmaster@erikojebo.se</author><title>Setting the Default Startup Directory for your Cygwin Bash</title><description>To change the path in which your bash shell is started you can edit your .bashrc file.&lt;br /&gt;To do this, open cygwin and simply open the file &lt;em&gt;~/.bashrc&lt;/em&gt; in your favourite text editor and add a cd command at the bottom.</description><pubDate>Fri, 15 Oct 2010 08:13:20 +0200</pubDate><a10:updated>2010-10-15T08:13:20+02:00</a10:updated></item><item><guid isPermaLink="false">284</guid><link>http://www.erikojebo.se/Post.aspx?id=284</link><author>webmaster@erikojebo.se</author><title>Getting Emacs to Behave under Cygwin</title><description>If you install emacs under cygwin, there is a risk that you won't be able to close emacs with the usual C-x C-c, since it is mapped to C-x C-g for some reason. To fix this, add an environment variable called &lt;em&gt;CYGWIN&lt;/em&gt; with the value &lt;em&gt;binmode ntsec tty &lt;/em&gt;.</description><pubDate>Sat, 02 Oct 2010 17:53:48 +0200</pubDate><a10:updated>2010-10-02T17:53:48+02:00</a10:updated></item><item><guid isPermaLink="false">281</guid><link>http://www.erikojebo.se/Post.aspx?id=281</link><author>webmaster@erikojebo.se</author><title>Set Up Shared and Local Git Repository</title><description>In my current hobby project I'm using git with a shared repository on an external hard drive, and a local repository on my primary disk.&lt;br /&gt;&lt;br /&gt;To set up an empty shared repository, use the following command:&lt;br /&gt;&lt;pre class="prettyprint"&gt;git init --bare --shared&lt;/pre&gt;This tells git to create a shared repository that is not supposed to have a working copy of the code, just to be pushed to and pulled from.&lt;br /&gt;&lt;br /&gt;You then clone the shared repository on your primary disk by executing the git clone command:&lt;br /&gt;&lt;pre class="prettyprint"&gt;git clone /path/to/shared/repository&lt;/pre&gt;You will get a warning about that you have cloned an empty repository, but that isn't a problem.&lt;br /&gt;&lt;br /&gt;Now you are all set to add your first file and do your first commit in the new repostiory. For example:&lt;br /&gt;&lt;pre class="prettyprint"&gt;touch todo.txt&lt;br /&gt;git add .&lt;br /&gt;git commit -m 'Added todo list'&lt;/pre&gt;You now have something that you can push to the shared repository. If you try to push your commits to the shared repository using &lt;pre class="prettyprint"&gt;git push origin&lt;/pre&gt; you will get an error message saying &lt;em&gt;No refs in common and none specified; doing nothing; Perhaps you should specify a branch such as 'master'&lt;/em&gt;. This is because of that the remote repository is completely empty, and has no branches. To push your commits you have to explicitly specify the name of the branch, i.e. &lt;em&gt;master&lt;/em&gt;. &lt;pre class="prettyprint"&gt;git push origin master&lt;/pre&gt;&lt;br /&gt;Now you have pushed your first commits to the remote repository and you can now push and pull like nobody's business.&lt;br /&gt;</description><pubDate>Thu, 09 Sep 2010 20:15:55 +0200</pubDate><a10:updated>2010-09-09T20:15:55+02:00</a10:updated></item><item><guid isPermaLink="false">280</guid><link>http://www.erikojebo.se/Post.aspx?id=280</link><author>webmaster@erikojebo.se</author><title>Use ReSharper to Quickly Generate GUIDs</title><description>Everyone who has used the GUID generation tool in Visual Studio knows that it is quite limited in the GUID formats it can generate. If you are a ReSharper user there is a much more enjoyable way to generate GUIDs. Anywhere in your code or in an XML file, type &lt;strong&gt;nguid&lt;/strong&gt; and press tab to expand the snippet. That will generate a new GUID and let you choose between a number of formats.</description><pubDate>Wed, 08 Sep 2010 21:31:49 +0200</pubDate><a10:updated>2010-09-08T21:31:49+02:00</a10:updated></item><item><guid isPermaLink="false">278</guid><link>http://www.erikojebo.se/Post.aspx?id=278</link><author>webmaster@erikojebo.se</author><title>Getting Current System Uptime under Windows</title><description>If you are trying to remember when you came to work, one trick is to check your system uptime:&lt;br /&gt;&lt;br /&gt;In a command prompt:&lt;br /&gt;- Run the command "net statistics server" or "net stats srv"&lt;br /&gt;- Check the first line of the output, which says "Statistics since …" which shows when you booted</description><pubDate>Mon, 06 Sep 2010 09:29:04 +0200</pubDate><a10:updated>2010-09-06T09:29:04+02:00</a10:updated></item><item><guid isPermaLink="false">274</guid><link>http://www.erikojebo.se/Post.aspx?id=274</link><author>webmaster@erikojebo.se</author><title>Removing Regions in Visual Studio</title><description>Some people are truly in love with the whole #region thing in Visual Studio. Personally I can't say I share that sentiment. Because of this I found myself wanting to quickly remove all regions in a solution. The first solution that came to mind was our old friend the regular expression.&lt;br /&gt;&lt;br /&gt;To remove all regions, open the Find &amp; replace dialog (Ctrl-H), choose the appropriate scope (current document, entire solution etc) and click the "Use regular expressions" checkbox. Enter the following regular expression in the Find field:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;\#(end)*region.*$&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Make sure the Replace field is empty, and hit the Replace All button. Tada!</description><pubDate>Fri, 27 Aug 2010 15:09:41 +0200</pubDate><a10:updated>2010-08-27T15:09:41+02:00</a10:updated></item><item><guid isPermaLink="false">256</guid><link>http://www.erikojebo.se/Post.aspx?id=256</link><author>webmaster@erikojebo.se</author><title>Search &amp; Replace with Visual Studio Regular Expressions</title><description>Regular expressions are an awesome tool, but unfortunately every language/environment has its own dialect, including Visual Studio.&lt;br /&gt;&lt;br /&gt;Visual Studio uses {} to indicate a group to capture. A captured group can later be referenced by using backslash and the match number (one based).&lt;br /&gt;&lt;br /&gt;As an example, let's say you want to do the following replacement:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;// Original&lt;br /&gt;var person = new Person("Frank");&lt;br /&gt;&lt;br /&gt;// Expected result&lt;br /&gt;var person = new Person { Name = "Frank" };&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To achieve this you can use the following search and replace patterns in Visual Studio:&lt;br /&gt;&lt;br /&gt;Search pattern: &lt;strong&gt;var person = new Person\({:q}\);&lt;/strong&gt;&lt;br /&gt;Replace pattern: &lt;strong&gt;var person = new Person { Name = \1 };&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;:q is a special Visual Studio regex symbol for matching a quoted string. Another custom VS symbol is :z which matches an integer.</description><pubDate>Thu, 24 Jun 2010 10:27:46 +0200</pubDate><a10:updated>2010-06-24T10:27:46+02:00</a10:updated></item><item><guid isPermaLink="false">247</guid><link>http://www.erikojebo.se/Post.aspx?id=247</link><author>webmaster@erikojebo.se</author><title>Getting Started with FitNesse</title><description>If you haven't heard of FitNesse, it is Unclebob's acceptance testing framework which let's you specify test cases as wiki pages. These test cases then interact with fixture classes that call the system under test.&lt;br /&gt;&lt;br /&gt;The first step in getting started with FitNesse is to download it. You only need one file, FitNesse.jar, which can be found at &lt;a href='http://fitnesse.org/FrontPage.FitNesseDevelopment.DownLoad'&gt;the FitNesse wiki&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Download fitnesse.jar and put it in your project's lib directory. For the purpose of this blog post we will assume a directory structure that looks like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;c:- code&lt;br /&gt;  - hellofitnesse&lt;br /&gt;    - bin&lt;br /&gt;    - fitnesse&lt;br /&gt;    - lib&lt;br /&gt;    - src&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So, now the lib folder contains fitnesse.jar.&lt;br /&gt;&lt;br /&gt;Since FitNesse is based on a wiki, it runs on its own web server. To start fitnesse go to the hellofitnesse directory and execute the following command:&lt;br /&gt;&lt;pre class="prettyprint"&gt;java -jar lib/fitnesse.jar -p 8080 -r fitnesse&lt;/pre&gt;&lt;br /&gt;This command starts fitnesse and tells it to listen to the port 8080. It also tells it to unpack all the files that it needs in the folder fitnesse.&lt;br /&gt;&lt;br /&gt;You can now open up a browser and go to localhost:8080. You should then see the default FitNesse wiki.&lt;br /&gt;&lt;br /&gt;Edit the front page by clicking the edit link in the menu to the left and add the following line:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;!define TEST_SYSTEM {slim}&lt;br /&gt;&lt;br /&gt;!path lib/fitnesse.jar&lt;br /&gt;!path bin&lt;br /&gt;&lt;br /&gt;&gt;HelloFitnesse&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The first line tells FitNesse to use slim to run the tests. The path statements tell FitNesse where to find fitnesse.jar and the .class files for your fixtures (change this path if your .class files will be generated into some other directory).&lt;br /&gt;&lt;br /&gt;This last line creates a link to a child page called HelloFitnesse. When you save the page, a little question mark will appear after HelloFittnesse. Click the question mark to create the page.&lt;br /&gt;&lt;br /&gt;This page is now just an ordinary wiki page. To make the page into a test suite page click the properties link in the menu, and select Suite.&lt;br /&gt;&lt;br /&gt;Edit the HelloFitnesse page and add the following row:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&gt;MyFirstTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Create the page and change its properties to make it into a test page.&lt;br /&gt;&lt;br /&gt;Edit the MyFirstTest page and add the following code to create a simple test:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;|import  |&lt;br /&gt;|fixtures|&lt;br /&gt; &lt;br /&gt;!|AddFixture           |&lt;br /&gt;|a     |b     |sum?    |&lt;br /&gt;|1     |2     |3       |&lt;br /&gt;|2     |2     |3       |&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The first two lines defines a table with all the packages to import. The second table defines the actual test. The first row specifies the name of the fixture class to run. The columns a and b represent state that should be set in the fixture, and the column sum? represents a method call which returns the result that should be verified.&lt;br /&gt;&lt;br /&gt;The values a and b are set by calling the methods setA and setB with the corresponding values. When both a and b are set the method sum() will be called and the return value will be compared to the expected value.&lt;br /&gt;&lt;br /&gt;You can now run the test by clicking the Test link in the menu. However, since the actual test fixture has not been written yet you will get an exception that looks something like this:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Could not invoke constructor for AddFixture[0]&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;So, the next step is to write the fixture class.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;package fixtures;&lt;br /&gt;&lt;br /&gt;public class AddFixture {&lt;br /&gt;    private int _a;&lt;br /&gt;    private int _b;&lt;br /&gt;&lt;br /&gt;    public void setA(int a) {&lt;br /&gt;        _a = a;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void setB(int b) {&lt;br /&gt;        _b = b;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public int sum() {&lt;br /&gt;        return _a + _b;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Compile the fixture class and make sure the AddFixture.class file is actually located in bin/fixtures/ folder (if bin is your output folder, specified in the !path directive earlier). You should now be able to run the test and get the proper response, one row should succeed and one should fail.</description><pubDate>Fri, 16 Apr 2010 22:05:54 +0200</pubDate><a10:updated>2010-04-16T22:05:54+02:00</a10:updated></item><item><guid isPermaLink="false">240</guid><link>http://www.erikojebo.se/Post.aspx?id=240</link><author>webmaster@erikojebo.se</author><title>Fixing Default Indentation in Resharper</title><description>The default indentation rules for Resharper are quite ugly. Both object/collection initializers and anonymous methods are indented way to much. Fortunately this can be fixed.&lt;br /&gt;&lt;br /&gt;To fix the indentation of anonymous methods, go to Resharper-&gt;Options-&gt;Braces Layout and choose "BSD style" for "Anonymous method declaration". Then go to Other and deselect the option "Indent anonymous method body".&lt;br /&gt;&lt;br /&gt;Fixing the object/collection initializer indentation is quite similar. Go to Resharper-&gt;Options-&gt;Braces Layout and choose "BSD style" for "Array oand object initializer". Then go to Other and set "Continous line indent multiplier" to 1.</description><pubDate>Wed, 31 Mar 2010 09:49:21 +0200</pubDate><a10:updated>2010-03-31T09:49:21+02:00</a10:updated></item><item><guid isPermaLink="false">239</guid><link>http://www.erikojebo.se/Post.aspx?id=239</link><author>webmaster@erikojebo.se</author><title>The Importance of Nomenclature</title><description>Naming is a crucial part of programming, and is probably one of the most challenging parts of the craft. The Behaviour Driven Development movement currently does its best to bring attention to the fact that terminology and naming not only effects the way we read and understand code, it also effects the way approach problems and thereby also the solutions that we come up with.&lt;br /&gt;&lt;br /&gt;An example of how terminology influences the code you write is the naming of the attribute which specifies that a method is a test method. NUnit uses the traditional jUnit style by using the [Test] attribute. However, xUnit.NET has chosen to call their attribute  [Fact]. This simple change is quite effective in pointing you in the right direction when you sit down to write your unit test.&lt;br /&gt;&lt;br /&gt;The [Test] attribute is quite weak since it does not say much, except that the method is to be executed by the test runner. For example, a common style of writing unit test is to do something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void TestPush()&lt;br /&gt;{&lt;br /&gt;   // Create a stack instance&lt;br /&gt;   // Do some stuff with it  &lt;br /&gt;   // Write a bunch of asserts&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The [Fact] attribute on the other hand sets quite a firm expectation on the test method. If you change the [Test] attribute above to a [Fact] attribute it makes no sense. TestPop is certainly not a fact. You are pushed to write tests with names that actually state some fact about the code under test. To be able to write a fact about the code you are testing you also have to chose a more narrow scope, probably with a single logical assert.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Fact]&lt;br /&gt;public void GivenEmptyStack_WhenPushing_ThenSizeIsOne()&lt;br /&gt;{&lt;br /&gt;   Stack stack = new Stack();&lt;br /&gt;   stack.Push(0);&lt;br /&gt;   Assert.Equal(1, stack.Size);&lt;br /&gt;}&lt;/pre&gt;</description><pubDate>Mon, 29 Mar 2010 20:33:18 +0200</pubDate><a10:updated>2010-03-29T20:33:18+02:00</a10:updated></item><item><guid isPermaLink="false">238</guid><link>http://www.erikojebo.se/Post.aspx?id=238</link><author>webmaster@erikojebo.se</author><title>Implementation of a Micro DSL</title><description>I've previously touch on the topic of using extension methods to clean up the syntax of your unit tests. That approach can give you very expressive and readable tests, for example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Validate_RequestWithValidPath_Ok()&lt;br /&gt;{&lt;br /&gt;    "GET /valid/path.html HTTP/1.1"&lt;br /&gt;        .ShouldValidateTo(RequestValidationStatus.Ok);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The drawback of using extension methods is when you want to have optional state setup methods that you can chain together to form an expression.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Validate_RequestWithValidPath_Ok()&lt;br /&gt;{&lt;br /&gt;    "GET /valid/path.html HTTP/1.1"&lt;br /&gt;        .UsingPathChecker(new AlwaysValidPathChecker())&lt;br /&gt;        .ShouldValidateTo(RequestValidationStatus.Ok);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If the UsingPathChecker method in the example above needs to be optional you have to supply two extension methods to the string class, UsingPathChecker and ShouldValidateTo. You also need a class that is used as a return value of the UsingPathChecker method call, which holds the state and also contains a ShouldValidateTo method.&lt;br /&gt;&lt;br /&gt;As you can see, it can quickly get a bit messy. To get rid of some of the mess, you can use a simple factory method with a well chosen name.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Validate_RequestWithValidPath_Ok()&lt;br /&gt;{&lt;br /&gt;    TheRequest("GET /valid/path.html HTTP/1.1")&lt;br /&gt;        .UsingPathChecker(new AlwaysValidPathChecker())&lt;br /&gt;        .ShouldValidateTo(RequestValidationStatus.Ok);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The method TheRequest in this example simply returns a new instance of the helper class which holds the state, and thereby eliminates the need for the extension methods. You now only have one place where you can put all the stuff you need to create your little DSL.&lt;br /&gt;&lt;br /&gt;Here is an example of what the helper class could look like:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;class HttpRequestValidatorSpecsIntermediary&lt;br /&gt;{&lt;br /&gt;    string _input;&lt;br /&gt;    private HttpRequestValidator _validator;&lt;br /&gt;&lt;br /&gt;    internal HttpRequestValidatorSpecsIntermediary(string input)&lt;br /&gt;    {&lt;br /&gt;        _input = input;&lt;br /&gt;        _validator = new HttpRequestValidator();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    internal HttpRequestValidatorSpecsIntermediary UsingPathChecker(IPathChecker pathChecker)&lt;br /&gt;    {&lt;br /&gt;        _validator = new HttpRequestValidator(pathChecker);&lt;br /&gt;&lt;br /&gt;        return this;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    internal void ShouldValidateTo(RequestValidationStatus expectedStatus)&lt;br /&gt;    {&lt;br /&gt;        var actualValidationStatus = _validator.Validate(new HttpRequest(_input));&lt;br /&gt;&lt;br /&gt;        Assert.AreEqual(expectedStatus, actualValidationStatus);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;</description><pubDate>Mon, 29 Mar 2010 19:56:21 +0200</pubDate><a10:updated>2010-03-29T19:56:21+02:00</a10:updated></item><item><guid isPermaLink="false">236</guid><link>http://www.erikojebo.se/Post.aspx?id=236</link><author>webmaster@erikojebo.se</author><title>Lessons Learned from Uncle Bob</title><description>Earlier this week I spent three days with Robert C. Martin, also known as Uncle Bob. He was in Stockholm giving a course on Advanced TDD, which I had the privilege to attend to. Hearing Uncle Bob talk about TDD for three whole days lead to quite a few thoughts and insights. This post is an attempt to summarize the main points I took away from his course.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Don't try to convince others, convince yourself&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;As a test driven developer on a team with non-test driven colleagues you have probably asked yourself the question "how do I convince the others to take up TDD?". The answer to this question is that you probably can't convince anyone to start doing TDD. That decision has to come from each and everyone themselves. What you can do is to make sure that you are really convinced of the benefits of TDD, yourself. If you are, it will shine through. As time passes the quality and effectiveness of your work will hopefully speak for itself, and help inspire the other developers.&lt;br /&gt;&lt;br /&gt;By focusing on following test driven practices yourself and being open for sharing your point of view when anyone shows interest you can slowly but surely spread the knowledge.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Tests are truly first class citizens&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Tests are important. Very important. The test code deserves the same tender love and care as your production code. Fore some reason test code is generally not held to the same standards as the production code. You should strive to keep your test code just as DRY and expressive as all other code. Make sure not to repeat tedious setup code or hard-to-read asserts. Use extract method if you can, and split the tests to make sure they are only testing one thing.&lt;br /&gt;&lt;br /&gt;The test is more important than the prettiness of your public interfaces. The consequence of this is that it is better to add test specific methods/properties to your objects and to &lt;br /&gt;increase the visibility of private/protected methods than to let your tests suffer. However, every time you are tempted to promote a private method to public for testability you should stop and think if it is not really an inner class that is trying to escape.&lt;br /&gt;&lt;br /&gt;For some reason, test code is generally not shipped with the production code. Why not use the fact that you have an extremely powerful diagnostic tool available and ship the tests when you release, together with some light weight test runner software. By doing that you can drastically improve your ability to diagnose problems that occur on the systems on which your software is being deployed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Make sure you can trust your test suite&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Once you have worked on a code base that is covered by a test suite you trust, no matter the size, you tend to get hooked on the feeling of freedom to refactor and change the code. When you reach this point of trust you can start reaping some of the most powerful benefits of a test driven code base. The flexibility of the code allows you to do whatever you please and get immediate feedback. Your QA process is drastically shortened which makes frequent releases and deployment a breeze.&lt;br /&gt;&lt;br /&gt;Achieving this kind of trust for your test suite is not something you do easily. It takes hard work and discipline. A good way to do keep the trust level high is to follow the three laws of TDD (see "TDD is hard, get used to it" below).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Don't ask for permission, just do it&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;A colleague of mine has the philosophy that it is easier to ask for forgiveness than for permission. This is especially true when it comes to TDD. If you start asking management and colleagues if you should do TDD or not, in an organization that is not that progressive, you will probably hear the usual anti-TDD arguments. If you don't want to take the fight, the easies way to resolve this issue is to not ask in the first place.&lt;br /&gt;&lt;br /&gt;Most likely, neither colleagues nor management will explicitly tell you NOT to do TDD if you just do it as a natural part of your development process. If your team/management is extreme enough to not want test code in the code base, or something crazy like that, just keep the test code locally on your machine instead of checking it in, or simply throw it away after letting it drive your production code.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Tests are both documentation and functional spec&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;By letting acceptance tests and unit tests drive the implementation of requirements, as well as the modifications made due to changed requirements, your tests always reflect the current functional spec. Your acceptance test provide an executable functional specification. If you are using a tool such as FitNesse that specification will be easily understood by the customer. In the best of worlds, this specification is not only easily understood by the customer, the customer is also able to create and update specs. Even if that is not the case, the acceptance tests provide a powerful basis from which customer and developer can cooperate to evolve the spec.&lt;br /&gt;&lt;br /&gt;The unit tests provide an extensive documentation of the actual implementation of the requirements. This documentation will always be up to date, which is not usually the case with traditional documentation. Code examples very efficiently describe the usage and behaviour of classes/methods etc, much more efficiently than written text. Unit tests are a very useful mixture of executable specification and usage examples. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;True TDD is hard, get used to it&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;To get all the benefits of TDD, you always have to push yourself to follow the three rules of TDD:&lt;br /&gt;&lt;br /&gt;1. You are not allowed to write any production code unless it is to make a failing unit test pass.&lt;br /&gt;2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.&lt;br /&gt;3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test. &lt;br /&gt;&lt;br /&gt;This is hard. Really hard. Testing legacy code is hard. Testing the GUI is hard. Convincing colleagues is hard. Keeping your tests fast is hard.&lt;br /&gt;&lt;br /&gt;There are no simple solutions which magically solve these hard issues. They are hard because they are hard. The only thing you can do is to do the best that you can, and do it over and over again.&lt;br /&gt;</description><pubDate>Fri, 19 Mar 2010 21:05:53 +0100</pubDate><a10:updated>2010-03-19T21:05:53+01:00</a10:updated></item><item><guid isPermaLink="false">232</guid><link>http://www.erikojebo.se/Post.aspx?id=232</link><author>webmaster@erikojebo.se</author><title>Emacsify Your Windows Keyboard Navigation</title><description>If you are an Emacs user you might miss the Emacs navigation keybindings while working in Windows. The Emacs navigation keybindings let you navigate through text without moving your hands from the touch typing position. This can be quite nice, especially when working on a laptop which has home/end and the arrows in funky locations on the keyboard.&lt;br /&gt;&lt;br /&gt;Since Emacs shortcuts are extremely ctrl heavy, many Emacs users rebind their caps lock key to be another ctrl key. To emulate the Emacs bindings, while at the same time keeping all the default Windows keybindings, I created a little AutoHotKey script that maps CapsLock+n/p etc to up arrow/down arrow etc.&lt;br /&gt;&lt;br /&gt;&lt;a href='http://www.autohotkey.com/'&gt;AutoHotKey&lt;/a&gt; is a small but extremely powerful application for doing stuff with keybindings in Windows. If you have not checked it out, I strongly suggest that you do. It can do some really usefull things.&lt;br /&gt;&lt;br /&gt;The tiny AutoHotKey script I wrote goes as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;CapsLock &amp; n::Send {Down}&lt;br /&gt;CapsLock &amp; p::Send {Up}&lt;br /&gt;CapsLock &amp; e::Send {End}&lt;br /&gt;CapsLock &amp; a::Send {Home}&lt;br /&gt;CapsLock &amp; f::Send {Right}&lt;br /&gt;CapsLock &amp; b::Send {Left}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now you can navigate easily in any Windows application, even explorer, while still keeping your system pair programming friendly by keeping all standard keybindings unchanged.</description><pubDate>Sun, 07 Mar 2010 22:17:12 +0100</pubDate><a10:updated>2010-03-07T22:17:12+01:00</a10:updated></item><item><guid isPermaLink="false">210</guid><link>http://www.erikojebo.se/Post.aspx?id=210</link><author>webmaster@erikojebo.se</author><title>Quick Tip: Kill Multiple Processes in Windows</title><description>Linux folks have probably used the &lt;em&gt;kill&lt;/em&gt; command from time to time. The Windows equivalent is &lt;em&gt;&lt;a href='http://technet.microsoft.com/en-us/library/bb491009.aspx'&gt;taskkill&lt;/a&gt;&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;To kill all instances of the WINWORD process, use the following command.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;taskkill /im WINWORD* /f&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The im switch specifies the image name, and the f switch tells taskkill to forcefully terminate the processes.</description><pubDate>Fri, 29 Jan 2010 12:55:05 +0100</pubDate><a10:updated>2010-01-29T12:55:05+01:00</a10:updated></item><item><guid isPermaLink="false">209</guid><link>http://www.erikojebo.se/Post.aspx?id=209</link><author>webmaster@erikojebo.se</author><title>Can Your Site be Used by a Visually Impaired User?</title><description>One of the benefits of using semantic HTML is that your site becomes more accessible to users with visual disabilities. This is because of the fact that screen reader applications make some assumptions about the content of your site depending on what tags you use in your markup. These assumptions are based on the semantic meaning of the HTML tags, which is why semantic markup fits screen readers like a glove.&lt;br /&gt;&lt;br /&gt;Writing semantic markup is not always that easy, though. The choice of how to mark up the content of your site can be difficult. To check the accessibility of your site and make these choices easier you can run your site through a screen reader and see which markup makes the most sense.&lt;br /&gt;&lt;br /&gt;An excellent way to see how a screen reader would present the contents of your site is to use the Firefox extension &lt;a href='http://www.standards-schmandards.com/projects/fangs/faq/'&gt;Fangs&lt;/a&gt;. This extension can generate the text that would be read by the screen reader &lt;a href='http://en.wikipedia.org/wiki/JAWS_%28screen_reader%29'&gt;Jaws&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;When you have installed Fangs, all you have to do is right click and choose "View Fangs", and tada, there is the screen reader output in plain text.</description><pubDate>Mon, 21 Dec 2009 15:58:19 +0100</pubDate><a10:updated>2009-12-21T15:58:19+01:00</a10:updated></item><item><guid isPermaLink="false">208</guid><link>http://www.erikojebo.se/Post.aspx?id=208</link><author>webmaster@erikojebo.se</author><title>Using Extension Methods to Refactor Your Tests</title><description>In a recent &lt;a href='http://www.iamnotmyself.com/2009/10/23/TDDKataCalculatorDemonstration.aspx'&gt;Katacast&lt;/a&gt;, Bobby Johnsson gave an excellent example of how C# extension methods can be used to create simplistic and readable test cases. He was performing the &lt;a href='http://katas.softwarecraftsmanship.org/?p=80'&gt;StringCalculator&lt;/a&gt; kata which is a kata where you write a calculator class that can calculate the sum of the integers in a given input string, for example "1,3,5".&lt;br /&gt;&lt;br /&gt;Without extension methods an ordinary test case for this kata might look something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Add_GivenTwoSingleDigitNumbers_ShouldReturnTheSum()&lt;br /&gt;{&lt;br /&gt;    var calculator = new StringCalculator();&lt;br /&gt;    int sum = calculator.Add("1,2");&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(3, sum);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This is a quite ordinary test, and there is no problem with the test itself. However if you have 15 other tests with the same structure there is room for some refactoring. One approach to refactor the test could be to simply create a helper method, like so:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;private void VerifySum(string input, int expectedSum)&lt;br /&gt;{&lt;br /&gt;    var calculator = new StringCalculator();&lt;br /&gt;    int actualSum = calculator.Add(input);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(expectedSum, actualSum);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This would give a test case that look like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Add_GivenTwoSingleDigitNumbers_ShouldReturnTheSum()&lt;br /&gt;{&lt;br /&gt;    VerifySum("1,2", 3);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you instead use the extension method refactoring you could create an extension method that looked something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public static class StringExtensions&lt;br /&gt;{&lt;br /&gt;    public static void ShouldAddUpTo(this string input, int expectedSum)&lt;br /&gt;    {&lt;br /&gt;        var calculator = new StringCalculator();&lt;br /&gt;&lt;br /&gt;        int actualSum = calculator.Add(input);&lt;br /&gt;&lt;br /&gt;        Assert.AreEqual(expectedSum, actualSum);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This approach gives a test that looks like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void Add_GivenTwoSingleDigitNumbers_ShouldReturnTheSum()&lt;br /&gt;{&lt;br /&gt;    "1,2".ShouldAddUpTo(3);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I must say that the extension method approach gives a much more fluent and readable syntax, with a very Ruby-esque feeling to it. The back side of using extension methods is that it can be confusing for the reader, since you must have included the namespace of the extensions for them to show up, and you cannot resolve those namespaces as easily as you could with standard method calls. In my opinion, the example above is an excellent use of extension methods when the extension method is located in the same place as the tests, since that makes it obvious what is going on.&lt;br /&gt;&lt;br /&gt;Extension methods is truly an awesome feature of C#, when used correctly.</description><pubDate>Fri, 11 Dec 2009 11:01:50 +0100</pubDate><a10:updated>2009-12-11T11:01:50+01:00</a10:updated></item><item><guid isPermaLink="false">207</guid><link>http://www.erikojebo.se/Post.aspx?id=207</link><author>webmaster@erikojebo.se</author><title>Verifying IEnumerable&lt;T&gt; HasMany Mapping with Fluent NHibernate</title><description>Fluent NHibernate has built in functionality to make it very simple to verify that your entities are mapped correctly. To test the mappings of an entity you can use the PersistenceSpecification class:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[Test]&lt;br /&gt;public void CanMapPost()&lt;br /&gt;{&lt;br /&gt;    using (ISession session = SessionFactorySingleton.OpenSession())&lt;br /&gt;    {&lt;br /&gt;        new PersistenceSpecification&lt;Post&gt;(session)&lt;br /&gt;            .CheckProperty(p =&gt; p.Name, "Post Name")&lt;br /&gt;            .CheckProperty(p =&gt; p.DateTime, new DateTime(2009, 12, 6))&lt;br /&gt;            .CheckProperty(p =&gt; p.Comment, "Comment goes here")&lt;br /&gt;            .CheckProperty(p =&gt; p.Amount, 250.90)&lt;br /&gt;            .VerifyTheMappings();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This creates an instance of the Post class, saves it to the database, retrieves it again and verifies that its properties have the correct values.&lt;br /&gt;&lt;br /&gt;The PersistenceSpecification class also includes functionality to verify &lt;em&gt;HasMany&lt;/em&gt; and &lt;em&gt;References&lt;/em&gt; mappings. For example, you could verify a HasMany mapping like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;new PersistenceSpecification&lt;Post&gt;(session)&lt;br /&gt;    .CheckList(p =&gt; p.Tags, tags)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This works well if you have a setter for your collection. However, if you expose the collection as an IEnumerable&lt;T&gt; and use a private backing field, for example by mapping it as Access.CamelCaseField, it will not work. This is because of the fact that Fluent NHibernate does not know how to add the items to the collection.&lt;br /&gt;&lt;br /&gt;This issue has been fixed in later releases of Fluent NHibernate. Now you can verify your IEnumerable&lt;T&gt; property mapping using the CheckEnumerable method of the PersistenceSpecification class. A complete verification of the post class above could look like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public void CanMapPost()&lt;br /&gt;{&lt;br /&gt;    using (ISession session = SessionFactorySingleton.OpenSession())&lt;br /&gt;    {&lt;br /&gt;        var tags = new List&lt;Tag&gt;()&lt;br /&gt;        {&lt;br /&gt;            new Tag("tag 1"),&lt;br /&gt;            new Tag("tag 2")&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        new PersistenceSpecification&lt;Post&gt;(session)&lt;br /&gt;            .CheckProperty(p =&gt; p.Name, "Post Name")&lt;br /&gt;            .CheckProperty(p =&gt; p.DateTime, new DateTime(2009, 12, 6))&lt;br /&gt;            .CheckProperty(p =&gt; p.Comment, "Comment goes here")&lt;br /&gt;            .CheckProperty(p =&gt; p.Amount, 250.90)&lt;br /&gt;            .CheckEnumerable(p =&gt; p.Tags, (p, t) =&gt; p.AddTag(t), tags)&lt;br /&gt;            .VerifyTheMappings();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;</description><pubDate>Mon, 07 Dec 2009 21:15:59 +0100</pubDate><a10:updated>2009-12-07T21:15:59+01:00</a10:updated></item><item><guid isPermaLink="false">205</guid><link>http://www.erikojebo.se/Post.aspx?id=205</link><author>webmaster@erikojebo.se</author><title>Free .NET Profiler</title><description>If you don't have access to a commercial profiler, such as JetBrains' dotTrace or ANTS Profiler, take a look at &lt;a href='http://www.ijw.co.nz/index.htm'&gt;IJW Profiler for .NET &amp; Java&lt;/a&gt;. It is a basic, but quite nice profiler which is free and the source code is available from the link above.</description><pubDate>Fri, 06 Nov 2009 20:04:47 +0100</pubDate><a10:updated>2009-11-06T20:04:47+01:00</a10:updated></item><item><guid isPermaLink="false">202</guid><link>http://www.erikojebo.se/Post.aspx?id=202</link><author>webmaster@erikojebo.se</author><title>Starting WPF Application with Window from another Assembly</title><description>When you create a WPF application you get the standard App.xaml and Window.xaml files. The App.xaml file has the StartupUri attribute set to Window.xaml.  This is all well and fine, and if you rename Window.xaml or create another window that you want to use as the startup window you can simply modify the value of the StartupUri attribute.&lt;br /&gt;&lt;br /&gt;If you are want to use a window defined in another assembly as the startup window it becomes a little trickier.  The simplest way to accomplish this is to override the Application.OnStarupt method. In you override you can instantiate the window you want to show and then simply call Show() on it. For example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;public partial class App : Application&lt;br /&gt;{&lt;br /&gt;    protected override void OnStartup(StartupEventArgs e)&lt;br /&gt;    {&lt;br /&gt;        // Create an instance of the window that is located in another assembly&lt;br /&gt;        var customWindow = new CustomWindow();&lt;br /&gt;&lt;br /&gt;        customWindow.Show();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You could also create your own main method, and manually create and run your application.&lt;br /&gt;&lt;br /&gt;The first step is to create your own main method is to change the Build Action property of your App.xaml file from ApplicationDefinition to Page, which stops Visual Studio from creating a main method for you. The second step is to add a main method to your App class:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;public partial class App : Application&lt;br /&gt;{&lt;br /&gt;    [STAThread]&lt;br /&gt;    public static void Main()&lt;br /&gt;    {&lt;br /&gt;        Application app = new Application();&lt;br /&gt;&lt;br /&gt;        // Create an instance of the window that is located in another assembly&lt;br /&gt;        var customWindow = new CustomWindow();&lt;br /&gt;&lt;br /&gt;        app.Run(customWindow);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now you are all set. Compile, run and enjoy!</description><pubDate>Sun, 18 Oct 2009 19:26:05 +0200</pubDate><a10:updated>2009-10-18T19:26:05+02:00</a10:updated></item><item><guid isPermaLink="false">198</guid><link>http://www.erikojebo.se/Post.aspx?id=198</link><author>webmaster@erikojebo.se</author><title>Avoiding Flicker at Page Load when Using the Ajax Control Toolkit ModalPopup Extender Control</title><description>If you are using the ModalPopup extender control from the Ajax Control Toolkit you might have noticed that the popup flickers when the page is loaded. This is because of that there is a slight delay from when the page is rendered to when the javascript of the extender control hides the popup.&lt;br /&gt;&lt;br /&gt;To get around this you need to set the display style of the popup element. It seems like you have to set the style directly at the control, and not from an external style sheet.&lt;br /&gt;&lt;br /&gt;To set the display style to none for your popup element, add the following attribute:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;style="display: none"&lt;/pre&gt;</description><pubDate>Thu, 15 Oct 2009 20:35:28 +0200</pubDate><a10:updated>2009-10-15T20:35:28+02:00</a10:updated></item><item><guid isPermaLink="false">177</guid><link>http://www.erikojebo.se/Post.aspx?id=177</link><author>webmaster@erikojebo.se</author><title>Displaying Modal Dialogs with WPF</title><description>In WPF you can show a modal dialog with the ShowDialog method, just as in WinForms. However, it does not behave quite right. For example, if you display a modal dialog, then swich to another application, and switch back to you application the main form will be shown and not the modal dialog.&lt;br /&gt;&lt;br /&gt;To get the expected behaviour you have to set the Owner property of the dialog before calling ShowDialog():&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;var dialog = new FooDialog();&lt;br /&gt;dialog.Owner = this;&lt;br /&gt;dialog.ShowDialog();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This makes the modal dialog the topmost window of the application.</description><pubDate>Thu, 13 Aug 2009 12:54:41 +0200</pubDate><a10:updated>2009-08-13T12:54:41+02:00</a10:updated></item><item><guid isPermaLink="false">176</guid><link>http://www.erikojebo.se/Post.aspx?id=176</link><author>webmaster@erikojebo.se</author><title>NHibernate: Using Paging with Join Queries</title><description>When you write queries that use joins in NHibernate, most of the time the result you get back is not unique at the root entity level. This is usually not a problem since you can stick a DistinctRootEntityResultTransformer on the query and get a nice unique result set.&lt;br /&gt;&lt;br /&gt;However, if you want to page the result based on the root entities there is a problem. Since the DistinctRootEntityResultTransformer operates on the result set that comes back from the database and the paging modifies the database query the paging is made before the data is filtered to be unique. This leads to a result that you probably don\'t want.&lt;br /&gt;&lt;br /&gt;To combine paging with joined queries you can split the query into two separate queries. The first query fetches the root entities based on some criteria and pages the result. The second query performs the join for the entities returned from the previous query, without caring about paging or uniqueness. Since NHibernate makes sure an entity is mapped to exactly one instance in a session the result from the second query loads data into the instances returned from the first query. &lt;br /&gt;&lt;br /&gt;Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;// Assuming two classes: Post and Comment. A post has many comments.&lt;br /&gt;&lt;br /&gt;public IEnumerable&lt;Post&gt; GetPostsByPageWithCommentsLoaded(int pageIndex, int pageSize)&lt;br /&gt;{&lt;br /&gt;    using (ISession session = SessionFactory.OpenSession())&lt;br /&gt;   {&lt;br /&gt;      // Load the posts using paging&lt;br /&gt;      var posts = session.CreateCriteria(typeof(Post))&lt;br /&gt;         .AddOrder(Order.Desc("DateTime"))&lt;br /&gt;         .SetFirstResult(pageIndex * pageSize)&lt;br /&gt;         .SetMaxResult(pageSize)&lt;br /&gt;         .List&lt;Post&gt;();&lt;br /&gt;&lt;br /&gt;     // Eager load the comments for all the posts returned in the previous query&lt;br /&gt;     // Restrictions.In() is used to avoid fetching unnecessary data&lt;br /&gt;    // temporaryPosts is never used. The point of the query is to load data into&lt;br /&gt;    // the instances returned from the previous query&lt;br /&gt;     var temporaryPosts = session.CreateCriteria(typeof(Post))&lt;br /&gt;         .SetFetchMode("Comments", FetchMode.Eager)&lt;br /&gt;         .Add(Restrictions.In("Id", posts.Select(p =&gt; p.Id).ToArray()))&lt;br /&gt;         .List&lt;Post&gt;();&lt;br /&gt;&lt;br /&gt;     return posts;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;</description><pubDate>Sun, 26 Jul 2009 17:45:34 +0200</pubDate><a10:updated>2009-07-26T17:45:34+02:00</a10:updated></item><item><guid isPermaLink="false">172</guid><link>http://www.erikojebo.se/Post.aspx?id=172</link><author>webmaster@erikojebo.se</author><title>The First Lesson of Scrum</title><description>The other day I learned one of the most important Scrum techniques: Always take a new sticky by gently pulling it of the pad from left to right, &lt;em&gt;not&lt;/em&gt; by yanking it upwards as you would normally do. This ensures maximum stickiness, so that the notes last the whole sprint.</description><pubDate>Wed, 17 Jun 2009 22:12:30 +0200</pubDate><a10:updated>2009-06-17T22:12:30+02:00</a10:updated></item><item><guid isPermaLink="false">167</guid><link>http://www.erikojebo.se/Post.aspx?id=167</link><author>webmaster@erikojebo.se</author><title>C#: Creating a Cursor from a Resource</title><description>A simple way to use custom cursor icons in .NET applications is to include the .cur file in the project resources (project properties -&gt; resources -&gt; add resource from file).&lt;br /&gt;&lt;br /&gt;A cursor can then be created like so:&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;Cursor cursor = new Cursor(new System.IO.MemoryStream(&lt;br /&gt;    Properties.Resources.MyCursorName));&lt;br /&gt;&lt;/pre&gt;</description><pubDate>Tue, 02 Jun 2009 12:28:16 +0200</pubDate><a10:updated>2009-06-02T12:28:16+02:00</a10:updated></item><item><guid isPermaLink="false">165</guid><link>http://www.erikojebo.se/Post.aspx?id=165</link><author>webmaster@erikojebo.se</author><title>Tests as Documentation</title><description>A couple of weeks ago there was an internal seminar at work about Test-Driven Development. The seminar was held by one of the senior devs who has been doing TDD for a few years. As a part of the seminar he presented a few levels of TDD practice, corresponding to different "aha" moments.&lt;br /&gt;&lt;br /&gt;One of the "aha" moments was the realization that tests are documentation. When I heard this I thought for a while if I had had this "aha" moment yet or not. I could see how tests could be considered documentation, but that was not how I primarily saw it.&lt;br /&gt;&lt;br /&gt;However, about a week ago I actually had that "aha" moment, and then once again today. Last week I had to go through some complex import/export code that I had worked with maybe a month earlier. Since I had worked on the code not that long ago I thought that I would remember how the code was supposed to work. That was not the case. I found myself wondering if a specific behaviour was by design or if it was a bug. After a few seconds of thinking I realized that the tests could tell me ("aha!"). A quick look in the test fixture and I knew that the code worked according to spec.&lt;br /&gt;&lt;br /&gt;Today I was setting up a new hobby project, and I was rewriting some old configuration code for Fluent-NHibernate, since that project is changing quite rapidly. I was writing the database configuration and tried to find the correct way to specify "integrated security" as login for a MS SQL Server connection. Intellisense showed that there was a "TrustedConnection()" method of the configuration object, but it had no XML comments.&lt;br /&gt;&lt;br /&gt;A quick google search turned up the Fluent NHibernate SVN repository, so I though to myself that I might as well take a quick look in the source. The search function at google code found the unit test for TrustedConnection().&lt;br /&gt;&lt;br /&gt;The test look as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;[Test]&lt;br /&gt;public void ConnectionString_for_trustedConnection_is_added_to_the_configuration()&lt;br /&gt;{&lt;br /&gt;    MsSqlConfiguration.MsSql2005&lt;br /&gt;        .ConnectionString(c =&gt; c&lt;br /&gt;            .Server("db-srv")&lt;br /&gt;            .Database("tables")&lt;br /&gt;            .TrustedConnection())&lt;br /&gt;        .ToProperties().ShouldContain("connection.connection_string" ,&lt;br /&gt;               "Data Source=db-srv;Initial Catalog=tables;Integrated Security=True");&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Integrated security it is then. Unit tests to the rescue! ("aha!")&lt;br /&gt;&lt;br /&gt;However, when it comes to public libraries and API:s like Fluent NHibernate I really think that there is no excuse for not writing XML comments. Documentation in Intellisense is such a productivity boost that it is a must have for all API:s.&lt;br /&gt;&lt;br /&gt;From now on, I can say without a doubt that I believe that unit tests are documentation.</description><pubDate>Sun, 17 May 2009 21:22:27 +0200</pubDate><a10:updated>2009-05-17T21:22:27+02:00</a10:updated></item><item><guid isPermaLink="false">150</guid><link>http://www.erikojebo.se/Post.aspx?id=150</link><author>webmaster@erikojebo.se</author><title>Programmatically Add Conditional Formatting Formula to a Cell in Excel</title><description>The following snippet demonstrates how to add a conditional formatting rule to a cell. The rule specifies that the cell color is set to red if the contents of the cell has a length in characters longer than 3.&lt;br /&gt;&lt;br /&gt;The snippet assumes an existing worksheet instance.&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var range = (Excel.Range)worksheet.Cells[rowNo + 2, colNo + 1];&lt;br /&gt;&lt;br /&gt;Excel.FormatCondition condition = (Excel.FormatCondition)range.FormatConditions.Add(&lt;br /&gt;    Microsoft.Office.Interop.Excel.XlFormatConditionType.xlExpression, &lt;br /&gt;    Type.Missing, "=LEN(R[0]C[0])&gt;3", Type.Missing, Type.Missing, &lt;br /&gt;    Type.Missing, Type.Missing, Type.Missing);&lt;br /&gt;&lt;br /&gt;condition.Interior.ColorIndex = 3; // Red&lt;br /&gt;&lt;/pre&gt;</description><pubDate>Wed, 15 Apr 2009 14:30:36 +0200</pubDate><a10:updated>2009-04-15T14:30:36+02:00</a10:updated></item><item><guid isPermaLink="false">148</guid><link>http://www.erikojebo.se/Post.aspx?id=148</link><author>webmaster@erikojebo.se</author><title>"Could not find installable ISAM" when Reading Excel Document Programatically with Microsoft Jet</title><description>This generic error message is often an indication of a syntax error in the connection string. In my case the error occurred when I tried to add a second Extended Property, like so:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var connection = new System.Data.OleDb.OleDbConnection(&lt;br /&gt;    "Provider=Microsoft.Jet.OLEDB.4.0;" +&lt;br /&gt;    "Data Source=" + path + ";" +&lt;br /&gt;    "Extended Properties=Excel 8.0;HDR=No;");&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The problem turned out to be that the multiple extended properties were not quoted. A single extended property does not need quoting, but multiple do.&lt;br /&gt;&lt;br /&gt;The fix was then simply to add escaped quotes:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;var connection = new System.Data.OleDb.OleDbConnection(&lt;br /&gt;    "Provider=Microsoft.Jet.OLEDB.4.0;" +&lt;br /&gt;    "Data Source=" + path + ";" +&lt;br /&gt;    "Extended Properties=\\"Excel 8.0;HDR=No;\\"");&lt;/pre&gt;</description><pubDate>Tue, 07 Apr 2009 15:32:17 +0200</pubDate><a10:updated>2009-04-07T15:32:17+02:00</a10:updated></item><item><guid isPermaLink="false">141</guid><link>http://www.erikojebo.se/Post.aspx?id=141</link><author>webmaster@erikojebo.se</author><title>SQL Server Express Profiler</title><description>At the moment I am comparing a few different object/relational mapping tools (ORM:s), namely Linq to SQL, Entity Framework, NHibernate and MyGeneration Doodads. Both Linq to SQL and NHibernate have excellent features for writing all generated SQL statements to the console. Unfortunately it is not quite as easy in the other three.&lt;br /&gt;&lt;br /&gt;Today I needed to debug a Subsonic query but could not find any information about how to output the generated SQL to the console. Finally I gave up and thought to myself that I would have been really great if Microsoft had included a SQL profiler in the SQL Server Express Editions. It then occurred to me that I had never really tried to find another SQL profiler, so a quick google search found &lt;a href='http://sqlprofiler.googlepages.com/'&gt;SQL Server 2005/2008 Express Profiler&lt;/a&gt; which is an open source profiler for MS SQL Server Express, as you probably guessed.&lt;br /&gt;&lt;br /&gt;The project home page is quite spartan and the GUI isn't the best ever, but it works. First of all you have to select which events you want to see in the trace. When that is set up you just press play, do you SQL-magic and watch the queries appear in the trace.&lt;br /&gt;&lt;br /&gt;So, if you are using SQL Server Express it is absolutely worth a try.</description><pubDate>Fri, 20 Feb 2009 15:17:10 +0100</pubDate><a10:updated>2009-02-20T15:17:10+01:00</a10:updated></item><item><guid isPermaLink="false">122</guid><link>http://www.erikojebo.se/Post.aspx?id=122</link><author>webmaster@erikojebo.se</author><title>Compiling Fluent-NHibernate for NHibernate 2.1 without Using Rake</title><description>Currently Fluent-NHibernate references NHibernate version 2.0.0.4 per default. At the Fluent-NHibernate page the suggested way of compiling Fluent-NHibernate for NHibernate 2.1 is to do as follows:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;To enable NH 2.1 support, run the rake task (rake use_nhib_21) and then build FluentNHibernate.Versioning.NH21. To go back to NH 2.0.1, run the rake task (rake use_nhib_201) and build FluentNHibernate.Versioning.NH20&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;To do this you obviously need to have rake installed. As a Windows user you can get rake by using the &lt;a href='http://rubyinstaller.rubyforge.org/wiki/wiki.pl'&gt;Ruby installer for Windows&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;However, if you do not want to install rake, you can still change the references by hand and compile new Fluent-NHibernate binaries.&lt;br /&gt;&lt;br /&gt;First of all, get the latest source code by doing a check out of the latest code from the &lt;a href='http://code.google.com/p/fluent-nhibernate/'&gt;Fluent-NHibernate repository&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;For each project in the solution, remove the references to NHibernate and NHibernate.Linq, then add references to the latest dlls. These dlls are located in the sub-folder Tools/NHibernate/nhib2.1/ in your check out folder.&lt;br /&gt;&lt;br /&gt;Now you can find a shining, new FluentNHibernate.dll, that uses NHibernate 2.1, in one of the bin folders.</description><pubDate>Fri, 23 Jan 2009 14:31:36 +0100</pubDate><a10:updated>2009-01-23T14:31:36+01:00</a10:updated></item><item><guid isPermaLink="false">120</guid><link>http://www.erikojebo.se/Post.aspx?id=120</link><author>webmaster@erikojebo.se</author><title>SCTP Socket Programming</title><description>If you are interested in socket programming you really should take a look at the Stream Control Transmission Protocol (&lt;a href='http://en.wikipedia.org/wiki/SCTP'&gt;SCTP&lt;/a&gt;). It is a new transport protocol that can be used in about the same way as both UDP and TCP, depending on which kind of SCTP socket that is used.&lt;br /&gt;&lt;br /&gt;SCTP has two kinds of sockets, one-to-one sockets and one-to-many sockets. One-to-one sockets are very similar to classic TCP sockets, and are also known as TCP style sockets. One-to-many sockets are similar to UDP sockets, and as you probably can guess, are also known as UDP style sockets.&lt;br /&gt;&lt;br /&gt;SCTP one-to-one sockets are actually so similar to TCP sockets that you can convert a TCP application to SCTP just by changing the socket call:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;// TCP socket&lt;br /&gt;int tcp_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); &lt;br /&gt;&lt;br /&gt;// SCTP one-to-one socket&lt;br /&gt;int sctp_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you want to dig deeper into SCTP I can recommend &lt;a href='http://unpbook.com/'&gt;Unix Network Programming, 3rd&lt;/a&gt; ed. by Stevens, R. The book covers both basic and more advanced parts of SCTP, with useful examples. If you do some kind of socket programming, you should own a copy of this book, no matter what protocol you currently use. The only problem with the SCTP part of the book is that the specification has changed slightly since the book was printed, so I would recommend updated man pages as API reference. Personally I prefer the &lt;a href='http://docs.sun.com/app/docs/coll/40.10'&gt;Sun Solaris man pages&lt;/a&gt;, which are available both as web pages and PDF.&lt;br /&gt;&lt;br /&gt;For RFC:s, drafts and documents &lt;a href='http://www.sctp.org'&gt;SCTP.org&lt;/a&gt; is the place to go.</description><pubDate>Sat, 17 Jan 2009 16:44:03 +0100</pubDate><a10:updated>2009-01-17T16:44:03+01:00</a10:updated></item><item><guid isPermaLink="false">98</guid><link>http://www.erikojebo.se/Post.aspx?id=98</link><author>webmaster@erikojebo.se</author><title>Recompiling VirtualBox</title><description>If you run VirtualBox with Ubuntu as your host OS, you will probably run in to some problems when there is a kernel upgrade. To fix this you just have to do a recompile of VirtualBox, like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;killall VirtualBox&lt;br /&gt;sudo /etc/init.d/vboxdrv setup&lt;/pre&gt;</description><pubDate>Tue, 09 Dec 2008 10:21:15 +0100</pubDate><a10:updated>2008-12-09T10:21:15+01:00</a10:updated></item><item><guid isPermaLink="false">84</guid><link>http://www.erikojebo.se/Post.aspx?id=84</link><author>webmaster@erikojebo.se</author><title>Batch Image Resize/Rotate in Ubuntu</title><description>Resizing and rotating images can be a really time consuming and boring task that just begs to be automated. If you are running Ubuntu, this is easily achieved by using &lt;a href='http://imagemagick.org/script/index.php'&gt;Imagemagick&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;To install imagemagick, just do an ordinary apt-get, like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;sudo apt-get install imagemagick&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now you are ready to start batch processing your images. Here are some examples of useful commands:&lt;br /&gt;&lt;br /&gt;To resize an image to a specific size:&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert foo.jpg -resize 640x480 foo2.jpg&lt;/pre&gt;&lt;br /&gt;To resize using a percentage:&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert foo.jpg -resize 50% foo2.jpg&lt;/pre&gt;&lt;br /&gt;To resize an image to a specific width, keeping aspect ratio:&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert foo.jpg -resize 640 foo2.jpg&lt;/pre&gt;&lt;br /&gt;To resize an image to a specific height, keeping aspect ratio:&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert foo.jpg -resize x480 foo2.jpg&lt;/pre&gt;&lt;br /&gt;To rotate an image:&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert foo.jpg -rotate 90 foo2.jpg&lt;/pre&gt;&lt;br /&gt;These commands can be chained together like so:&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert foo.jpg -rotate 90 -resize 640 foo2.jpg&lt;/pre&gt;&lt;br /&gt;To operate on multiple files, file &lt;a href='http://en.wikipedia.org/wiki/Glob_(programming)'&gt;globbing&lt;/a&gt; can be used, like this&lt;br /&gt;&lt;pre class="prettyprint"&gt;convert *.jpg -rotate 90 -resize 500 rotated_%03d.jpg&lt;/pre&gt;&lt;br /&gt;The %03d means three incrementing digits. So in this example, the new file names will be rotated_000.jpg, rotated_001.jpg etc.</description><pubDate>Fri, 05 Dec 2008 09:35:57 +0100</pubDate><a10:updated>2008-12-05T09:35:57+01:00</a10:updated></item><item><guid isPermaLink="false">79</guid><link>http://www.erikojebo.se/Post.aspx?id=79</link><author>webmaster@erikojebo.se</author><title>Unit Testing Selection on a Windows Forms ListView Control</title><description>This is kind of an edge case, but it just stole about two hours of my day, so I thought it might be of interest for someone else who stumbles upon this problem.&lt;br /&gt;&lt;br /&gt;In the application I am currently working on I am subclassing a ListView to make it handle strongly typed tags, and for example directly get a strongly typed instance of the tag of the currently selected item. To test my implementation I programmatically created an instance of my ListView subclass, added items and added an item to the selection using both:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;Item.Selected = true&lt;/pre&gt; and &lt;br /&gt;&lt;pre class="prettyprint"&gt;listView.SelectedIndices.Add(...)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The problem was that this had no effect at all on the ListView\'s SelectedIndices and SelectedItems collections.&lt;br /&gt;&lt;br /&gt;I tried doing the same thing on a ListView in one of the forms of my appliation and that worked, but it did not work if I instantiated the ListView directly in the code. After some googeling I finally found a comment to the MSDN documentation on the ListViewItem.Selected Property, saying that the Selected property could not be trusted if the ListView had not been drawn. I did some quick testing myself and confirmed that the problem indeed disappeared if the ListView was drawn.&lt;br /&gt;&lt;br /&gt;So, the solution to the problem was to do something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;        [Test]&lt;br /&gt;        public void CanGetSelectedItems()&lt;br /&gt;        {&lt;br /&gt;            // Sadly, this is needed to make the ListView handle selection properly.&lt;br /&gt;            // If the ListView is instantiated directly SelectedIndices, SelectedItems etc&lt;br /&gt;            // do not update when modified.&lt;br /&gt;            var f = new DummyForm(listView);&lt;br /&gt;            f.Show();&lt;br /&gt;&lt;br /&gt;            listView.SelectedIndices.Add(0);&lt;br /&gt;&lt;br /&gt;            Assert.AreEqual(1, listView.SelectedIndices.Count);&lt;br /&gt;&lt;br /&gt;            ICollection&amp;lt;string&amp;gt; items = listView.SelectedItems;&lt;br /&gt;&lt;br /&gt;            Assert.AreEqual(1, items.Count);&lt;br /&gt;            Assert.IsTrue(items.Contains(strings[0]));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private class DummyForm : Form&lt;br /&gt;        {&lt;br /&gt;            public DummyForm(ListView listView)&lt;br /&gt;            {&lt;br /&gt;                this.WindowState = FormWindowState.Minimized;&lt;br /&gt;                this.ShowInTaskbar = false;&lt;br /&gt;                this.Controls.Add(listView);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;</description><pubDate>Thu, 20 Nov 2008 10:55:38 +0100</pubDate><a10:updated>2008-11-20T10:55:38+01:00</a10:updated></item><item><guid isPermaLink="false">78</guid><link>http://www.erikojebo.se/Post.aspx?id=78</link><author>webmaster@erikojebo.se</author><title>RhinoMocks - Expect that Method is Never Called At All</title><description>If you want to set up an expectation that a method is never called at all, and not just that it is never called with a specific parameter, you can do like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;MockRepository mocks = new MockRepository();&lt;br /&gt;Foo mockFoo = mocks.StrictMock&lt;Foo&gt;();&lt;br /&gt;&lt;br /&gt;// For void methods:&lt;br /&gt;mockFoo.Bar();&lt;br /&gt;LastCall.IgnoreArguments().Repeat.Never();&lt;br /&gt;&lt;br /&gt;// For non-void methods:&lt;br /&gt;Expect.Call(mockFoo.Baz(1)).Return(null).Repeat.Never();&lt;br /&gt;LastCall.IgnoreArguments().Repeat.Never();&lt;br /&gt;&lt;br /&gt;mockFoo.Replay();&lt;br /&gt;&lt;br /&gt;// Do stuff&lt;br /&gt;&lt;br /&gt;mockFoo.VerifyAllExpectations();&lt;/pre&gt;</description><pubDate>Wed, 19 Nov 2008 13:35:23 +0100</pubDate><a10:updated>2008-11-19T13:35:23+01:00</a10:updated></item><item><guid isPermaLink="false">77</guid><link>http://www.erikojebo.se/Post.aspx?id=77</link><author>webmaster@erikojebo.se</author><title>Fluent-NHibernate - Unknown Entity Class Exception</title><description>If you are using Fluent NHibernate and you get the exception:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;NHibernate.MappingException: Unknown entity class: Foo.Bar.Baz&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Make sure your mapping file (BazMap in this example) is public.&lt;br /&gt;&lt;br /&gt;If you still get the exception, make sure you are loading you class maps correctly. It should look something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;Configuration configuration = new Configuration().Configure();&lt;br /&gt;&lt;br /&gt;var persistenceModel = new PersistenceModel();&lt;br /&gt;persistenceModel.addMappingsFromAssembly(typeof(Baz).Assembly);&lt;br /&gt;persistenceModel.Configure(configuration);&lt;br /&gt;            &lt;br /&gt;var sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;PersistenceModel is located in the FluentNHibernate namespace.</description><pubDate>Tue, 18 Nov 2008 11:19:22 +0100</pubDate><a10:updated>2008-11-18T11:19:22+01:00</a10:updated></item><item><guid isPermaLink="false">73</guid><link>http://www.erikojebo.se/Post.aspx?id=73</link><author>webmaster@erikojebo.se</author><title>Windows PowerShell</title><description>This weekend I installed SQL Server 2008 Express and one of the requirements for the install was to have Windows PowerShell. I had heard &lt;a href='http://www.hanselman.com/blog/'&gt;Scott Hanselman&lt;/a&gt; talk about it previously, but never tried it myself.&lt;br /&gt;&lt;br /&gt;Today I tried it out for the first time, and after about 5 seconds I decided that it was a great product. The simple reason was that I typed ls and hit return, and it listed the folder contents for me. That is so sweet.&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_blog/powershell.jpg' /&gt;&lt;br /&gt;&lt;br /&gt;Previously I have had a small batch file named ls that did a dir, just to be able to list without having to think about it, but this is quite a bit nicer.</description><pubDate>Fri, 14 Nov 2008 08:34:33 +0100</pubDate><a10:updated>2008-11-14T08:34:33+01:00</a10:updated></item><item><guid isPermaLink="false">72</guid><link>http://www.erikojebo.se/Post.aspx?id=72</link><author>webmaster@erikojebo.se</author><title>Debugging NUnit Unit Tests in Visual C# Express 2008</title><description>Since Visual C# Express does not allow add-in:s, and attach to process is not available, you have to roll your own simple test runner if you want to be able to debug your unit test.&lt;br /&gt;&lt;br /&gt;Fortunately this is quite simple. The easiest way, in my opinion, is to add a new Console Application project to your solution. Add a reference to nunit-console-runner.dll, and then add the following code to the main of the new console app:&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_blog/nunit-console-runner.jpg' /&gt;&lt;br /&gt;Set the console app as start up project by right clicking the project in the solution explorer and choosing "Set as start up project".&lt;br /&gt;&lt;br /&gt;You can now set break points in your test code and simply hit F5 to debug the tests.</description><pubDate>Thu, 13 Nov 2008 15:42:23 +0100</pubDate><a10:updated>2008-11-13T15:42:23+01:00</a10:updated></item><item><guid isPermaLink="false">70</guid><link>http://www.erikojebo.se/Post.aspx?id=70</link><author>webmaster@erikojebo.se</author><title>Cleaning Up Your Grub Boot Menu (Ubuntu)</title><description>As more and more kernel updates get installed on your system the grub menu tends to grow longer. This is easy to fix:&lt;br /&gt;&lt;br /&gt;Back up your grub menu file:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;sudo cp /boot/grub/menu.lst /boot/grub/menu.lst.bak&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Edit your menu file:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;sudo gedit /boot/grub/menu.lst&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Comment out any unwanted entries by adding a # in the beginning of each line that should not be displayed.&lt;br /&gt;&lt;br /&gt;Save the file, and when you reboot the grub menu should be nice and small again.</description><pubDate>Wed, 12 Nov 2008 13:04:16 +0100</pubDate><a10:updated>2008-11-12T13:04:16+01:00</a10:updated></item><item><guid isPermaLink="false">50</guid><link>http://www.erikojebo.se/Post.aspx?id=50</link><author>webmaster@erikojebo.se</author><title>Changing the Default Vista Explorer Window Size</title><description>After using Vista for a month or two, the single most annoying thing was that Windows Explorer kept opening in its default size, which is far to small, at least if you have a decent resolution. I finally got tired of it and googled for a fix. It turns out that if you shift click the close button on the window, it remembers that size for the specific folder you are currently in. So what you have to do is to:&lt;br /&gt;&lt;br /&gt;- start explorer the way you normally do&lt;br /&gt;- change the size of the window to the desired size, then&lt;br /&gt;- shift click the close button in the top, right hand corner before you do anything else.&lt;br /&gt;&lt;br /&gt;It should now pop up with the right size, from now on.&lt;br /&gt;&lt;br /&gt;For more info, read this &lt;a href='http://blog.maniacd.net/2007/05/15/steps-for-setting-vistas-default-explorer-window-size/'&gt;blog post&lt;/a&gt;.</description><pubDate>Sat, 25 Oct 2008 07:45:22 +0200</pubDate><a10:updated>2008-10-25T07:45:22+02:00</a10:updated></item><item><guid isPermaLink="false">34</guid><link>http://www.erikojebo.se/Post.aspx?id=34</link><author>webmaster@erikojebo.se</author><title>C# Automatic properties in Visual Studio 2008</title><description>In .Net Framework 3.5 automatic properties were introduced. Automatic properties is a compiler feature that lets you write less code, and thereby increases your productivity. To add a property that only has a basic getter and setter, you only have to type:&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_autoprop/prop.jpg' /&gt;&lt;br /&gt;The compiler then fills in the rest at compile time, making the above line equivalent to:&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_autoprop/oldprop.jpg' /&gt;&lt;br /&gt;&lt;br /&gt;To create a read-only property, just make the setter private:&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_autoprop/propg.jpg' /&gt;&lt;br /&gt;To simplify this even further, there are two code snippets, prop and propg, that generate template properties and let you fill in the name and type. The snippet "prop" generates a read/write property and "propg" generates a read-only property.&lt;br /&gt;&lt;br /&gt;To invoke a snippet, start typing the snippet name and it will appear in the IntelliSense-window.&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_autoprop/vs2008prop.jpg' /&gt;&lt;br /&gt;Pres tab once to auto-complete the snippet name, and tab again to expand the snippet name into the actual snippet.&lt;br /&gt;&lt;br /&gt;&lt;img src='images/upload/code_autoprop/vs2008prop_expanded.jpg' /&gt;</description><pubDate>Wed, 01 Oct 2008 12:20:56 +0200</pubDate><a10:updated>2008-10-01T12:20:56+02:00</a10:updated></item><item><guid isPermaLink="false">9</guid><link>http://www.erikojebo.se/Post.aspx?id=9</link><author>webmaster@erikojebo.se</author><title>Haskell</title><description>As you probably have noticed, functional programming has recently become the new, hot thing, and one of the languages that suddenly has gotten an upswing in publicity is &lt;a href='http://en.wikipedia.org/wiki/Haskell_(programming_language)'&gt;Haskell&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;After listening to a few podcasts on functional programming I decided to play around with Haskell, and came up with the idea to use it for my &lt;a href='http://projecteuler.net'&gt;Project Euler&lt;/a&gt; solutions. A quick search led to the Ubuntu Hardy Heron apt-packages &lt;a href='http://packages.ubuntu.com/gutsy/ghc6'&gt;ghc6&lt;/a&gt; and &lt;a href='http://packages.ubuntu.com/gutsy/haskell-mode'&gt;haskell-mode&lt;/a&gt;, which are the Glasgow Haskell Compiler and an Emacs add-on for Haskell syntax support.&lt;br /&gt;&lt;br /&gt;As always, the apt-installation was quick and simple, so in a matter of minutes I had a nice little Haskell development environment set up. The next step was to read some tutorials at the official &lt;a href='http://www.haskell.org'&gt;Haskell page&lt;/a&gt; and shortly there after I was up and running.&lt;br /&gt;&lt;br /&gt;&lt;img src='http://upload.wikimedia.org/wikipedia/commons/3/30/Fp_no_destructive_assignment.png' class='left' /&gt;I have to say that Haskell must be one of the most elegant languages I have encountered, so far. The pattern matching makes function definitions very simple and easy to read, but the detail I found the most appealing is the list definition syntax. For example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;[a | a &lt;- [1..], a [mod] 3 == 0]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This declares an infinite list of all integers that are evenly divisible by three, which is made possible by Haskell's &lt;a href='http://en.wikipedia.org/wiki/Lazy_evaluation'&gt;lazy evaluation&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;A few days ago I was searching for new sites with video lectures, and to my surprise I found a great &lt;a href='http://haskell.org/haskellwiki/Video_presentations#Introductions_to_Haskell'&gt;Haskell video lecture&lt;/a&gt; from O'Reilly Open Source Convention. The lecture was given by &lt;a href='http://en.wikipedia.org/wiki/Simon_Peyton_Jones'&gt;Simon Peyton Jones&lt;/a&gt; who is one of the guys behind the language. To my even greater surprise I then read that the &lt;a href='http://se-radio.net'&gt;Software Engineering Radio&lt;/a&gt; team had just released a &lt;a href='http://feeds.feedburner.com/~r/se-radio/~3/377820928/episode-108-simon-peyton-jones-functional-programming-and-haskell'&gt;podcast&lt;/a&gt; with Peyton Jones on functional programming and Haskell. Hopefully, more resources like these will pop up in the future.&lt;br /&gt;&lt;br /&gt;If you are up to it, I really recommend playing around with Haskell. As a starting point, I would suggest the short tutorials at &lt;a href='http://www.haskell.org'&gt;Haskell.org&lt;/a&gt; and the video lectures mentioned above.</description><pubDate>Thu, 04 Sep 2008 14:14:56 +0200</pubDate><a10:updated>2008-09-04T14:14:56+02:00</a10:updated></item><item><guid isPermaLink="false">2</guid><link>http://www.erikojebo.se/Post.aspx?id=2</link><author>webmaster@erikojebo.se</author><title>Unit Testing in PHP</title><description>Recently I have begun to adopt a test driven style of development, trying to do it the &lt;a href='http://en.wikipedia.org/wiki/Kent_Beck'&gt;Kent Beck&lt;/a&gt; way, i.e. test first. When I started planning the development of this site I remembered reading about &lt;img src='http://upload.wikimedia.org/wikipedia/commons/2/27/Crystal_Clear_mimetype_php.png' class='right' /&gt;two different unit testing frameworks for PHP, namely &lt;a href='http://phpunit.de'&gt;PHPUnit&lt;/a&gt; and &lt;a href='http://simpletest.org/'&gt;Simpletest&lt;/a&gt;. I decided to start with PHPUnit, and to my delight it was available as an apt package in my beloved &lt;a href='http://www.ubuntu.com/'&gt;Ubuntu&lt;/a&gt;, so the installation went as smooth as only apt installations can.&lt;br /&gt;&lt;br /&gt;I tried it out on the shipped example, a unit test of an array, and was a bit disapointed with the interface. I had envisioned a PHP based framework that you simply ran in your browser, providing a nice HTML GUI. Instead it was a command line utility, that gave a quite sparse feedback, when running the tests. It had some nice features as report generation in various formats and testing code coverage, so it seemed powerful, but lacking the green bar/red bar feedback.&lt;br /&gt;&lt;br /&gt;So, Google to the rescue. I did a quick search and found the documentation for the PHPUnit API basically explaining a nice way of using the API to create a GUI wrapper for PHPUnit. Another search resulted in a source forge project called Cool (&lt;a href='http://cool.sourceforge.net/'&gt;"Cool Object Orientated Library"&lt;/a&gt;, a little GNU-esque there... and it really is "orientated"). This turned out to be just what I was looking for. It was a php site, that provided a nice GUI for PHPUnit. The installation was painless, since all you had to do was to extract so that it is available from the web server.&lt;br /&gt;&lt;br /&gt;I started playing around with Cool and PHPUnit and, unfortunately, it soon became obvious that Cool did not handle test suites that well, or at least that it did not work properly in my set-up. Once again, to the Google-mobile, but no luck, so I decided to give Simpletest a try instead.&lt;br /&gt;&lt;br /&gt;As with Cool, all you had to do to install Simpletest was to extract it to a place that was reachable by your PHP code and you were done. I was glad to find that it shipped with a simple HTML GUI, so this was more in line with what I first had in mind. The documentation was well written and had good examples, so it did not take long to get up and running. Since everything felt solid I decided to use Simpletest for the project.&lt;br /&gt;&lt;br /&gt;Simpletest turned out to be really nice to work with and it has a nice Mocking-framework, that was easy to use. The only down side was the HTML reporter which was very basic, so after a while I decided to write a slightly more advanced reporter. The API used by the reporter classes was documented and since the source for the original HTML reporter was available it was quite a simple job to write a custom reporter.&lt;br /&gt;&lt;br /&gt;So, if you want a light weight, portable testing framework for PHP, I can recommend Simpletest. I will probably post my extended HTML reporter at this site in the near future, if anyone wants to try it out.</description><pubDate>Thu, 04 Sep 2008 13:58:06 +0200</pubDate><a10:updated>2008-09-04T13:58:06+02:00</a10:updated></item></channel></rss>

