session architecture in asp.net
I have an object that looks like this:
public class MySession{
string UserID {get;set;}
List<Object1> ListOfObject1 {get;set;}
List<Object2> ListOfObject2 {get;set;}
....
several other lists....
}
The advantage of doing this is that I can write something like "on page load" MySession TheSession = Session["TheSession"] as MySession;
and then access the session's properties 开发者_JAVA百科more easily in code.
For now, this works with InProc session but I'm looking to move to SQL Server session.
What's the best way to make this change? I'm thinking of serializing the MySession object into a json string and then letting SQL Server session save the session as a string. Then, when I reload the session, simply load the json string and deserialize it.
I don't know if this is the most efficient way to do it. Thanks for your suggestions.
All you need to do is make MySession
and all related classes [Serializable]
(binary serialization).
ASP.Net will automatically serialize it for you.
If efficiency is the only concern, I am not sure you gain much, unless the object graph is fairly large on some users. With session defaults, it is binary serialized, which would pack down even more than your JSON.
If you need some readability on the session bits (debugging?), then you could go your route.
As long as you mark your class with [Serializable] attribute and it is in fact serializable, you don't have to do your own manual serialization. So long as your web application is configured properly for out-of-process session state management and everything you stick into Session is serializable, everything should work.
精彩评论