Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

ObjectContainer - A simple way to demarcate SessionState between pages

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
9 Feb 2006CPOL2 min read 29.6K   21   4
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:

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Belarus Belarus
Graduated in Belurassian State University on chair of Artificial Intelligence.
Take a part in development video alarm system for PC with live AI analyzing multiple video streams.

3 years of ASP.NET experience in development multi-thier applications, integrated applications, enterprise level solutions.

Almost 1 year experience in ASP.NET 2.0 starting from BETA 2.

Now working in
http://pro-softsolutions.com

Comments and Discussions

 
GeneralPossible Improvement Pin
Adam L. Stevenson15-Feb-06 19:01
Adam L. Stevenson15-Feb-06 19:01 
GeneralRe: Possible Improvement Pin
Dzianis@gmail.com26-Feb-06 21:58
Dzianis@gmail.com26-Feb-06 21:58 
QuestionHmm. One question Pin
dj_uncas9-Feb-06 6:47
dj_uncas9-Feb-06 6:47 
AnswerRe: Hmm. One question Pin
Dzianis@gmail.com26-Feb-06 21:49
Dzianis@gmail.com26-Feb-06 21:49 

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

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