Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  ASP.NET  »  Session Diagram - A Better Way To Control Your Session
Session Diagram - A Better Way To Control Your Session
by Mohammad Hefny | Published  12/01/2006 | ASP.NET | Rating:
Mohammad Hefny
H-Prog Design & Architecture Manager ( A Computer Engineer, Faculty Of Engineering Cairo University MCSD in VC++  

View all articles by Mohammad Hefny...


When developing an ASP.Net application, it is very common for many developers to use Session for passing data between different pages in the web application. Usually developers tends to leave Session data in memory until the whole session is closed when the user closes the window browser or make sign-out, the overall result of this behavior is that Session size accumulates with garbage or -not used any more- objects as the user navigates in the web application.

Let us assume that we develop a simple e-learning solution, where administrator makes tasks such as "Register Students", "Define Subject", "Define Subject Contents", "Define Exams" , "Publish News". As we can see, the tasks vary and some of them can be grouped such as "Define Subject", "Define Subject Contents", "Define Exams", while others such as "Register Students" and "Publish News" can not. In the previous example - and based on what I find developers do - they tend to create session data of objects such as Student, Subject, Exam, News and store them in session.

 Now imagine the administrator goes to Search Employee screen and selected on to modify his/her data. Then moved to Search Subject screen and selected a Subject and started define its contents then in the exam section of the subject the administrator added a new Exam......etc. more session data are created as we go into the web application. Now the administrator goes to the News section. All subject related session data are garbage now and waste valuable memory space with no need.


- I started thinking that I can utilize Session.Clear, but the issue is that some session data should be permanent through session life, e.g.  the logged user session data that can be a user object with properties that define user roles and permitted activities and this cannot be deleted !

- I started to think of creating a shared class with functions like ClearSubjectSession, ClearEMployeeSession, ClearNewsSession. Then I need to track what page I am in now and what page I came from and start to call these functions one by one. Another disadvantage is that in a team, if a developer created a session data and forgets to clear it in the related clear function then it will be kept in memory for the session lifetime.

Session Diagram & Session Item Solution

Let us deepen our vision of the Session object and add more dimensions. The Session can be seen as a collection that holds session data "Session Items". If we add another dimension we can see the Session Item it self as another collection holds children "related" session data "Session Items". The session now has a structure of a tree instead of queue.


Sample Image - maximum width is 600 pixels


Each session data appears in the diagram as a node, related session data can be grouped by a parent. Permanent session data is marked permanent so that they are not removed when clearing session data.

This is translated into actual code by defining SessionItem object.

Using the code

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

First Create a SessionItem object in the start of every Session, the following code is added to Global.asax

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Session("MainSessionName") = New HProg.SessionManagement.SessionItem
End Sub

From ASP.Net viewpoint there is a single session data stored each session under the name "MainSessionName". The first rule is all session data should be created as children of this session data. for example:

Private mSessionItem As HPROG.SessionManagement.SessionItem
SessionItem = Session("MainSessionName")
SessionItem.Children.Add("SessionItemName", "Value")

Some functions have been added to ease use of the object,  for example instead of a two line function to add a session data you can use

HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "SessionItemName", "Value")

This is implemented as a shared function in the Sessions object. Furthermore, you can create a session child session item given a string path as in the next example:

HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "SessionItemName.ChildSessionItem", "Value2")

In the attached web demo you can see the following:

  1- SessionItem  t1 is created and its children created under it.

  2- It is not allowed to create a sessiondata as a child of a nonexisted session such as the sessiondata item named "exception"

    ' a session data named t1 with value string "t1 value"
    HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1", "t1 value")
    ' a child session data named "t2"
    HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.t2", "t2 value")
    ' a child session data named "t3"
    HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.t2.t3", "t3 value")
        ' "exception is a string value, and ierror is a node that is not created...don't get confused"
        HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.ierror.t3", "exception")
    Catch e As Exception
    End Try

  3- You can directly access a leaf child.

HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "SessionItemName.ChildSessionItem", "Value2")

  4- You can delete Session of a Node or All non-Permanent or All

 '' Remove all Children of SessionItem "i2.c"
        HProg.SessionManagement.Sessions.GetSessionItem(Me.Page, "i2.c").Children.RemoveAll()

 '' Remove All Non-Permanent Session Items
        HProg.SessionManagement.Sessions.RemoveAll(Me, True)

 '' Remove All SessionItems
        HProg.SessionManagement.Sessions.RemoveAll(Me, False)

Advantages: - You can control your session data - If multiple developers work in the same application in different modules there is no concern of similar session item names. Every module has its own Tree Node. - Session Items of a particular module can be all cleared with out the need to clear them explicitly one by one.

Disadvantages: Well there is nothing for free. The sessionItem class comes with an additional memory space and more processing. So it is recommended to use it only when application stored complex session data. Also you can use your own ordinary session items together with SessionItem class in a hyprid model. Remember that SessionItem is basically just another session data a stored in the ASP.Net Session Object.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.90243902439025 out of 5
 41 people have rated this page
Article Score13225
Sponsored Links