与Web应用程序Singleton模式,不是一个好主意! [英] Singleton pattern with Web application, Not a good idea!

查看:106
本文介绍了与Web应用程序Singleton模式,不是一个好主意!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些有趣的事情,我靠运气注意到它,而我调试其他的事情。我申请MVP模式,我做了所有的presentations之间共享一个单独的控制器。

I found something funny, I notice it by luck while I was debugging other thing. I was applying MVP pattern and I made a singleton controller to be shared among all presentations.

突然,我想通了,有些事件是在第一次回传被调用一次,如果有两次是二回传,100次,如果有100回传。

Suddenly I figured out that some event is called once at first postback, twice if there is two postback, 100 times if there is 100 postbacks.

由于辛格尔顿是基于一个静态变量,它持有的实例和静态变量住在回传,我有线的情况假设,这将是一次连线,并重新接线每次回发。

because Singleton is based on a static variable which hold the instance, and the static variable live across postbacks, and I wired the event assuming that it will be wired once, and rewired for each postback.

我觉得在一个Web应用程序应用单之前,我们应该考虑再三,还是我错过了什么?

I think we should think twice before applying a singleton in a web application, or I miss something??

感谢

推荐答案

您应该三思而后行任何时候,你使用的是静态对象在多线程应用程序,因为共享状态的(不仅是单件模式)。适当的锁定机构应以接入到共享状态同步施加。如果不这样做一些很难发现漏洞可能出现。

You should think twice any time you are using static objects in a multi-threaded application (not only the singleton pattern) because of the shared state. Proper locking mechanisms should be applied in order to synchronize the access to the shared state. Failing to do so some very difficult to find bugs could appear.

这篇关于与Web应用程序Singleton模式,不是一个好主意!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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