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 8645461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:33:28+00:00 2026-06-12T12:33:28+00:00

The microsoft tutorial http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application advises to implement dispose pattern, like this: private bool disposed

  • 0

The microsoft tutorial http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application advises to implement dispose pattern, like this:

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);
        }

Why i should do it, why i cannot simply dispose context and enough, what happens if i will use only:

context.Dispose()

Which goals of implementation of Microsoft’s dispose pattern?

  • 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-12T12:33:29+00:00Added an answer on June 12, 2026 at 12:33 pm

    You can just use…

        public void Dispose() // IDisposable implementation
        {
            context.Dispose();
        }
    

    …without virtual Dispose overload and without private disposed flag because

    • the context itself checks if Dispose has already been called so that nothing happens on a second call and no exception will be thrown
    • the context class has its own finalizer that will ensure that the database connection is released on garbage collection if you didn’t call Dispose explicitly

    The last point doesn’t mean btw that you don’t need to call context.Dispose() at all because the point in time when the garbage collector will finalize the context is indeterministic and it might be later than the point when you have created a new context instance and use it possibly with the same entities – which can cause some trouble. So: Always dispose a context explicitly or by a using block.

    I also have doubt that GC.SuppressFinalize(this); has any effect here because you don’t have a finalizer in your class nor in a base class, so there is nothing to suppress.

    In my opinion the pattern in your example is a fragment (that misses a finalizer/destructor implementation) and that might be useful if you have to deal with your own unmanaged resources in your class. But for your UnitOfWork class you don’t have to. The unmanaged resource (database connection) is managed by the context and you only need to delegate the work to it by calling context.Dispose().

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

Sidebar

Related Questions

I decided to learn MVC so I am going through this tutorial, http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part2-cs .
I am using the Data Annotation Validator, outlined here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx The Data Annotations Model
I am following the Microsoft music store tutorial for asp.net mvc 3 using visual
I have been working through Microsoft's ASP.NET MVC tutorials, ending up at this page
Using this tutorial: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide I can get a Directory Listing to display when I
I've followed this tutorial to try to add a template to Visual Studio: http://www.switchonthecode.com/tutorials/visual-studio-how-to-create-item-templates
essentially what I'm trying to do is get my head around Microsoft's ASP.NET MVC
I'm using the following to surface data from Oracle: http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/EntityFrameworkWCF/WCFEntityFramework.htm I have 2 WCF
I'm making my way through the early Data Access Tutorials on Microsoft's ASP.NET website
I am doing the following tutorial http://msdn.microsoft.com/en-us/library/ms731835%28v=vs.100%29.aspx and the program is working perfectly fine.

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.