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 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.