单例模式如何在 Web 范围内工作?
我知道单例模式是如何工作的,但我怀疑它在网络范围内是如何工作的。不是自然而然的按要求吗?
发出请求并实例化单例,请求结束。单例被破坏了?如果是,为什么有些人明确地让单例进入请求范围?如果不是,会发生什么?单例保留在内存中,并且为每个请求创建一个新单例?
更新:
当我说“为什么有些人明确地让单例进入请求范围”时,例如使用 Ninject 我必须这样做:
Bind<ISession>().To(SessionSingleton.Instance).InRequestScope();
更新 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;
}
}
}
更新 3:
我的 DbContext
是 Singleton?我认为不,我的 DbContextFactory
是单例的,但是在这种情况下我的 DbContext
会在请求结束时被销毁吗?
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使在 Web 应用程序的上下文中,也存在具有全局范围的对象。其中之一是由框架创建的 System.Web.HttpApplication。静态实例也具有全局范围。这些全局对象的生命周期与应用程序的生命周期(而不是 Web 请求的生命周期)相关。该框架可能会停止并启动您的应用程序(想想应用程序池回收)。
在 Web 应用程序的上下文中,单例也具有全局作用域,因此它们与应用程序的生命周期相关,并且不会在每个请求结束时被销毁。当网络应用程序重新启动时,它们可能会被销毁并重新创建,但这不应该计量您的应用程序。
另外,为了完成其他人所说的,单身人士通常是邪恶的,在设计单身人士之前你应该三思而后行,除非它们正是你所需要的。您可能希望确保只有一个实例的一些示例包括日志记录、缓存、NHibernate 的会话工厂和其他横切基础架构组件,以及仅作为一个实例存在的资源模型。
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.
正如 Raynos 所说:单身人士是邪恶的。
但是回答你的问题,单例不会为每个请求启动或销毁,但它保留在内存中并在所有请求之间共享,无论何时发出请求。
我不完全理解你的意思为什么有些人明确地让单例进入请求范围?。你说的是一个具体的框架吗? (出于某种原因,我感觉你在谈论春天)。您能举个例子来说明您所指的内容吗?
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?