Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Question about disposing my Entity Model

Posted on 2011-02-23
3
Medium Priority
?
456 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 1000 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 1000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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