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

徘徊边缘 提交于 2021-01-05 07:40:20

问题


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://docs.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.


回答1:


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



来源:https://stackoverflow.com/questions/43294927/why-is-there-no-dispose-method-to-override-in-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!