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

推荐订阅源

有赞技术团队
有赞技术团队
V
V2EX
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
量子位
Engineering at Meta
Engineering at Meta
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
I
Intezer
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
P
Proofpoint News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Scott Helme
Scott Helme
Stack Overflow Blog
Stack Overflow Blog
博客园 - 【当耐特】
S
Securelist
T
Tailwind CSS Blog
Simon Willison's Weblog
Simon Willison's Weblog
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
P
Privacy International News Feed
K
Kaspersky official blog
T
Tor Project blog
L
LINUX DO - 热门话题
Apple Machine Learning Research
Apple Machine Learning Research
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
Last Week in AI
Last Week in AI
L
LangChain Blog
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
博客园 - 叶小钗
I
InfoQ

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

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中也建议使用第二种方案.