65.9K
CodeProject is changing. Read more.
Home

ObjectContainer - A simple way to demarcate SessionState between pages

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (2 votes)

Jan 24, 2006

CPOL

2 min read

viewsIcon

30024

downloadIcon

118

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.