is it a good idea to create an enum for the key names of session values?

后端 未结 7 2206
失恋的感觉
失恋的感觉 2021-02-14 22:45

instead of doing

 session(\"myvar1\") = something
 session(\"myvar2\") = something
 session(\"myvar3\") = something
 session(\"myvar4\") = something
7条回答
  •  -上瘾入骨i
    2021-02-14 23:03

    I've used classes like this to make a typed session/cache wrapper. You may need to add additional code to the get/set, but I'll leave that up to you.

    internal class SessionHelper
    {
        private const string  myVar1Key = "myvar1";
    
        public static int MyVar1
        {
            get
            {
                return (int)System.Web.HttpContext.Current.Session[myVar1Key];
            }
            set
            {
                System.Web.HttpContext.Current.Session[myVar1Key] = value;
            }
        }
    }
    

    Sorry about the C#....

提交回复
热议问题