Solved

Question about disposing my Entity Model

Posted on 2011-02-23
3
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 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