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

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

instead of doing

 session(\"myvar1\") = something
 session(\"myvar2\") = something
 session(\"myvar3\") = something
 session(\"myvar4\") = something
相关标签:
7条回答
  • 2021-02-14 23:22

    For the simple thing of removing all of the string key references, I would go with global static/shared constants visible at application scope.

    Otherwise, the strongly typed simple wrapper for session variables is a superior alternative, and it's far more OOP-friendly considering that you get intellisense and Object Browser compatibility.

    The only way I can see the enum being of much value is to be used to index into an array or a similar list. But even then you have to cast the enum to an int.

    So, you could have an array that you load at application startup with all of your session variable keys and then an enum for the indexes. However, given that the Session object derives from HttpSessionState, which derives from IEnumerable, you should be able to just do a foreach loop over the session variables, if you need to.

    0 讨论(0)
提交回复
热议问题