Singleton class or a class with static methods in ASP.NET MVC application [duplicate]

扶醉桌前 提交于 2019-12-10 14:23:43

问题


Possible Duplicate:
ASP .NET Singleton

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?


回答1:


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.




回答2:


Your thinking is correct, singleton and static classes are shared by multiple users. Not a good thing if the info stored is supposed to be 'user-specific'.

"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?"

Again your right on target, having a singleton class in ASP.Net app means there will only be one instance of the object in the process.

I think you might benefit by knowing a little about Out-Of-Process session servers: http://msdn.microsoft.com/en-us/library/ms972429.aspx

HTH



来源:https://stackoverflow.com/questions/4382537/singleton-class-or-a-class-with-static-methods-in-asp-net-mvc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!