title: How does this implementation of IDisposable work?
body: <p>I'm about to build my first n-tier web application, and as such, I'm educating myself on the principles, and looking at example code. I came across a great example of this type of application here: <a href="https://github.com/MarlabsInc/SocialGoal" rel="nofollow">https://github.com/MarlabsInc/SocialGoal</a>
  However, I'm confused about how the EF Context on the data layer is disposed. Up to this point, I've disposed of my context, either by, for example, utilizing a using block, or overriding the Dispose method on my MVC Controllers. In this application, however, the Dispose method is never explicitly called, instead the factory class that instantiates the context is derived from a base class that implements IDisposable: </p>
  
  <p>Factory class:</p>
  
  <pre><code>public class DatabaseFactory : Disposable, IDatabaseFactory
  {
      private SocialGoalEntities dataContext;
      public SocialGoalEntities Get()
      {
          return dataContext ?? (dataContext = new SocialGoalEntities());
      }
      protected override void DisposeCore()
      {
          if (dataContext != null)
              dataContext.Dispose();
      }
  }
  </code></pre>
  
  <p>Disposable class:</p>
  
  <pre><code>public class Disposable : IDisposable
  {
      private bool isDisposed;
  
      ~Disposable()
      {
          Dispose(false);
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
      private void Dispose(bool disposing)
      {
          if (!isDisposed &amp;&amp; disposing)
          {
              DisposeCore();
          }
  
          isDisposed = true;
      }
  
      protected virtual void DisposeCore()
      {
      }
  }   
  </code></pre>
  
  <p>With a newbie's eyes, this looks like the Context will never be disposed. The only way <code>Dispose(bool)</code> is called, is from the finalizer. And since this passes <code>false</code> as parameter, the virtual <code>DisposeCore()</code> method is never called, and since this has to be called for the Context to dispose, it never will. Am I correct in thinking this, or am missing some key piece of .NET knowledge?</p>