我应该使用单身吗?
这是一个半关系问题,提出以下问题:
This is a semi-related question to question to the following question I just raised:
确定一个类作为一个缓存用于存储在XML文件中的Url解析规则,我认为单例会解决我的问题,但是引入全局状态(尽管它只是沿一个方向运行XML - >解析器)和静态依赖关系。
After determining that a class is to act as a cache for Url parsing rules stored in an XML file, I have considered that a singleton would solve my problem, but introduce global state (although it is only traveling in one direction XML -> parser), and static dependencies.
引导我考虑一个单例的设计考虑是:(请注意,这是一个使用模块捕获并解析使用相同解析器的ALL请求的Web应用程序)
The design consideration that led me to consider a singleton was: (note that this is a web application that uses a module to catch and parse ALL requests using the same parser)
- 我需要缓存存储在XML中的url解析规则,所以类需要在请求之间挂起。我还有一种解析URL的方法,给出规则,它决定了在HttpModule级别的请求路由。
是在这种情况下有单身人士有效吗?你将如何解决这个问题?
Is a singleton valid in this case? How would you solve this?
不需要一个具有所有相关缺点的单例(习惯反单身人士链接)。
There's no need for a singleton with all of the associated drawbacks (customary anti-singleton link ).
我喜欢将其存储在 HttpContext.Cache
中的建议。一些类似的选择:
I like the suggestion to store it in HttpContext.Cache
. Some similar alternatives:
-
将其存储在
HttpApplication.Application
将属性添加到应用程序类中以存储它,然后在相关的 HttpModule
中存储一个类 -
Add a property to your application class to store it, then in the relevant HttpModule
, store a class-level reference to your application.