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.
Here’s the source for the DirectoryInfoExtensions class.