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

  • SEARCH
  • Home
  • 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 732549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:09:17+00:00 2026-05-14T07:09:17+00:00

what is the preferred practice when linq2sql using (in asp.net mvc applications): to create

  • 0

what is the preferred practice when linq2sql using (in asp.net mvc applications): to create “singleton” for DataContext like:

partial class db
{
    static db _db = new db(global::data.Properties.Settings.Default.nanocrmConnectionString, new AttributeMappingSource());

    public static db GetInstance()
    {
        return _db;
    }
}

or to retrieve new instance when it needed within using:

using (db _db = new db())
{
    ...
}

the usage of using brings some limitations into code. so I prefer to use singleton one. is it weird practice?

UPD:
explanation why i’m using singleton:

public class UserGroupRepository
{
    public static IQueryable<Group> RolesFor(string username)
    {
        User user = UserRepository.WithUsername(username);

        return from g in db.GetInstance().Groups
                join ug in db.GetInstance().UsersGroups on g.Id equals ug.GroupId
                where ug.UserId == user.Id
                select g;
    }
}

i have this method. due to it returns IQueryable – I can continue composing query without it executing, so here just lazy result returns.
if i rewrite the same code with using – i cannot be able to return IQueryable (because db will be disposed and IQueryable will be lost too), and i would change it to List. and now this method will return “huge” List from which i will filter data on previous function.

i hope i describe enough detailed.

  • 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-14T07:09:18+00:00Added an answer on May 14, 2026 at 7:09 am

    Linq to Sql data contexts are NOT thread safe, and should only be used within the context of a single thread. Using the singleton pattern is not only contrary to standard linq2sql practices, but will result in serious problems if your application comes under any sort of serious load.

    EDIT:

    In response to your limitations with regard to the using block, try implement your RolesFor method as an extension method:

    public static IQueryable<Group> GetUserRoles(this Database db, string username)
    {
            return from g in db.GetInstance().Groups
                    join ug in db.GetInstance().UsersGroups on g.Id equals ug.GroupId
                    where ug.UserId == user.Id
                    select g;
    }
    

    This would allow you to call your method inside a using block from anywhere:

    using(Database db = createContext())
    {
         IQueryable<Group> queryable = db.GetUserRoles("MyUsername");
         // from here on you can query the queryable object
         var groups = (from g in queryable
                       where g.Name == "MyRole"
                       select g).ToList();
    }
    

    EDIT 2

    In reponse to your comment about opening another connection to the sql server for each instance of the data context. Creating a datacontext will not open a connection to the sql server, but each actual operation will. Regardless of whether you create 1 or 4 datacontexts, if you are performing 4 operations on the database, 4 sqlconnections will be opened. However, keep in mind that .NET uses a sql server connection pool, so each operation doesn’t require the creation of an entirely new SqlConnection, but only the retrieval of an existing one from the connection pool and the reopening of the connection

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

Sidebar

Related Questions

What's the preferred (best practice) means of connecting an ASP.Net Website to a database?
Is there an accepted/expected best-practice way to encapsulate the model portion of ASP.NET MVC
I'm creating a class. What's the best practice for naming properties/methods when your preferred
What is the preferred way to work with Singleton class in multithreaded environment? Suppose
I'm aware of current practice of using Executors instead of ThreadGroup: generally preferred way
I am starting a new project using C# and ASP.NET 3.5 with SQL Server
When defining a PHP class, which is preferred/best practice? Are there any key differences
What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites
Which is the preferred way of defining class properties in Python and why? Is
Given the classes Company , Employee , and Car what is the preferred practice

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.