I built a strongly typed session object that looks like this:
class MySession
{
public string UsedID {get;set;}
public List<int> ListOfInt {get;set;}
public List<string ListOfString {get;set;}
.....
}
I'm currently using InProc session so when a page loads, I开发者_C百科 write:
MySession TheSession = Session["UserSession"] as MySession;
and then later in the code I can access each property with TheSession.XYZ
syntax.
This is really cool but I'm thinking that it might be better to store the session in the DB.
I'm thinking about serializing the MySession object in a json string and store the string in a DB that I can retrieve and deserialize when a page loads.
Is this a good way to do it?
Thanks for your suggestions.
First of all make sure you need all the data in Session. I sense that you rather need to create database tables and store data in the tables.
If you are sure that you need Session, then it would make sense to use standard <sessionState mode="SQLServer">
. You can read more about Session-State Modes here.
If you are sure you want custom serialization, then it would make sense to use binary serialization instead of JSON. It will need less memory to store and less resources to serialize/deserialize.
ASP.NET Session State Server vs. InProc Session covers the pros and cons of using a state server or in-process session storage.
As for using JSON, it's certainly more lightweight than XML and just about as flexible - at least for your purposes. I see no reason not to go with it.
精彩评论