Singleton类或在ASP.NET MVC应用程序的静态方法的类 [英] Singleton class or a class with static methods in ASP.NET MVC application

查看:93
本文介绍了Singleton类或在ASP.NET MVC应用程序的静态方法的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
   ASP .NET辛格尔顿

我知道静态属性/方法单独的类和类之间的一般性的区别,但我想知道它是如何影响并发(许多用户登录的应用程序)在ASP.NET MVC Web应用程序?例如,我们在我们的单(或静态的属性)类存储设置。是否有一个机会,两个用户突然开始看到/共享相同的设置?我的意思是,如果一个用户改变他的设置(被它们存储在内存中的应用程序运行时),是否会影响其他用户?据我知道IIS为应用程序创建一个w3wp.exe进程,因此所有用户/观众将是相同的过程里面,所以会这样对什么有影响吗?

I know the general differences between singleton class and a class with static properties/methods but I would like to know how it influences the concurrency (many users logged in an application) in a ASP.NET MVC web application? For instance, we are storing settings in our singleton (or static properties) class. Is there a chance that two users suddenly start seeing/sharing the same settings? I mean, if one user changes his settings (being they are stored in memory for the runtime of the app), will it affect the other user? As far as I know the IIS creates one w3wp.exe process for an application, thus all users/visitors will be inside the same process, so could this have an effect on anything?

推荐答案

简短的回答,是的,用户将看到任何静态属性相同的值。它将在会话之间共享。

Short answer, yes, the users will see the same value for any static property. It will be shared between the sessions.

龙回答:是的,另外,如果每个会话试图在同一时间,你可以用意外的行为最终更新共享属性,特别是如果共享(静态)属性是动态增长型(任何集合型)。你需要处理的并发访问或修改共享属性的值时。

Long answer: yes and additionally if each session is attempting to update shared properties at the same time you can end up with undesired behavior, especially if the shared (static) property is a type that dynamically grows (any collection type). You'll need to handle concurrency when accessing or modifying the values of shared properties.

的唯一理由,一个单身或静态类是专门为共享一组与所有连接值/设置/配置数据。它使内存利用率很低,如果所有用户都需要相同的信息。在这种情况下,你要小心只有一次初始化值或使用原子锁,以确保只有一个线程正在改变一次信息更新。任何数量的线程可以读取数据,而不互相干扰。

The only reason to have a singleton or static class is specifically to share one set of values/settings/configuration data with all connections. It keeps memory utilization low if all users need the same information. In this case you'd take care to initialize the values only once or update it atomically using a lock to ensure only one thread is changing the information at a time. Any number of threads could be reading the data without interfering with each other.

如果您需要为不同的用户不同的价值观,这是更好,因为你不需要担心同意并锁定相关的性能命中使用实例属性。

If you need different values for different users, it's better to use instance properties as you won't need to worry about concurrence and the performance hit associated with locking.

静态方法都很好。实例和静态方法之间的唯一真正的区别是它们被称为方式。即使实例方法在内存只在一个地点举行。只有实例属性给出了自己的内存空间。

Static methods are fine. The only real difference between instance and static methods is the manner in which they are called. Even instance methods are held in memory in only one location. Only the instance properties are given their own memory space.

这篇关于Singleton类或在ASP.NET MVC应用程序的静态方法的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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