In the course of making my software more testable, I’ve attempted to eliminate a dependency on the file system (in this case, via System.IO) by creating an interface, IFileSystem. I just did a quick search for this term and came back with only one C# interface (in the first few results) that matches this, which is for CC.NET, and looks like this:
My basic version of the interface is similar:
From this, I was able to extract some simple System.IO dependencies that were detecting if a file existed and moving it. This made my unit tests much simpler, as they no longer depended on certain paths being configured for the tests to work (though integration tests might still require this). Once the interface was extracted, it was a simple matter to create a WindowsFileSystem class that implemented the interface:
This worked great for my code that simply needed to detect if a file existed and if so, move it, as well as for some email template code that simply needed to read in templates from disk. However, my next step is to try and get web-based file uploads working. This is a bit more of a challenge because I need to work with(or rather, around) the HttpPostedFile class. Here’s the code currently, which is living in a user control’s codebehind. My next step is going to be to extend my IFileSystem interface to support DirectoryExists, CreateDirectory, and SaveFile operations. The real trick that’s coming, though, is that once I’ve done this my next step is going to be to write an Amazon S3 IFileSystem implementation – so I want to be sure my interface doesn’t automatically assume that I’m working with a standard disk-based file system.
Current Ugly Code:
Continue reading about Dependency Inversion in Part 2.