Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:00:06+00:00 2026-06-04T23:00:06+00:00

My Controllers inherit from a class that contains a field that implements IDisposable. So

  • 0

My Controllers inherit from a class that contains a field that implements IDisposable. So my first instinct was to write:

public abstract class EventRepositoryControllerBase : Controller
{
    protected EventRepository eventRepos { get; private set; }

    public EventRepositoryControllerBase(EventRepository eventRepos)
    {
        this.eventRepos = eventRepos;
    }

    public override void Dispose()
    {
        try
        {
            base.Dispose();
        }
        finally
        {
            eventRepos.Dispose();
        }
    }
}

but this won’t compile because Controller does not mark the Dispose method as virtual/override. So now I think I’m stuck. Even if I mark my method as new, won’t the framework hold references typed as Controller and as such my method will never be invoked? Suggestions on how to work around this?

UPDATE

So I looked at the MVC3 source and saw this:

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

    protected virtual void Dispose(bool disposing) {
    }

So I guess I’ll just put my code in the 2nd method. Not sure that this behavior is contractually specified though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:00:11+00:00Added an answer on June 4, 2026 at 11:00 pm

    If IDisposable.Dispose were implemented with an unsealed method, then the code for derived types would run before and after the base-type cleanup code; since GC.SuppressFinalize shouldn’t be called until after a derived type has finished its cleanup (including any portions that should perhaps occur after the base type has finished its cleanup), the GC.SuppressFinalize is made in a sealed implementation which in turn calls a virtual method whose signature is void Dispose(bool).

    Note that while conceptually it is a good idea to have a virtual method called within a sealed wrapper, there are some flaws in Microsoft’s implementation. Most notably:

    1. Because it uses a private flag in the base class indicate whether the object has yet been disposed, but does not use that flag in the wrapper method, every derived class that wishes to avoid duplicate disposal must also have its own disposal flag;
    2. There are three scenarios by which a derived `Dispose` call could exit:
      1. It could return normally
      2. It could throw an exception, but nonetheless have achieved everything that can be achieved such that finalization should nonetheless be suppressed (e.g. because an `IDisposable` logging object wrapped a file, and the file data was not written successfully on closure). An ugly situation, but one which isn’t helped by leaving the object registered for finalization.
      3. It could throw an exception, in a scenario where the object should remain eligible for finalization.

      Microsoft’s `Dispose` pattern provides no means of distinguishing among the latter two choices. While leaving an object registered for finalization-based cleanup might in many cases be harmless, it could cause confusion if the `Finalize` method is used to log failures to call `Dispose`.

    3. It implies that objects should often hold some resources that they clean up via finalization and some resources that are only cleaned up via `Dispose`, and that unsealed types which don’t clean up anything via finalization should make provision for derived types which might. In practice, classes with finalizers should avoid holding references to any objects not needed for finalization; resources which need finalization should be encapsulated into their own class objects, which should then be held by classes that wouldn’t have to worry about finalizing them.

    Back before Microsoft had figured out the best ways to handle unmanaged resources, the Dispose pattern was a good first effort. Today, I would think it better to regard the virtual method’s parameter as being a dummy parameter used to change the signature, than as something meaningful (even though one should always pass True when chaining the virtual method from the wrapper).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a abstract controller class that I all my controllers inherit from.
I have a BaseController that all Controllers inherit from. The CaseController has the [Authorize]
I've built a Base Controller that all of my Controllers inherit from, and I've
I'm using some namespaced controllers that also inherit from a parent controller. In each
I have a number of Controllers in my project that all inherit from a
I have a base controller abstract class, which my other controllers inherit. This class
I have two view controllers that allow changes to the Address Book. The first
I have the following code: public abstract class BaseController : Controller { public IUserService
public abstract class MyControllerBase : Controller { protected override void OnActionExecuting(ActionExecutingContext context) { //
I have a library base class ( Controller ) and three sub-classes that inherit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.