How Singleton Pattern works in a web scope?
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(开发者_如何学C);
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
}
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.
As Raynos said: Singletons are evil.
But answering your question, the singleton doesn't get instatiated or destroyed for each request, but it remains in memory and is shared between all requests regarless of when the request is made.
I don't fully understand whay you mean by why some people doing to let singleton in request scope explicitly?. Are you talking about a spefic framework? (for some reason I have the feeling you're talking about Spring). Can you give an example of what you're referring to?
精彩评论