Click here to Skip to main content
Click here to Skip to main content

Session Diagram "A Way For Better Controlling Your Session"

, 29 Nov 2006
Rate this:
Please Sign up or sign in to vote.
An article discussion a solution for cleaning session from unnecessary object in a simple way.

Introduction

This article discusses a solution for session utilization and management to avoid filling the memory by unused session data. The article introduces a diagram called "Session Diagram" and a couple of simple classes that can be used with in the Session object to efficiently manage sessions.

Background

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 tend to leave Session data in memory until the whole session is closed when the user closes the window browser or sign-out; the overall result of this behavior is that Session size accumulates with garbage or orphaned 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 varies 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.

Solutions

  • I started thinking that I can utilize Session.Clear, but the issue is that some session data should be permanent through session life. A good example is the logged user session data that can be a user object with properties that define user roles and permitted activities - this cannot be deleted!
  • I started to think of creating a shared class with function 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 forget to clear it in the related clear function then it will be kept in memory for the session lifetime.

Session Diagram and 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. Further more, 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", _<BR>                                                "t1 value")
     
    ' a child session data named "t2"  
    HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.t2", <BR>                                                "t2 value") 
    ' a child session data named "t3" 
    HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.t2.t3", _
                                                    "t3 value")  <BR>Try
       ' "exception is a string value, and ierror is a node that is not _
       ' created...don't get confused"
        HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, _<BR>                                            "t1.ierror.t3", "exception")
    Catch e As Exception
        Response.Write(e.Message)
    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.

History

N/A

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohammad Said Hefny
Architect
Egypt Egypt
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralC# Version Pinmemberpocketkaos21-Dec-06 4:06 

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
Web02 | 2.8.140721.1 | Last Updated 29 Nov 2006
Article Copyright 2006 by Mohammad Said Hefny
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid