Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

.Net Session Variables (C#)

Options
  • 30-05-2012 5:07pm
    #1
    Registered Users Posts: 3,992 ✭✭✭


    Ok So a project im working on has a TON of session variables and its becoming a massive issue keeping track of them all.

    Does anyone know of a way to consolidate these into a single class/object?

    I found this online:
    
    using System.Web;
    
    /// <summary>
    /// This class is used for setting and getting the session variables.
    /// Doing it this way means that one can see all available session variables.
    /// </summary>
    public static class SessionHelper
    {
        /// <summary>
        /// The Current Users ID (PersNo/EmplNo)
        /// </summary>
        public static int UserID
        {
            get
            {
                return (int)HttpContext.Current.Session["UserID"];
            }
    
            set
            {
                HttpContext.Current.Session["UserID"] = value;
            }
        }
    
    }
    
    

    So calling:
    SessionHelper.UserID gives us the Session["UserID"] in int form.

    Has anyone used similar?

    Does it work if multiple users are using the Web Application at the same time ?

    Does the fact that its static mean that users will overwrite each others sessions? Or is it okay that the property is static?

    If not, are there any alternatives?

    Im currently throwing together a little test page to test this out.


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    For languages that can't have global variables and free functions, I usually wrap things like that in a class with static properties like you for sessions, for settings in desktop apps, for everything.

    It won't overwrite the session info for other users. The session relates to the current HttpContext.


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Session variables should be used sparingly when you say you have a ton of them I would get worried.

    But the code you posted will handle multiple sessions fine.

    You could go a step further and create a class to hold you variables and pop that into a single session variable.


  • Registered Users Posts: 3,992 ✭✭✭Korvanica


    Cheers Lads, The test page is proving your point aswell :) (Testing with about 15 users - no issues so far)
    kayos wrote: »
    Session variables should be used sparingly when you say you have a ton of them I would get worried.

    But the code you posted will handle multiple sessions fine.

    You could go a step further and create a class to hold you variables and pop that into a single session variable.

    Ton of sessions because - Legacy code, evolved over time, multiple developers... Massive SESSION that probably included drinking and coding blind! Gah! :P

    At least this way, you can type
    SessionHelper.

    and a list of available sessions will pop up, makes development so much easier !

    How would you go about doing a single class ? Just a load of peoperties? and call

    (SessionClass)Session["SessionClass"].UserId

    ?

    Cheers


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Session should be encapsulated or abstracted away, you might not always have a webstack (think API for IOS or something). A wrapper helps, but session is a bad naming convention for it!


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Korvanica wrote: »
    Ton of sessions because - Legacy code, evolved over time, multiple developers... Massive SESSION that probably included drinking and coding blind! Gah! :P

    Nothing wrong with drunken programming as long as you fix the stupid mistakes when sober :).
    Korvanica wrote: »
    How would you go about doing a single class ? Just a load of peoperties? and call

    (SessionClass)Session["SessionClass"].UserId

    Have the SessionHelper expose a property for your data object.
        public static class SessionHelper
        {
            private const string UserDataKey = "UserData";
    
            public static UserData UserData
            {
                get
                {
                    //if null return a new instance of the SessionData object you might not want to do it this way
                    return HttpContext.Current.Session[UserDataKey] as UserData ?? new UserData(); 
                }
                set
                {
                    HttpContext.Current.Session[UserDataKey] = value;
                }
            }
        }
    
        public class UserData
        {
            public int UserId { get; set; }
            public string UserName { get; set; }
            public DateTime LastActivity { get; set; }
        }
    


  • Advertisement
Advertisement