title: DI: Handling Life of IDisposable Objects
body: <p>So I'm working on my DI/IoC Container <a href="http://ioc.codeplex.com" rel="noreferrer">OpenNETCF.IoC</a> and I've got a (reasonable) feature request to add some form of lifecycle management for IDisposable items in the container collections.</p>
  
  <p>My current thinking is that, since I can't query an object to see if it's been disposed, and I can't get an event for when it's been disposed, that I have to create some form of wrapper for objects that a developer wants the framework to manage.</p>
  
  <p>Right now objects can be added with AddNew (for simplicity sake we'll assume there's only one overload and there is no Add):</p>
  
  <pre><code>public TTypeToBuild AddNew&lt;TTypeToBuild&gt;() { ... }
  </code></pre>
  
  <p>What I'm considering is adding a new method (well group of them, but you get the picture):</p>
  
  <pre><code>public DisposableWrappedObject&lt;IDisposable&gt; AddNewDisposable&lt;TTypeToBuild&gt;()
      where TTypeToBuild : class, IDisposable
  {
      ...
  }
  </code></pre>
  
  <p>Where the DisposableWrappedObject looks like this:</p>
  
  <pre><code>public class DisposableWrappedObject&lt;T&gt;
      where T : class, IDisposable
  {
      public bool Disposed { get; private set; }
      public T Instance { get; private set; }
  
      internal event EventHandler&lt;GenericEventArgs&lt;IDisposable&gt;&gt; Disposing;
  
      internal DisposableWrappedObject(T disposableObject)
      {
          if (disposableObject == null) throw new ArgumentNullException();
  
          Instance = disposableObject;
      }
  
      ~DisposableWrappedObject()
      {
          Dispose(false);
      }
  
      public void Dispose()
      {
          Dispose(true);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          lock(this)
          {
              if(Disposed) return;
  
              EventHandler&lt;GenericEventArgs&lt;IDisposable&gt;&gt; handler = Disposing;
              if(handler != null)
              {
                  Disposing(this, new GenericEventArgs&lt;IDisposable&gt;(Instance));
              }
  
              Instance.Dispose();
  
              Disposed = true;
          }
      }
  }
  </code></pre>
  
  <p>Now, when an items gets added to the container via AddNewDIsposable, an eventhandler is also added so that when it gets Disposed (via the wrapper) the framework removes it from the underlying collection.</p>
  
  <p>I actually have this implemented and it's passing the unit tests, but I'm looking for opinions on where this might be broken, or how it might be made more "friendly" to the consuming developer.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Since there was a question on how the Disposing event is used, here's some code (trimmed to what's important):</p>
  
  <pre><code>private object AddNew(Type typeToBuild, string id, bool wrapDisposables)
  {
      ....
  
      object instance = ObjectFactory.CreateObject(typeToBuild, m_root);
  
      if ((wrapDisposables) &amp;&amp; (instance is IDisposable))
      {
          DisposableWrappedObject&lt;IDisposable&gt; dispInstance = new
                 DisposableWrappedObject&lt;IDisposable&gt;(instance as IDisposable);
          dispInstance.Disposing += new 
                 EventHandler&lt;GenericEventArgs&lt;IDisposable&gt;&gt;(DisposableItemHandler);
          Add(dispInstance as TItem, id, expectNullId);
          instance = dispInstance;
      }
  
      ....
  
      return instance;
  }
  
  private void DisposableItemHandler(object sender, GenericEventArgs&lt;IDisposable&gt; e)
  {
      var key = m_items.FirstOrDefault(i =&gt; i.Value == sender).Key;
      if(key == null) return;
      m_items.Remove(key);
  }
  </code></pre>