简体   繁体   English

Asp.Net Singleton Pattern场景

[英]Asp.Net Singleton Pattern scenario

In Asp.Net in what scenario i will use singleton as most of the job can be done with Session / Cache / Static by checking whether it is null nor not. 在Asp.Net中,在什么情况下我将使用单例,因为大多数工作可以通过检查它是否为空来完成Session / Cache / Static。

please refer to my previous question 请参阅我之前的问题

Singleton Pattern / Check for Null - While using Asp.Net with Session 单例模式/检查空 - 在使用带会话的Asp.Net时

I think you are confusing the particular implementation of the Singleton pattern with the concept itself. 我认为你将Singleton模式的特定实现与概念本身混淆。 Your previous question / implementation could be interpreted as Singleton within a user session, if you saved in the Application cache instead it would be a Singleton for the ASP.NET application. 您之前的问题/实现可以在用户会话中解释为Singleton,如果您保存在Application缓存中,则它将是ASP.NET应用程序的Singleton。

Singleton just means that there is only one instance of a particular object at any time and this is enforced by the application - how you enforce this is an implementation detail that might vary depending on your choice of platform / language. Singleton只是意味着任何时候只有一个特定对象的实例,这由应用程序强制执行 - 您如何强制执行此操作是一个实现细节,可能会根据您选择的平台/语言而有所不同。

In particular for ASP.NET you may still use the standard singleton pattern for C# using a static variable (see Implementing the Singleton Pattern in C# ). 特别是对于ASP.NET,您仍然可以使用静态变量为C#使用标准单例模式(请参阅在C#中实现单例模式 )。 There will only be multiple instances if your application is used in a web farm scenario, also refer to this SO thread: Are static class instances unique to a request or a server in ASP.NET? 如果您的应用程序在Web场方案中使用,则只有多个实例,也请参阅此SO线程: ASP.NET中的请求或服务器的静态类实例是唯一的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM