A couple of weeks ago at the Wellington .NET User Group we had a very interesting presentation from Patrick, one of the fine TradeMe developers, talking about the use of simple “value objects” to represent distinct kinds of information.

This presentation came to mind today, when I caught myself passing folder paths around some code as strings instead of using DirectoryInfo instances. Converting the code to use DirectoryInfo values instead of the strings not only made the code simpler and easier to read, it highlighted a couple of bugs well before I had a chance to test the code in question.

Even better, I found a couple of places were a simple extension method could make the code even simpler, allowing me to adopt a more declarative style (for a couple of lines at least).

For example, the FolderForEnvironment method returns the location in which data files for a particular environment should be saved for later processing. The EnsureExists() method will automatically create the folder if it doesn’t already exist, allowing the rest of the routine to rely on the folders existence.

var referenceFolder = FolderForEnvironment(ReferenceSystem).EnsureExists();
var targetFolder = FolderForEnvironment(TargetSystem).EnsureExists();

Here’s the source for the DirectoryInfoExtensions class.

/// <summary>
/// Extension methods for DirectoryInfo
/// </summary>
public static class DirectoryInfoExtensions
{
    /// <summary>
    /// Enforce the existence of a directory, throwing an exception if it is missing
    /// </summary>
    /// <param name="directory">DirectoryInfo for the directory to test</param>
    /// <returns>Directory parameter, for chaining.</returns>
    /// <exception cref="DirectoryNotFoundException">If the directory is missing</exception>
    public static DirectoryInfo MustExist(this DirectoryInfo directory)
    {
        if (!directory.Exists)
        {
            throw new DirectoryNotFoundException(directory.FullName);
        }

        return directory;
    }

    /// <summary>
    /// Ensure the existence of a directory, creating it if necessary
    /// </summary>
    /// <param name="directory">DirectoryInfo for the directory to test</param>
    /// <returns>Directory parameter, for chaining.</returns>
    public static DirectoryInfo EnsureExists(this DirectoryInfo directory)
    {
        if (!directory.Exists)
        {
            directory.Create();
        }

        return directory;
    }
}

Comments

blog comments powered by Disqus
Next Post
Language Extensions for C#  19 May 2014
Prior Post
Visual Studio and IIS Autoconfiguration  23 Mar 2014
Related Posts
Avoid hardcoded wait times  26 May 2018
Guaranteed Progression  19 May 2018
Prefer declarative method names  03 Feb 2018
Using Premeditation  27 Jan 2018
With Relocation  20 Jan 2018
Using Consolidation  13 Jan 2018
Using Semantic Types  06 Jan 2018
Using Extension Methods  30 Dec 2017
Error Methods  25 Nov 2017
Pass implementations, not representations  14 Oct 2017
More smart-code posts »
Archives
May 2014
2014