Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:02:09+00:00 2026-05-13T13:02:09+00:00

Originally, I was using my DataContext object as a global singleton. When using this

  • 0

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();
      }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:02:09+00:00Added an answer on May 13, 2026 at 1:02 pm

    To address only the question about redundancy, without commenting on the whole context-per-object philosophy or Rick’s code, which I haven’t reviewed,

    You can eliminate the two lines with

    var customer = new Customer().GetCustomer(primaryKeyId);
    

    Alternatively, create a factory that acts as a static gateway:

    public static class CustomerFactory
    {
       public static Customer BuildCustomerWithId(_<int/short/long>_ primaryKeyId)
       {
          var customer = new Customer();
          return customer.GetCustomer(primaryKeyId);
       }
    }
    

    Not only does this clean up your object creation (now you just call var customer = CustomerFactory.BuildCustomerWithId(1);), but now you can modify the BuildCustomerWithId() method, if necessary, without changing the consuming classes. For example, you might later decide you don’t like instantiating the DataContext in the business objects, and you refactor it all out. You can instantiate the DataContext in the factory instead, and you don’t have to change any code that calls BuildCustomerWithId(). Static gateway makes unit testing slightly more challenging, but still much easier than instantiating Customer objects directly.

    I realize that this doesn’t solve the problem of first instantiating and then calling the getter method, but frankly, I don’t see that as a problem from a performance standpoint — only in terms of syntax/readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Instead of having a return value you could try passing… May 13, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer It works for me, though I'm using jQuery (1.4.1) from… May 13, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer This is the simple old school approach that works with… May 13, 2026 at 11:19 pm

Related Questions

Ok, the short version is that I have a Linq entity from a LinqDataSourceUpdateEventArgs
Ok, I am using Tortoise Subversion in Vista and using VisualSVN on a Windows
I'm doing a raytracer hobby project, and originally I was using structs for my
originally i was using easyPHP (windows) then i switch to mac and used MAMP.
This XML file contained archived news stories for all of last year. I was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.