title: Multi-Hierarchy IDisposable Implementation for DbContext in C#
body: <p>Base class:</p>
  
  <pre><code>public abstract class Repository : IDisposable
  {
      private bool _disposed;
      private DbContext _context;
      public Repository(DbContext context)
      {
          _context = context;
      }
  
      public void SetSomething()
      {
          //...Access the database and set something for tracing
          _context.Database.SqlQuery(....);
      }
  
      public void UnSetSomething()
      {
          //...Access the database and cancel something for tracing
          _context.Database.SqlQuery(....);
      }
  
      #region Object Disposal
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      ~Repository()
      {
          Dispose(false);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (_disposed)
              return;
  
          if (disposing)
          {
              // free other managed objects that implement IDisposable only
              if (_context != null)
              {
                  _context.Dispose();
                  _context = null;
              }
          }
  
          _disposed = true;
      }
      #endregion
  }
  </code></pre>
  
  <p>Sub Class:</p>
  
  <pre><code>public class ScheduleRepository : Repository
  {
      private AppContext _context;
  
      public ScheduleRepository(DbContext context)
          : base(context)
      {
          _context = (AppContext)context;
      }
  
      #region Object Disposal
      bool _disposed;
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      ~ScheduleRepository()
      {
          Dispose(false);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (_disposed)
              return;
  
          if (disposing)
          {
              // free other managed objects that implement IDisposable only
              if (_context != null)
              {
                  _context.Dispose();
                  _context = null;
              }
          }
  
          _disposed = true;
  
          base.Dispose(disposing);
      }
      #endregion
  }
  </code></pre>
  
  <p>Logic Class:</p>
  
  <pre><code>public class ScheduleFacade
  {
      private ScheduleRepository _repository;
      public ScheduleFacade()
      {
          _repository = new ScheduleRepository(AppContext.Create());
      }
  
      public ScheduleSetting GetScheduleById(string scheduleId)
      {
          if (!string.IsNullOrEmpty(scheduleId))
          {
              _repository.SetSomething();
  
              ScheduleSetting settings = _repository.GetScheduleById(scheduleId);
  
              _repository.UnSetSomething();
  
              return LoadScheduleSettings(settings);
          }
          else
          {
              throw new ArgumentException("The scheduleId parameter cannot be empty.");
          }
      }
  
      private ScheduleSetting LoadScheduleSettings(ScheduleSetting settings)
      {
          //...code ....
      }
  }
  </code></pre>
  
  <p>Is this the correct way to implement <code>IDisposable</code> on an abstract class implementation? This is not following the DRY principals like it should but I am unclear how to do this properly.</p>
  
  <p>I want to make sure that I am cleaning up my <code>DbContext</code> appropriately.</p>
  
  <p>EDIT: It appears that more information is needed to clarify what I am doing and why I am passing in DbContext in the constructor (I have added more code above to please re-read).  I needed the DbContext in the abstract class to access the database and do some work.  Isn't this how I would use an abstract class that is shared among multiple sub classes and thus allowing me to adhere to the DRY principal and centralize future maintenance?  </p>
  
  <p>How would I pass the DbContext to the abastract class if I dont pass it through the contstructor (method injection comes to mind but that would require that devs of future repositories might forget to pass the context to the base class).</p>