Click here to Skip to main content
15,867,568 members
Articles / Operating Systems / Windows

Scoped Context Store

Rate me:
Please Sign up or sign in to vote.
3.60/5 (5 votes)
15 Apr 2010CPOL 27.8K   15   2
A data store to store data by scope.

Introduction

Sometimes I need to store data scoped by some context and isolated from the other scopes.

If I'm building a library that is to be used in both Windows client and Web server environments, I have different isolation/scope needs: for a Web server environment, the scope is the current HTTP context; for a Windows client environment, the scope is the current process.

Defining the Contract

The needs for this store are simple, just a dictionary of objects with an object key.

C#
/// <summary>
/// Represents a generic name/value store.
/// </summary>
public interface IScopedContextStore
{
    /// <summary>
    /// Gets or sets the value with the specified key.
    /// </summary>
    /// <value>The value with the specified key.</value>
    object this[object key] { get; set; }

    /// <summary>
    /// Clears the store.
    /// </summary>
    void Clear();
}

Concrete Implementations

The implementations are also simple.

Static Scoped Context Store

This store is scoped by application domain and uses a static Hashtable.

C#
/// <summary>
/// Application Dommanin scoped context store.
/// </summary>
public class StaticScopedContextStore : IScopedContextStore
{
    private static Hashtable store = new Hashtable();

    #region IScopedContextStore Members

    /// <summary>
    /// Gets or sets the value with the specified key.
    /// </summary>
    /// <value>The value with the specified key.</value>
    public object this[object key]
    {
        get
        {
            return StaticScopedContextStore.store[key];
        }
        set
        {
            StaticScopedContextStore.store[key] = value;
        }
    }

    /// <summary>
    /// Clears the store.
    /// </summary>
    public void Clear()
    {
        StaticScopedContextStore.store.Clear();
    }

    #endregion
}

Thread Static Scoped Context Store

This store is scoped by thread, and uses a thread static Hashtable.

C#
/// <summary>
/// Thread scoped context store.
/// </summary>
public class ThreadStaticScopedContextStore : IScopedContextStore
{
    [ThreadStatic]
    private static Hashtable store;

    private static Hashtable Store
    {
        get
        {
            if (ThreadStaticScopedContextStore.store == null)
            {
                ThreadStaticScopedContextStore.store = new Hashtable();
            }
            return ThreadStaticScopedContextStore.store;
        }
    }

    #region IScopedContextStore Members 

    /// <summary>
    /// Gets or sets the value with the specified key.
    /// </summary>
    /// <value>The value with the specified key.</value>
    public object this[object key]
    {
        get
        {
            return ThreadStaticScopedContextStore.Store[key];
        }
        set
        {
            ThreadStaticScopedContextStore.Store[key] = value;
        }
    }

    /// <summary>
    /// Clears the store.
    /// </summary>
    public void Clear()
    {
        ThreadStaticScopedContextStore.Store.Clear();
    }

    #endregion

}

HTTP Context Scoped Context Store

This store is scoped by HTTP context (suited for Web server applications), and uses a Hashtable stored in the current HttpContext instance.

C#
/// <summary>
/// HttpContext scoped context store.
/// </summary>
public class HttpContextScopedContextStore : IScopedContextStore
{
    private static readonly object httpContextKey = new object();

    private static Hashtable Store
    {
        get
        {
            Hashtable hashtable = 
              HttpContext.Current.Items[httpContextKey] as Hashtable;
            if (hashtable == null)
            {
                HttpContext.Current.Items[httpContextKey] = 
                                    hashtable = new Hashtable();
            }
            return hashtable;
        }
    } 

    #region IScopedContextStore Members 

    /// <summary>
    /// Gets or sets the value with the specified key.
    /// </summary>
    /// <value>The value with the specified key.</value>
    public object this[object key]
    {
        get
        {
            return HttpContextScopedContextStore.Store[key];
        }
        set
        {
            HttpContextScopedContextStore.Store[key] = value;
        }
    } 

    /// <summary>
    /// Clears the store.
    /// </summary>
    public void Clear()
    {
        HttpContextScopedContextStore.Store.Clear();
    }

    #endregion
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions

 
GeneralNice article Pin
Prasad Khandekar15-Jan-07 18:03
professionalPrasad Khandekar15-Jan-07 18:03 
Hello,

Good illustration. I have been successfully using this idea for a very long time.



Prasad P. Khandekar
Knowledge exists, man only discovers it.

GeneralRe: Nice article Pin
Paulo Morgado15-Jan-07 22:38
professionalPaulo Morgado15-Jan-07 22:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.