I've got an ASP.Net project that uses session state, I'd like to be a little more strict about how we access session state, I'm not happy about all the strings floating around the code. I also need to know when a particular value is stored/updated in session state for tracking that object's latest value.
The string issue is easy to solve using constants, but it doesn't help with the tracking. Encapsulating them all in a single class is appealing, but then I have to pass the session object to that class, and that seems a little messy
I'm thinking of using one of two options:
- Extension getters and setters on the session object
- An extension method on the session object to return a class with the getters and setters
The first gives me the syntax:
var thing = Session.GetThing();
Session.SetThing(thing);
The second gives:
var thing = Session.Wrapper().Thing;
Session.Wrapper().Thing = thing;
Both have their appeal, though I'm leaning towards the second. In an ideal world I'd like to be able to do this:
var thing = Session.Thing(); // easy to do
Session.Thing() = thing; // don't think it's possible
What's the preferred way of handling this? Any of these, another way, or am I just doing it wrong?