When to call Dispose in Entity Framework?

Posted by Abdel Olakara on Stack Overflow See other posts from Stack Overflow or by Abdel Olakara
Published on 2010-05-01T13:35:57Z Indexed on 2010/05/01 13:37 UTC
Read the original article Hit count: 275

Filed under:

Hi All,

In my application I am making use of Spring.Net for IoC. The service objects are called from the ASP.Net files to perform CRUD operations using these service object. For example, I have CustomerService to do all CRUD operations on Customer table. I use entity framework and the entities are injected .. my question is where do I call the dispose method?

As far as I understood from the API documentations, unless I call Dispose() there is no guaranty it will be garbage collected! So where and how do I do it?

Example Service Class:

 public class CustomerService
{
    public ecommEntities entities = {get; set;}

    public bool AddCustomer(Customer customer)
    {
        try
        {
            entities.AddToCustomer(customer);
            entities.SaveChanges();
            return true;
        }
        catch (Exception e)
        {
            log.Error("Error occured during creation of new customer: " + e.Message + e.StackTrace);
            return false;
        }            
    }

    public bool UpdateCustomer(Customer customer)
    {
        entities.SaveChanges();
        return true;
    }

public bool DeleteCustomer(Customer customer)
.
.
.   

And I just create an object of CustomerService at the ASP partial class and call the necessary methods.

Thanks in advance for the best practice and ideas..

Regards,

Abdel Raoof

© Stack Overflow or respective owner

Related posts about entity-framework