Click here to Skip to main content
Licence CPOL
First Posted 15 Jan 2007
Views 17,200
Bookmarked 15 times

Scoped Context Store

By | 15 Apr 2010 | Article
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)

About the Author

Paulo Morgado

Software Developer (Senior)
Paulo Morgado
Portugal Portugal

Member

About Paulo Morgado

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice article PinmemberPrasad Khandekar18:03 15 Jan '07  
GeneralRe: Nice article PinmemberPaulo Morgado22:38 15 Jan '07  

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.

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