This project is read-only.

Closing EF contexts in IDinnerRepository?

Sep 3, 2010 at 4:36 PM
Edited Sep 3, 2010 at 4:37 PM

Hi there,

Im wondering if anyone could tell me if we should close / dispose the ObjectContext after usage or not?
What is best practices here, i was under the impression that we should close an object context as soon as we have used it.
When i look in NerdDinner.Models.DinnerRepository class they create the EF context as soon as an instance of the repository classs are instantitated.

 public class DinnerRepository : NerdDinner.Models.IDinnerRepository

        NerdDinnerEntities db = new NerdDinnerEntities();

But, i never see that this ObjectContext are closed explictly like if you had used:

 using (NerdDinnerEntities db = new NerdDinnerEntities())
                return db.Dinners.Where(d => d.Title.Contains(q)
                           || d.Description.Contains(q)
                           || d.HostedBy.Contains(q));
So what is best practices here? Isnt it important to close this ObjectContext so
resources are released as fast as possible?
Mar 8, 2011 at 2:57 PM

I find this question quite interesting. Of course what you suggested is not possible because the returned IQueriable cannot be executed when the context is disposed. However not disposing the context at all seems strange.