Solved

Question about disposing my Entity Model

Posted on 2011-02-23
3
429 Views
Last Modified: 2012-05-11
I create my RDDBDataStore very very often and want to be certain I am disposing it correctly.

In this code, RDDB is the object created for me by the Entity Framework (4.0) with the RDDB.edmx file.

Is this code correct to dispose of it? I set a breakpoint on Dispose() but it never reach it. How can I test this?  Is this correct?

Thanks,
newbieweb
public RDDBDataStore()
        {
            rddbEntities = new RDDB();
        }

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

        protected void Dispose(bool disposing)
        {
            if (disposing)
            {
                rddbEntities.Dispose();
                GC.SuppressFinalize(this);
            }
        }

Open in new window

0
Comment
Question by:newbieweb
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
ID: 34963954
I'm not familiar with the Entity Framework, but the IDisposable pattern typically will look like this:

class DisposableObject : IDisposable
{
	private bool disposed = false;

	// Override Object.Finalize in VB.Net, use destructor syntax in C#
	~DisposableObject()
	{
		Dispose(false);
	}

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

	protected virtual void Dispose(bool disposing)
	{
		if (!disposed)
		{
			disposed = true;

			if (disposing)
			{
				// Clean-up managed resources only if .Dispose()
				// was explicitly called
			}
				// If disposing is false then we're running as
				// a result of the Garbage Collector running
				// our finalizer, it's only safe to clean
				// up un-managed resources in this case
		}
	}
}

Open in new window


If left to it's own devices, the garbage collector will eventually run the finalizer/destructor for this object, which in turn just calls the protected Dispose.  You can also call Dispose() yourself, or preferably use this object inside a "using" statement, instead of waiting for the GC.
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 250 total points
ID: 34978953
Controller itself implements IDisposable. So you can override Dispose and dispose of anything (like an object context) that you initialize when the controller is instantiated.
Usually Dispose is called when the object completes its scope of execution for clean up.

Check out this links:
http://stackoverflow.com/questions/4579056/disposing-of-object-context-in-entity-framework-4
http://stackoverflow.com/questions/1401327/entity-framework-how-should-i-instance-my-entities-object
http://stackoverflow.com/questions/4295975/repository-pattern-in-entity-framework-4-when-should-we-dispose
0
 

Author Closing Comment

by:newbieweb
ID: 34979235
Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question