I'm trying to keep my session in SQLServer. I know if an object needs to be stored in SQL than it needs to be Serialized. And i also know that i need to put [Serializable] attribute on every class if necessary. But the problem is i have more than 2000 object and keeps growing. They are bound together very tightly. If i put one of them to session probably i need to add [Serializable] attributre to all of them, which is pointless. What i want to know is how to write the same code for ISerializable interface just like [Serializable] attribute does in the base class? Any idea? Or should i add [Serializable] to every class. If i do is there any downside?
Asked
Active
Viewed 69 times
1 Answers
0
Create a more specific, little class that represent the data you actually need to persist in session and apply the [Serializable]
attribute only on that class.
Please note that [Serializable]
is NOT required by Sql Server itself but by the session management in asp.net session

Martino Bordin
- 1,412
- 1
- 14
- 29
-
First option acceptable but due to project specific constraints i can't create that 'more specific' class. – Murat Jan 12 '16 at 08:14
-
Ok. However adding Serializable attribute won't have any side effects until you actually serialize. In that case runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied. (source [MSDN](https://msdn.microsoft.com/en-us/library/system.serializableattribute(v=vs.110).aspx)) – Martino Bordin Jan 12 '16 at 08:21
-
Why not all objects are Serializable by default? Looks like i need to add [Serializable] attribute to all my classes. A fool's errand :\ – Murat Jan 12 '16 at 09:12
-
[Serializable] is a 'marker' to highlight the fact that the class can be serialized. It's not required by all serializer (i.e. XmlSerializer\DataContractSerializer don't use it) but you need it for Session persistance. – Martino Bordin Jan 12 '16 at 09:32