通用的自定义上下文对象

 /// <summary>
    /// 通用的自定义上下文对象
    /// </summary>
    public class GenericContext
    {
        /// <summary>
        /// 由于内部操作上,所有的容器类型均为 Dictionary<string, object>
        /// 所以定义一个固定的类型名称。
        /// </summary>
        class NameBasedDictionary : Dictionary<string, object> { }


        /// <summary>
        /// 用于 Windows 应用的线程级上下文成员容器
        /// </summary>
        [ThreadStatic]
        private static NameBasedDictionary threadCache;
        /// <summary>
        /// 标识当前应用是否为 Web 应用
        /// </summary>
        private static readonly bool isWeb = CheckWhetherIsWeb();
        /// <summary>
        /// Web 应用中 Context 保存的键值
        /// </summary>
        private const string ContextKey = "marvellousWorks.context.web";


        /// <summary>
        /// 对于 Web 应用,如果HttpContext对应内容元素没有初始化,则放置一个空容器。
        /// 对于 Windows 应用,由于 threadCache 为 [ThreadStatic],无需该过程。
        /// </summary>
        public GenericContext()
        {
            if (isWeb && (HttpContext.Current.Items[ContextKey] == null))
                    HttpContext.Current.Items[ContextKey] = new NameBasedDictionary();
        }



        /// <summary>
        /// 根据上下文成员名称,返回对应内容的。
        /// <remarks>
        /// 由于 threadCache 或 HttpContext 中的缓冲对象都会在构造过程中创建
        //  因此,这里没有对 cache == null 的判断。
        /// </remarks>
        /// </summary>
        /// <param name="name">上下文成员键值。</param>
        /// <returns>对应内容</returns>
        public object this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name)) return null;
                NameBasedDictionary cache = GetCache();
                if (cache.Count <= 0) return null;
                object result;
                if (cache.TryGetValue(name, out result))
                    return result;
                else
                    return null;
            }
            set
            {
                if (string.IsNullOrEmpty(name)) return;
                NameBasedDictionary cache = GetCache();
                object temp;
                if (cache.TryGetValue(name, out temp))
                    cache[name] = value;
                else
                    cache.Add(name, value);
            }
        }



        /// <summary>
        /// 根据应用类型获取相应上下文缓冲对象。
        /// </summary>
        /// <returns>缓冲对象。</returns>
        private static NameBasedDictionary GetCache()
        {
            NameBasedDictionary cache;
            if (isWeb)
                cache = (NameBasedDictionary)HttpContext.Current.Items[ContextKey];
            else
                cache = threadCache;
            if (cache == null)
                cache = new NameBasedDictionary();
            if (isWeb)
                HttpContext.Current.Items[ContextKey] = cache;
            else
                threadCache = cache;
            return cache;
        }

        /// <summary>
        /// 判断当前应用是否为 Web 应用的 Helper 方法 (非官方方法)
        /// </summary>
        /// <returns></returns>
        public static bool CheckWhetherIsWeb()
        {
            bool result = false;
            AppDomain domain = AppDomain.CurrentDomain;
            try
            {
                if (domain.ShadowCopyFiles)
                    result = (HttpContext.Current.GetType() != null);
            }
            catch (System.Exception){}
            return result;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值