Web应用程序C#ASP.NET中的单例模式 [英] Singleton Pattern in Web Application C# ASP.NET

查看:298
本文介绍了Web应用程序C#ASP.NET中的单例模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单例模式在Web应用程序中的作用是什么?



来自不同用户的10个请求用于登录如何使用它?



实例将10 = 1或10 = 10?



如果10 = 1?它将如何处理?



如果10 = 10?怎么会这样?实例存储在哪里?在网络的情况下。

whats the role of singleton pattern in web application?

10 requests from different users for log in how it will be used?

instance will 10=1 or 10=10?

if 10=1? how it will be handled?

if 10=10? how it will be? where the instance is get stored? in case of web.

推荐答案

请看我对这个问题的评论。



唯一合理的答案我可以给出的是:可以使用或不使用单例模式。不应经常使用此模式。在大多数应用程序中,根本不应该使用它。那么,它在Web应用程序中的作用是什么?如果您不使用此模式,则根本没有任何角色。如果您使用此模式,那就是您给它的角色。 :-)



没有特别的建议,除了一个:

在ASP.NET应用程序中,尽量不添加任何除了那些已经存在于ASP.NET技术(或应用程序环境)中的单例,例如 HttpContext 。请参阅此有用的讨论: http://stackoverflow.com/questions/2134511/asp-net-singleton [ ^ ]。



参见: http://msdn.microsoft.com/en-us/library/system.web.httpcontext%28v=vs.110%29.aspx [ ^ ]。



-SA
Please see my comment to the question.

The only reasonable answer I can give is: singleton pattern may or may not be used. This pattern should not be used often. In most cases of the application, it should not be used at all. So, what's the role of it in a Web application? No role at all, if you don't use this pattern. If you use this pattern, that is the role you gave to it. :-)

No special recommendations, except perhaps one:
In an ASP.NET application, try not to add any singletons in addition to those which already present in ASP.NET technology (or application environment), such as HttpContext. Please see this useful discussion: http://stackoverflow.com/questions/2134511/asp-net-singleton[^].

See also: http://msdn.microsoft.com/en-us/library/system.web.httpcontext%28v=vs.110%29.aspx[^].

—SA


这篇关于Web应用程序C#ASP.NET中的单例模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆