KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I’m using Linq to Entities and lately, I found that a lot of folks recommending wrapping the datacontext in a using statement like this: Using(DataContext db = new DataContext) { var xx = db.customers; } This makes sense. However, I’m not sure how to incorporate this practice in my model. For example: I have an interface (let’s call it customer) and it is implemented by a repository like this: namespace Models { public class rCustomer : iCustomer { readonly DataContext db = new DataContext(); public customer getCustomer(Guid id) { return db.customers.SingleOrDefault(por => por.id == id); } public iQueryable<customer> getTopCustomers() { return db.customers.Take(10); } //******************************************* //more methods using db, including add, update, delete, etc. //******************************************* } } Then, to take the advantage of using, I will need to change the methods to look like this: namespace Models { public class rCustomer : iCustomer { public customer getCustomer(Guid id) { using(DataContext db = new DataContext()) { return db.customers.SingleOrDefault(por => por.id == id); } } public iQueryable<customer> getTopCustomers() { using(DataContext db = new DataContext()) { return db.customers.Take(10); } } //******************************************* //more methods using db //******************************************* } } My question is: the recommendation of using “Using” is really that good? Please take in consideration that this change will be a major one, I have about 25 interfaces/repository c
Tags (comma-separated)
Save Edits
Cancel