Using session manager class in Global.asax

21 במרץ 2011

one comment

Using Session manager class in Global.asax

Long ago, I’ve written about Session Manager class. The class purpose was to wrap
session variables in properties and gain type safety to them.

In this class we’ve added a Session property with get accessor, which wraps the
HttpContext.Current.Session. Everything went well until I’ve encountered the need
to use this class on Session_End method in global.asax

Global.asax is not a regular Http Handler and therefore it doesn’t hold an HttpContext
This means that when going to the Session we’ve received our good old
NullReferenceException.

On global.asax there is access to the session variables but through it’s own Session
property.

So, what can we do?

One and not so recommended solution is skip the use of Session Manager and go directly
to the session variables in the old fashion way. That’s how you skip the type safety
provided by this class. This is not so recommended because of the type safety being broken
and code being replicated instead of being reuse

The other approach is to create Session property on the Session Manager class and set
it from the constructor. We’ll allow setting it from the constructor because we wouldn’t
want any unexpected corruption of this property.

And that’s how the constructor and the session property will look like:

          public SessionManager(HttpSessionState session)
        {
            Session = session;
        }
         public virtual HttpSessionState Session
        {             get;
            private set;
        }
All other properties we might want to put there, should stay exactly the same.
The Session property is virtual because if we would want to use it from our application’s pages
we’d like to inherit and use HttpContext.Current.Session
In order to use it from our regular pages (the one that holds HttpContext during their Load  --> 
Render lifecycle) we can inherit and override the Session property.
The code of the derived class will look like that:
  public class CurrentContextSessionManager : SessionManager
    {
        public CurrentContextSessionManager()
            :base(null)
        {
         }
         public override HttpSessionState Session
        {
            get
            {
                return HttpContext.Current.Session;
            }
        }
    }
Now we can gain back our type safety from SessionManager class. Here is 
some example of how we can use the base class (a cleanup mechanism called
from Session_End).
   protected void Session_End(object sender, EventArgs e)
        {
            ProductsBL productBL = new ProductsBL();
            SessionManager sessiongManager = new SessionManager(this.Session);

            productBL.ClearShoppingCart(sessiongManager.ShoppingCart);
        }

Where Shopping cart is a property wraps a session variable.

Summary

Our Session Manager class is good to go from every page in our application until
we have to do some actions with it from Async Http handlers such as global.asax
In these cases we have to set our own Session property. For all other pages we
can use a derived class that gets it’s session from HttpContext.Current.Session.

At the end, everyone points at the same session variables, and our type safety
won’t harm.

kick it on DotNetKicks.com

Add comment
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

תגי HTML מותרים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

one comment

  1. Harwood7 ביולי 2012 ב 12:39

    I also conceive thence , perfectly indited post! .

    להגיב