title: Programming against multiple interfaces
body: <p>I like very much the hint: "Program against an interface, not an implementation" and I am trying to follow it consistently. However I am in doubt how to keep this principle working when I have to decouple my code from objects that <em>must</em> inherit from several interfaces. A typical example could be:</p>
  
  <pre><code>namespace ProgramAgainstInterfaces
  {
      interface IMean
      {
              void foo();
      }  
  
      class Meaning : IMean , IDisposable
      {
          public void Dispose()
          {
              Console .WriteLine("Disposing..." );
          }
  
          public void foo()
          {
              Console .WriteLine("Doing something..." );           
          }
      }
  
     class DoingSomething
     {
          static void Main( string[] args)
          {
              IMean ThisMeaning = (IMean ) new Meaning ();  // Here the issue: I am losing the IDisposable methods
              ThisMeaning.foo();
              ThisMeaning.Dispose();                     // Error: i cannot call this method losing functionality
          }
     }   
  }
  </code></pre>
  
  <p>A possible way to solve this could be to define an ad-hoc interface that inherits from both the interfaces:</p>
  
  <pre><code>namespace ProgramAgainstInterfaces
  {
      interface IMean
      {
              void foo();
      }
  
      interface ITry : IMean , IDisposable
      {
      }
  
      class Meaning : ITry
      {
          public void Dispose()
          {
              Console .WriteLine("Disposing..." );
          }
  
          public void foo()
          {
              Console .WriteLine("Doing something..." );           
          }
      }
  
     class DoingSomething
     {
          static void Main( string[] args)
          {
              ITry ThisMeaning = (ITry ) new Meaning ();  // This works
              ThisMeaning.foo();
              ThisMeaning.Dispose();   // The method is available
          }
     }   
  }
  </code></pre>
  
  <p>but i am not sure if this is the more compact and effective solution: I could have more complex multiple inheritance hierarchies and this add complexity because I must create interfaces only to act as containers. There is a better design solution?</p>