Search code examples
c#asp.net-core-mvcdispose

Why is there no Dispose method to override in .Net Core?


I am trying to dispose of my DbContext in my GenericRep from my Generic Service.

My generic repo is implementing the pattern described here: https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

I have set up the first part in my repo:

public class GenericRepository<T> : IGenericRepository<T>, IDisposable
    where T: BaseEntity
{
    protected readonly ApplicationDbContext _context;
    protected DbSet<T> _dbSet;
    .
    .
    .
    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

Now in the article they have this in the StudentController:

  protected override void Dispose(bool disposing)
  {
     studentRepository.Dispose();
     base.Dispose(disposing);
  }

They don't have StudentController implementing IDisposable or anything. They just seem to be able to override Dispose as a standard overridable method on Object I guess.

So I need to put this in my Generic Service.

Here is my Generic Service:

public class GenericService<T> : IGenericService<T>
    where T : BaseEntity
{
    private IGenericRepository<T> _genericRepo;

    public GenericService(IGenericRepository<T> genericRepo)
    {
        _genericRepo = genericRepo;
    }
    . 
    .
    .
    public override void Dispose(bool disposing)
    {
        _genericRepo.Dispose();
        base.Dispose(disposing);
    }

So I have two red squigglies on my code.

1) No suitable method found to override for GenericService.

Is this a standard overridable object in Full Framework but not .Net Core?

2) Object does not contain a definition for Dispose on base.Dispose call.

Is this just done differently somehow in .Net Core?

UPDATE 1:

Thanks Nkosi for your answer below (Answer 1).

So now I am calling the serviceRepo dispose from my controller like this:

[Route("api/[controller]")]
public class CasesController : Controller, IDisposable
{
    private readonly IGenericService<Case> _caseGenericService;

    public CasesController(IGenericService<Case> caseGenericService)
    {
        _caseGenericService = caseGenericService;
    }
    .
    .
    .
    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _caseGenericService.Dispose();
                base.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

It kind of works, for a few disposes. But then it seems to start hanging.


Solution

  • The generic service will have to define/implement disposable just like in the generic repository. There is nothing to override or base to call as the current class is defining it.

    public class GenericService<T> : IGenericService<T>, IDisposable
        where T : BaseEntity
    {
        private IGenericRepository<T> _genericRepo;
    
        public GenericService(IGenericRepository<T> genericRepo)
        {
            _genericRepo = genericRepo;
        }
        . 
        .
        .
        private bool disposed = false;
        // Protected implementation of Dispose pattern.
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return; 
    
            if (disposing) {
                _genericRepo.Dispose();
                // Free any other managed objects here.
                //
            }
    
            // Free any unmanaged objects here.
            //
            disposed = true;
        }    
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
    

    Reference Implementing a Dispose Method