title: Singletons to facilate unit tests in a legacy code base. A good idea or not?
body: <p>Folks,
      I've a large legacy .Net code base, and I'm trying to introduce Unit Testing to the team. They're good guys but this is all new to them (to be honest it's reasonably new to me too).</p>
  
  <p>One of the problems is the code base makes heavy use of static classes in System.IO, there are extensive in-house libraries of static classes and classes aren't written to interfaces (unless there's an actual design reason to do so).</p>
  
  <p>I'm developing an ease-it-in strategy using NUnit and FakeItEasy.</p>
  
  <p>To address the static class dependencies I've written a tool that generates wrapper classes and interfaces for existing Static Classes. e.g. In a config file I say I want wrappers for <code>System.IO Directory &amp; File</code>, the tool generates an assembly with code along the lines of . . .</p>
  
  <pre><code>public interface IFile
  {
      // All Method signatures taken from System.IO.File
  }
  
  internal class File
      : IFile
  {
      // All Methods delegate to System.IO.File
  }
  
  public interface IIO
  {
      IFile File {get;}
      IDirectory Directory {get;}
  }
  
  internal class IO
      : IIO
  {
      public IFile File {get; private set;}
      public IDirectory Directory {get; private set;}
      public IO()
      {
          File = new File();
          Directory = new Directory();
      }
  }
  
  
  public static class IO
  {
      public IIO Instance {get; set;}
      static IO()
      {
          Instance = new IO();
      }
  }
  </code></pre>
  
  <p>The idea is that in the existing code base can be simply updated by a find/replace and adding a new project reference, and that in unit tests you can assign a Mock object to <code>IO.Instance</code>.</p>
  
  <p>One one hand I'm quite happy with this approach, it gives a clean and fast way to move away from using static classes directly, plus gives us a way to mock those static classes and lets us test code that depends on them.</p>
  
  <p>On the other hand, I kind of feel I've done a deal with the devil, I've replaced dependencies on implicit singletons with dependencies on <strong>explicit</strong> singletons.</p>
  
  <p>Is this all going to go horribly wrong for me sometime in the near future, or do you think this is a workable approach?</p>