Does Serializable hamper performance?
I recently changed the Session State to use SQL server and then realized not all my entities were serializable.
Now trying to find out which entities are being put in session throughout the code seems to be a big pain, so I was thinking why not make all of them serializable?
Will this have any performance hit? Why are classes not marked as Seri开发者_开发百科alizable by default?
Adding [Serializable]
doesn't impact performance; simply most classes don't need to be serializable, and the ability to store internal state doesn't always make sense - it only really makes sense for "entity"or "DTO" objects.
Some things to watch: make sure events aren't serialized; mark then [field: NonSerialized]
- otherwise you might grow more data than you expect ;p
Also; BinaryFormatter
can be... rather voluminous. If you want smaller data, I would suggest looking at (for example) protobuf-net (100% free), which you can hook via ISerializable
- it is generally significantly more efficient than BinaryFormatter
. Let me know if you are interested in more details.
精彩评论