Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ObjectContainer - A simple way to demarcate SessionState between pages

0.00/5 (No votes)
9 Feb 2006 1  
This article describes how to create a simple WebControl to isolate part of SessionState for a page or control to store data.

Introduction

I once developed a new Web Application, and the problem of storing some page-related data was raised. These were the main problems:

  • Can't save data to the database. My objects can be changed, and changes should be committed later (something like a wizard).
  • Can't put objects to the ViewState due to the huge size.
  • Can't use Session because the user can work with more than one page copies at the same time.
  • Data access layer doesn't support caching mechanism.
  • There are no special state machines.

The problem was resolved by writing a simple Web Control which holds the anchor to the data container where I store my objects.

ObjectContainer

We have only two properties and two fields in our custom Web Control derived from the WebControl class. First one:

private Guid _Key = Guid.Empty;
private Guid Key
{
    get
    {
        if ( _Key == Guid.Empty )
        {
            if ( this.ViewState[this.UniqueID] != null )
            {
                _Key = (Guid) this.ViewState[this.UniqueID];
            }
            else
            {
                this.Key = Guid.NewGuid();
            }
        }
        return _Key;
    }
    set
    {
        _Key = value;
        this.ViewState[this.UniqueID] = _Key;
    }
}

This is just the anchor for our data storage. We insert the anchor in the ViewState. The second:

private Hashtable _Data;
public Hashtable Data
{
    get
    {
        if (_Data == null )
        {
            if ( HttpContext.Current.Session[this.Key.ToString ()] != null )
            {
                _Data = ( Hashtable ) HttpContext.Current.Session[this.Key.ToString()];
            }
            else
            {
                this.Data = new Hashtable ();
            }
        }
        return _Data;
    }
    private set
    {
        _Data = value;
        HttpContext.Current.Session[this.Key.ToString ()] = _Data;
    }
}

As we can see, the public Hashtable uses a key to set and get the anchored data to and from the session.

As a result, we have a Web Control which can be easily drag 'n' dropped to your page or control, and then you can use the Data property to store your objects without caring about data identity in page copies at runtime.

Minuses

Of course, the main problem of this control is memory usage. All objects will be in memory till the session is alive. To prevent memory overload, the session should be cleaned manually when you finish all your operations with the container. Like that:

public void Clear ()
{
    HttpContext.Current.Session.Remove ( this.Key.ToString () );
}

Next time, I will describe how to extend this idea and implement a session container without using custom web controls.

Revision History

  • Added examples for .NET 1.1 and 2.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here