Tuesday, January 13, 2015

Let's get nasty!

public void SaveGroceryList(List<string> list, SqlServer sqlServer)
{
   using (sqlServer)
   {
      sqlServer.DoWhatever(list);
   }
}

 
 

If SqlServer is a type that inherits from IDisposable and you put it behind a second interface in the name of inversion of control, preexisting using statements around SqlServer, such as the one above, will need to be rewritten like so:

public void SaveGroceryList(List<string> list, ISqlServer sqlServer)
{
   try
   {
      sqlServer.DoWhatever(list);
   }
   catch(Exception exception)
   {
      throw;
   }
   finally
   {
      sqlServer.Dispose();
   }
}

 
 

This may mean there is no reason anymore to inherit from IDisposable at all. It really depends on whether or not anything in SqlServer's own project make a using statement with SqlServer. Implementations of such a shape likely will NOT need to be abstracted behind an interface.

Addendum 1/14/2015: It turns out that this blog posting is nasty with stupidity. One may just make an interface inheirt from IDisposable to get around this whole problem. Please see: this

No comments:

Post a Comment