2

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.

frenchie
  • 51,731
  • 109
  • 304
  • 510

2 Answers2

0

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.

Community
  • 1
  • 1
Yuck
  • 49,664
  • 13
  • 105
  • 135
0

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.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • i think,thats what you should do – DeveloperX Jun 22 '11 at 04:31
  • OK. What's best way to store a list of int or a list of string in a table? – frenchie Jun 22 '11 at 13:58
  • @frenchie - to answer this question correctly it would help to know that this list of ints stores. Without knowing that, the answer is: in the table with one column on int type. – Alex Aza Jun 22 '11 at 15:06
  • ? The list of ints stores ints. Is it better in your opinion to a) serialize the list into a string and store 1 string or b) store x rows of ints? – frenchie Jun 22 '11 at 15:14
  • @frenchie - I understood that the list stores ints :). The question is what these ints are, and how they are used. In other words do you need them in every session, do you need all of them in every session. How the list is generated originally, what is the source of the list? – Alex Aza Jun 22 '11 at 15:18
  • The list stores the ID of a list of objects. These objects are stored in a json string in the DB and I keep the list of ints in the InProc session to know which objects to retrieve in the DB. There are about 2000 ints per list of int. – frenchie Jun 22 '11 at 15:23
  • @frenchie - seems like the best option for is option #2. So you would have `typed session` object as you do now, but you would use SQLServer session. This will take care of session serialization/deserialization for you. The list attractive option is serializing the list to string. If you want to control serialization/deserialization, then you better use binary serializer. – Alex Aza Jun 22 '11 at 15:32