惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 阿牛-专注金融行业开发

2014-09-19.xml ADO.NET 3.5高级编程:应用LINQ&Entity Framework C#高级编程(第8版) ASP.NET MVC 4 Web编程 Version of SQLCE in WP8 Lesson 9 Oral English Training: Lesson 7 一个不错的Options交易策略网"option trading tips"可以学到各种Options的定义. 加强金融专业知识学习,现实从代码开发到业务设计的转型 SOA不是Web Service ReportViewer使用手册 梳理一下最近要重点好学的东西 有空看完<Beginning Xml with C# 2008>这本书, 深入学习一下Xml. Java vs C# 命名规则 <.NET分布式应用程序开发>读书笔记 第十章:Q&A 手动建立强类型DataSet Ndo v3.1发布了! ODP.NET和System.Data.OracleClient的一些不同 Table Scan, Index Scan, Index Seek
Singleton设计模式活学活用: 请求单一 vs 线程单一
阿牛-专注金融行业开发 · 2009-11-11 · via 博客园 - 阿牛-专注金融行业开发
/// <summary>
    /// 在Web Request期间只存在唯一实例的类
    /// 使用了Lazy
    /// </summary>
    public class SingletonPerRequest
    {
        public object Data;

        public static readonly string Key = "SingletonPerRequest.Key";

        public static SingletonPerRequest Current
        {
            get
            {
                SingletonPerRequest instance = (SingletonPerRequest)HttpContext.Current.Items[Key];
                if (instance==null)
                {
                    instance = new SingletonPerRequest();
                    HttpContext.Current.Items[Key] = instance;
                }
                return instance;
            }
        }
    }

    /// <summary>
    /// 在一个执行线程中只存在唯一实例的类
    /// 使用了Lazy
    /// </summary>
    public class SingletonPerThread
    {
        public object Data;

        public static readonly string Key = "SingletonPerThread.Key";

        public static SingletonPerThread Current
        {
            get
            {
                SingletonPerThread instance = (SingletonPerThread)CallContext.GetData(Key);
                if (instance == null)
                {
                    instance = new SingletonPerThread();
                    CallContext.SetData(Key, instance);
                }
                return instance;
            }
        }
    }

这只是个示例,你可以将它改为泛型类,以编写更安全的代码.

此外,CallContext在Web/Win中,可以移植,所以Web中也建议使用第二种方案.