title: C# Best way to wrap calls to an interface?
body: <p>I am working with legacy code, and converting single-instance implementations into multiple instance. </p>
  
  <p>The basic example below illustrates what I am doing - </p>
  
  <ol>
  <li>Creating a object that wraps each call of an interface with a
  disposable mutex (to give a nice using{...} block), and</li>
  <li>passing an instance of an object that uses the interface into an
  instance of the wrapper object.</li>
  </ol>
  
  <p>code here:</p>
  
  <pre><code>    // An Interface
  public interface IImplementation
  {
      void DoSomething();
  
      double MeasureSomething();
  }
  
  // An implementation
  public class Implementation : IImplementation
  {
      private int iparam;
      private string sparam;
  
      public Implementation(string sparam, int iparam)
      {
          this.sparam = sparam;
          this.iparam = iparam;
      }
  
      public void DoSomething()
      {
          //actually do something here
      }
  
      public double MeasureSomething()
      {
          double value = 0.0;
          //actually measure something here
          return value;
      }
  }
  
  // a disposable safety wrap object
  public class SafetyWrap : IDisposable
  {
      public SafetyWrap(string resource)
      {
          mutex = new Mutex(false, resource);
          try
          {
              if (mutex.WaitOne(Timeout.Infinite) == false)
                  throw new TimeoutException("Timed out trying to acquire lock");
          }
          catch (AbandonedMutexException)
          {
              // Log "Caught abandoned mutex"
          }
      }
  
      private Mutex mutex;
  
      #region IDisposable Members
  
      private bool disposed = false;
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      private void Dispose(bool disposing)
      {
          if (!disposed)
          {
              if (disposing)
              {
                  mutex.ReleaseMutex();
                  mutex.Close();
              }
              disposed = true;
          }
      }
  
      ~SafetyWrap()
      {
          Dispose(false);
      }
  
      #endregion IDisposable Members
  }
  
  // a wrapper class to protect the object
  public class WrappedImplementation : IImplementation
  {
      private IImplementation objectToWrap;
      private string mySafetyString;
  
      public WrappedImplementation(IImplementation objectToWrap)
      {
          this.objectToWrap = objectToWrap;
          mySafetyString = "GeneratedToPreventConflicts";
      }
  
      #region IImplmentation Members
  
      public void DoSomething()
      {
          using (new SafetyWrap(mySafetyString))
          {
              objectToWrap.DoSomething();
          }
      }
  
      public double MeasureSomething()
      {
          using (new SafetyWrap(mySafetyString))
          {
              return objectToWrap.MeasureSomething();
          }
      }
  
      #endregion IImplmentation Members
  }
  
  internal class Program
  {
      private static void Main(string[] args)
      {
          IImplementation normalObject = new Implementation("dummy", 1);
  
          IImplementation safeObject = new WrappedImplementation(normalObject);
      }
  }
  </code></pre>
  
  <p>This however involves creating a lot of boilerplate (I have in, actual fact, many interfaces to implement) and it still doesn't guarantee that they can and/or will be used everywhere that they could/should.</p>
  
  <p>Is there a better alternative way to achieve this aim?</p>