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
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
Archives
May 2014
2014