Click here to Skip to main content
Click here to Skip to main content

Scoped Context Store

, 15 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

/// <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.

/// <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.

/// <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.

/// <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)

Share

About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions

 
GeneralNice article PinmemberPrasad Khandekar15-Jan-07 19: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 PinmemberPaulo Morgado15-Jan-07 23:38 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 15 Apr 2010
Article Copyright 2007 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid