Solved

Question about disposing my Entity Model

Posted on 2011-02-23
3
399 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

11 Experts available now in Live!

Get 1:1 Help Now