Originally, I was using my DataContext object as a global singleton. When using this in ASP.Net, I was running into issues with contention because of the multi-threaded nature of ASP.Net.
So I did some searching for better alternatives, and found Rick Strahl's post about "per object" scenario. So each of my objects would have a DataContext local to the class.
So I have most of it figured out, but my question comes up when trying to get an instance of the object. Since all of the methods are instance methods, I need to get an instance of the Class first. Then I can use that instance to call the instance method to get the object that I want.
Something like this..
Customer cust = new Customer();
cust = cust.GetCustomer(primaryKeyID); // gets data using LINQ-To-SQL
This just seems redundant to me to create an instance of the class just to call a method to return the actual instance that I want. Is this the correct way of doing this? I would think there is a different way that still adheres to the methodology that Rick uses in his blog post.
Sample class code:
public partial class Customer
{
MyDataContext db = new MyDataContext(Settings.MyConnectionString);
public Customer GetCustomer(Int64 custID)
{
return db.Customers.SingleOrDefault(c => c.ID == custID);
}
public Customer AddCustomer(Customer c)
{
db.Customers.InsertOnSubmit(c);
db.SubmitChanges();
}
}