So more often then not I want to share something in a "singleton" fashion across a call stack. But a true singleton wouldn't work because the objects I want to share are usually not thread safe or its not a best practice to persist them across threads. NHibernate has a Session manager pattern that is very similar to what I want and so I extended on that principal making what I call a "Context" Singleton.  Its simply just a little base class that will store itself in right context and as such will also "die out" once that context has ended.

Here is the helper class


public abstract class ContextSingletonBase: IDisposable
    {
        private static string CONTEXT_PREFIX = "ContextSingleton:";

        protected static object _getObject(string key)
        {
            if (isWCF)
            {
                return WcfInstanceContext.Current.Items[ContextSingletonBase.CONTEXT_PREFIX + key];
            }
            else if (isHttp)
            {
                return System.Web.HttpContext.Current.Items[ContextSingletonBase.CONTEXT_PREFIX + key];
            }
            else
            {
                return CallContext.GetData(ContextSingletonBase.CONTEXT_PREFIX + key);
            }
        }
        protected static void _setObject(string key, object obj)
        {
            if (_getObject(key) != null)
            {
                if (isWCF)
                {
                    WcfInstanceContext.Current.Items.Remove(ContextSingletonBase.CONTEXT_PREFIX + key);
                }
                else if (isHttp)
                {
                    System.Web.HttpContext.Current.Items.Remove(ContextSingletonBase.CONTEXT_PREFIX + key);
                }
                else
                {
                    CallContext.SetData(ContextSingletonBase.CONTEXT_PREFIX + key, null);
                }
            }

            if (isWCF)
            {
                WcfInstanceContext.Current.Items.Add(ContextSingletonBase.CONTEXT_PREFIX + key, obj);
            }
            else if (isHttp)
            {
                System.Web.HttpContext.Current.Items.Add(ContextSingletonBase.CONTEXT_PREFIX + key, obj);
            }
            else
            {
                CallContext.SetData(ContextSingletonBase.CONTEXT_PREFIX + key, obj);
            }
        }
        private static bool isWCF
        {
            get
            {
                return (WcfInstanceContext.Current != null);
            }
        }
        private static bool isHttp
        {
            get
            {
                if (isWCF)
                {
                    return false;
                }
                if (System.Web.HttpContext.Current != null)
                {
                    return true;
                }
                return false;
            }
        }
        internal class WcfInstanceContext : IExtension
        {
            private readonly IDictionary items;

            private WcfInstanceContext()
            {
                items = new Hashtable();
            }

            public IDictionary Items
            {
                get { return items; }
            }

           public static WcfInstanceContext Current
            {
                get
                {
                    if (OperationContext.Current != null)
                    {
                        WcfInstanceContext context =
                            OperationContext.Current.InstanceContext.Extensions.Find();
                        if (context == null)
                        {
                            context = new WcfInstanceContext();
                            OperationContext.Current.InstanceContext.Extensions.Add(context);
                        }
                        return context;
                    }
                    return null;
                }
            }

           public void Attach(InstanceContext owner) { }

           public void Detach(InstanceContext owner) { }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _dispose();
        }

        protected abstract void _dispose();

        #endregion
    }


 So you will notice I test for several contexts.. This is because going down to CallContext is not always safe.. First off for Http Applications the executing thread can actually change.. If it changed and you weren't using HttpContext you would end up loosing your ContextSingleton. (Granted HttpContext is infact using CallContext at its core, using HttpContext will handled that potential thread switch out for you).  I also check for a WCF context.. This is because (and I'm not 100% sure about this) WCF services are handled diffrently depending on the connection point. Using the WCF Context instead of CallContext removes any "wierdness" that may occure.

So this was just a base class and on its own missing the "Singleton" part of the whole design.. Its mostly intrested in the storage of said singleton and not the actual access or storeage of it. So what does an Implemented Context Singleton look like?

Well here is an overly simple one that stores a Linq2Sql Data Context... you can see the pattern is pretty much pure "singleton" but instead of the Instance being a static field its a property and it calls back to the above static classes that then look in the approproate context for the actual instance of the helper.. In this way your interact with the class as if it were a traditional singleton but it only persists inside its appropriate context (Http, WCF or Call)..



  public class Helper:ContextSingletonBase
    {
        private static string KEY = "SingletonKey";
        public static Helper Instance
        {
            get
            {
                Helper h = GetHelper();
                if (h == null)
                {
                    h = MakeHelper();
                }
                return h;
            }
        }

        private static Helper MakeHelper()
        {
            Helper h = new Helper();
            Helper._setObject(Helper.KEY, h);
            return h;
        }

        private static Helper GetHelper()
        {
            object helper = Helper._getObject(Helper.KEY);
            if (helper != null)
            {
                return (Helper)helper;
            }
            return null;
        }

        private DataContext _dc;
        public DataContext DC
        {
            get
            {
                if (_dc == null)
                {
                    _dc = new DataContext();
                }
                return _dc;
            }
       }

        protected override void _dispose()
        {
            _dc.Dispose();
        }

}


One thing I would love to do is actually replace the Contexts code I have with some kind of provider model.. Use a bit of code (or settings) to detect the approriate context and return that.. That way the ContextSingletonBase wouldn't have to be depended on .NET 3.0 (like it is now) and its code wouldn't grow in complexity as other contexts become known (I only did this for Call, Http and WCF because those are the only contexts I've run into and Call is kind of the catch all.. But who knows, Silverlight might have its own... .NET compact framework might have one, etc... you can see how seporateing the context storage from the ContextSingleton helper could become a good thing..