65.9K
CodeProject is changing. Read more.
Home

Scoped Context Store

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.60/5 (5 votes)

Jan 15, 2007

CPOL
viewsIcon

28668

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
}