SessionWrapper Class in C#


ASP.NET provides mechanisms for storing information for a single user session or across multiple sessions.This is done using the HttpSessionState and HttpApplicationState classes.The Page class has Application and Session attributes to provide access to current objects.

The simple way to access them is as following:

if (Session["FirstName"] == null)

{

    LabelFirstName.Text = “FirstName”;

}

else

{

    LabelFirstName.Text = (string)Session["FirstName"];

}

if (Session["LastName"] == null)

{

    LabelLastName.Text = “LastName”;

}

else

{

    LabelLastName.Text = (string)Session["LastName"];

}

This method has several drawbacks:

Type Safety: Session contains objects, so you need to cast.

Null reference: You must check, it might be null

Variable name: don’t use hard coded strings, beware of typos…

So Lets see the wrapper object and how it solves those issues:

public static class SessionWrapper

{

    private static T GetFromSession<T>(string key)

    {

        object obj = HttpContext.Current.Session[key];

        if (obj == null)

        {

            return default(T);

        }

        return (T)obj;

    }

    private static void SetInSession<T>(string key, T value)

    {

        if (value == null)

        {

            HttpContext.Current.Session.Remove(key);

        }

        else

        {

            HttpContext.Current.Session[key] = value;

        }

    }

    private static T GetFromApplication<T>(string key)

    {

        return (T)HttpContext.Current.Application[key];

    }

    private static void SetInApplication<T>(string key, T value)

    {

        if (value == null)

        {

            HttpContext.Current.Application.Remove(key);

        }

        else

        {

            HttpContext.Current.Application[key] = value;

        }

    }

    public static string FirstName

    {

        get { return GetFromSession<string>(“FirstName”); }

        set { SetInSession<string>(“FirstName”, value); }

    }

    public static string LastName

    {

        get { return GetFromSession<string>(“LastName”); }

        set { SetInSession<string>(“LastName”, value); }

    }

    public static User User

    {

        get { return GetFromSession<User>(“User”); }

        set { SetInSession<User>(“User”, value); }

    }

}

It contains a few generic private functions to read and write objects from/to the Session or the Application objects, and public methods to be used from the code to access required properties. Note that the use of HttpContext.Current requires a valid request, or Current might be null!

This wrapper provides a safe typed access from all over the application, one place to define all objects that might be stored in the session. If you want to move objects from the Session to other storing mechanism this would be solved in the wrapper only.

The usage is also much easier:

LabelFirstName.Text = SessionWrapper.FirstName;

LabelLastName.Text = SessionWrapper.LastName;

Note that you can add any initialization code or default values in the wrapper:

public static string FirstName

{

    get

    {

        string firstName = GetFromSession<string>(“FirstName”);

        if( string.IsNullOrEmpty(firstName))

        {

            firstName = “FirstName”;

            SetInSession<string>(“FirstName”, firstName);

        }

        return firstName;

    }

    set { SetInSession<string>(“FirstName”, value); }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s