Click here to Skip to main content
Click here to Skip to main content
Go to top

ObjectContainer - A simple way to demarcate SessionState between pages

, 9 Feb 2006
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dzianis@gmail.com
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 PinmemberAdam L. Stevenson15-Feb-06 19:01 
GeneralRe: Possible Improvement PinmemberDzianis@gmail.com26-Feb-06 21:58 
QuestionHmm. One question Pinmemberdj_uncas9-Feb-06 6:47 
AnswerRe: Hmm. One question PinmemberDzianis@gmail.com26-Feb-06 21:49 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 9 Feb 2006
Article Copyright 2006 by Dzianis@gmail.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid