Solved

Question about disposing my Entity Model

Posted on 2011-02-23
3
416 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 33
C# Offline Apllication 5 54
Word Directory is not in the drop down list 4 25
Split in Javascript 5 30
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now