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 6661203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:15:01+00:00 2026-05-26T02:15:01+00:00

I know how the Singleton pattern works, but I have doubt how it works

  • 0

I know how the Singleton pattern works, but I have doubt how it works in a web scope. Is not naturally by request?

A request is made and the singleton is instantiated and the request has ended. The singleton was destroyed? If yes, why some people doing to let singleton in request scope explicitly? If no, what happens? The singleton remains in the memory and a new one is created for each request?

Update:

When I say “why some people doing to let singleton in request scope explicitly”, is for example using Ninject I have to do it:

Bind<ISession>().To(SessionSingleton.Instance).InRequestScope();

Update 2:

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}

Update 3:

My DbContext is Singleton? I think No, my DbContextFactory is singleton, BUT my DbContext in this case will be destroyed when the request ended?

public class DbContextFactory
{
    #region Fields

    private static volatile DbContextFactory _dbContextFactory;
    private static readonly object SyncRoot = new Object();
    public DbContext Context;

    #endregion

    #region Properties

    public static DbContextFactory Instance
    {
        get
        {
            if (_dbContextFactory == null)
            {
                lock (SyncRoot)
                {
                    if (_dbContextFactory == null)
                        _dbContextFactory = new DbContextFactory();
                }
            }
            return _dbContextFactory;
        }
    }

    #endregion

    #region Methods

    public DbContext GetOrCreateContext()
    {
        if (this.Context == null)
            this.Context = new DbContext(ConfigurationManager.AppSettings["DefaultConnectionString"]);

        return Context;
    }

    #endregion
}
  • 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-26T02:15:02+00:00Added an answer on May 26, 2026 at 2:15 am

    Even in the context of a web app there are objects with global scope. One of them is System.Web.HttpApplication which is created by the framework. Also static instances have global scope. These global objects lifetime is tied to the lifetime of your Application ( not of the web request). The framework might stop and start your application ( think app pool recycling ).

    In the context of a web app singletons also have global scope, so they are tied to the lifetime of the application and will not be destroyed at the end of each request. They might be destroyed and recreated when the web app is restarted, but that should not meter to your app.

    Also to complete what others have said, it’s true that singletons are usually evil and that you should think twice before designing something as a singleton, except when they are exactly what you need. Some examples where you might want to ensure you only have one instance are logging, caching, NHibernate’s session factory and other cross-cutting infrastructure components, and also models for resources that exist as only one instance.

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

Sidebar

Related Questions

I know Singleton pattern has been discussed so much. But because I'm not fully
I know that Singleton pattern is bad because it uses global state. But in
I have a singleton (I know that is a bad pattern). To control the
I have read multiple articles about why singletons are bad. I know it has
I know Microsoft has made efforts in the direction of semantic and cross-browser compliant
There are already quite some posts about the Singleton-Pattern around, but I would like
I know actionscript does not allowed private contstructor at any time and But if
I have an object cache which implements the Singleton design pattern. My approach to
I know there are lots of design patterns (facade, singleton, decorator, etc...) but I'm
Possible Duplicate: A Singleton that is not globally accessible Do you know a good

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.