c# - LINQ-To-SQL Business-Layer Object DataContext -


Basically, I was using my datacentact object as a global singleton. While using this in ASP.NET, I was running into disputes due to the multi-threaded nature of ASP.NET.

So I searched something for better options, and the "per object" scenario. Therefore, in each of my objects, there will be local datacontact for the class.

So I have found most of these, but my question comes in an attempt to get an example of the object. Since there are examples of ways in all methods, I have to meet an example of class first.

  Customer customer = new customer ();  

Cust = cust.GetCustomer (PrimaryKIID); // data is obtained using LINQ-to-SQL

This is just a way to call back a real example for me to make an example of class Looks redundant Is this the right way to do it? I think there is a different way that still follows the method in his blog post that uses Rick.

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 Customers AddCustomer (Customer C) {db.Customers.InsertOnSubmit (c); Db.SubmitChanges (); }}  

Without commenting on the whole context, only the solution to the question about redundancy To-do-per-view or Rick's code, which I have not reviewed,

you have two lines ending with

  var customer = new customer () can do. GetCustomer (PrimaryKeyId); Alternatively, create a factory that acts as a stable gateway:  
  public static class CustomerFactory {public static customer BuildCustomerWithId (_ & lt; int / short / long & gt; _ Primary KIID) {var customer = new customer (); Return customer Gate customer (primary kiid); }}  

This not only clears your object creation (you are now called (1); with only var customer = customerfire.bull customer , But now you can modify the BuildCustomerWithId () method, without changing consumer classes, for example, if you need to, for example, you can later decide that you can use the data contantex in business objects Do not like to instantiate, and you can refactor it all out Can. You can instantiate DataContext in the factory instead, and you do not have to change any code calling BuildCustomerWithId () . The stable gateway unit makes the test a little more challenging, but it is much easier to directly orient the customer objects.

I know that it does not solve the problem of the first institute and then the Calling Recipient Method, but clearly, I do not see it as a problem from the point of view - only syntax / readability In context.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -