AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=563&pId=-1
Session Variables - Saving with XML
page
by Keith Barrows
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 22150/ 14

Introduction

[Download Code]

When developing web sites there are many times that session state has to be saved between pages.  Maybe you are writing an Intranet app that takes 20 pages to do one action.  Or, maybe you have an Internet site that state must be saved as the user randomly navigates through the site.  Whatever the reasons, you need to save session state and are concerned about over-filling the memory.  Regardless of what method of storing session state you choose the point of this article is to make it easier for the developer and to keep the number of actual session variables down to one.

Having worked on many ASP.NET projects it seems we were always faced with code bloat just from passing data in and out of a page.  On one particular project I was tasked with exploring different ways to store data, reduce the memory footprint, and ease the developer's burden.  The first method we came up with was a class structure that contained a property for each variable we needed in our app. 

Storing a full blown object in memory was not saving us much in the way of memory footprint.  After looking at all of our data we discovered that all of the data we needed to store was numbers, strings and dates.  That's when we made the move to exploring XML as a possible storage medium.  One of the biggest benefits of XML is that it is a string.  Another is that it is easy to deal with.  So now that we have decided on an XML approach it is time to dive into the easiest method of developing this type of solution.

Class - BaseSessionStorage

[Download Code]

The first thing we need to do is create the base classes we will use in the rest of the application.  The first base class is the XML Handler.  This class will handle all the data in and out of the Session Object.  So how do we do this?  If you look in BaseClasses\BaseSessionStorage.cs you will see we are using two protected objects and a protected string:

protected XmlDocument _internalState;
protected HttpContext _httpContext;
private string _identifier;

Now that we have defined our objects we need to start dealing with them.  _internalState is the actual XML object we use to handle all of our XML based tasks. _httpContext is an internal representation of the HTTP context the server deals with. And _identifier is the Session State key half of the Session State key/value pair.  Whenever any object is instantiated that is inheriting from this object we want to make sure that the bare minimums are always instantiated.  We do this by allowing only one constructor.

 
public BaseSessionStorage(HttpContext appContext, string Identifier, string BaseXml) 
{ 
    // Set internal objects and variables... 
}

The above constructor guarantees we get the 3 elements we need to make this class work.  The rest of this class is a Public Property to get the XML string and a small handful of Public Methods to Get and Set the values and to get a Count of XML Nodes.

This article is not meant to be an XML tutorial so I will not be going into the details of this base class.  Suffice it to say that the XML handling is straight forward and works.  Please feel free to step through it, tweak it if necessary and have fun with it.

With this base class we can now create the application classes that we will use in the Web Pages, Web Controls and Web Services.

Class - PageInfo & AppInfo

[Download Code]

It's time to take the Base Class we just created, inherit it, and extend it.  There are 2 classes in the attached project that use the BaseSessionClass.  The first class handles User based information and the second class handles Page based information.  This is where it gets fun.  No one can tell you what the right variables are for your application.  The first app I did this for, the decision was made to split the User from everything else.  It may be that having 1 object will work for you.  You could also have a half dozen objects.

When creating a class there are only a few things to keep in mind; that have to be created:

  • Default XML_BODY and SESSION_NAME
    const string XML_BODY = "<?xml version='1.0'?>
    <PageInfo myID='-1' EntryGroupID='-1' EntryGroup='-1' 
    ErrorPage='~\\defaultError.aspx' ErrorMessage='' ErrorReturnPage='' 
    historyDate='' currentChoice='0' ErrorNoItems='' ItemTotalCnt='0' 
    ReturnPage='' OtherXmlData='' />";
    const string SESSION_NAME = "PageInfoData";
  • Call to Base Class Constructor
    internal PageInfo(HttpContext appContext):base(appContext, SESSION_NAME, XML_BODY)
    {
    }
  • Expose the values through public properties

Since the decision was made to split the User from the rest of the variables one more class was added to wrap this into a whole.  We ended up calling this class App, pretty creative of us.  It makes it real simple to deal with all of your session information this way.

App.Page.ID = textControl.Text;
App.User.FirstName = textFName.Text;
App.User.LastName = textLName.Text;

 

Summary

[Download Code]

Dealing with session state and variables can get pretty messy, especially in larger apps.  Having a standard way of tracking all of that information makes your job as a developer much easier. 

Session State is one way of maintaining state across several pages and even across post-backs on a single page.  Use it wisely.  This is just another arrow in your application state quiver.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-18 12:49:55 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search