Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ-To-SQL Business-Layer Object DataContext
    text
    copied!<p>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.</p> <p>So I did some searching for better alternatives, and found <a href="http://www.west-wind.com/WebLog/posts/160237.aspx" rel="nofollow noreferrer">Rick Strahl's post</a> about "per object" scenario. So each of my objects would have a DataContext local to the class.</p> <p>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.</p> <p>Something like this..</p> <pre><code> Customer cust = new Customer(); cust = cust.GetCustomer(primaryKeyID); // gets data using LINQ-To-SQL </code></pre> <p>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.</p> <p>Sample class code:</p> <pre><code> public partial class Customer { MyDataContext db = new MyDataContext(Settings.MyConnectionString); public Customer GetCustomer(Int64 custID) { return db.Customers.SingleOrDefault(c =&gt; c.ID == custID); } public Customer AddCustomer(Customer c) { db.Customers.InsertOnSubmit(c); db.SubmitChanges(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload