Dangers of Serialization
I get quite worried when I see serialization used for messaging, it tends to make the solution fragile and harder to change. My concerns partly flow on from the earlier posting about schemas giving a false sense of security. There is always a temptation when you have a schema to auto generate a class from it and then just use serialization. There a few issues with this:
Finally having said all of that if you have messages (or parts of messages) which hardly ever change, and when they do you expect code changes, then serialization can probably help.
- Schema changes require recompilation of the code to re-generate the class
- It's harder to work out which fields from the message you actually care about on the subscriber side
- There are more cases where changes in the schema break the code (or rather cause the deserialization to stop working)
- There is also a temptation to just serialize domain objects and start passing those around instead of defining proper business documents. (there are so many reasons why this is bad, a topic for a whole other post)
- You can ask your message subscribers to write tests for fetching the values they care about. So then you can know exactly which parts of the message the subscriber cares about and if recent changes have broken anything. (more on testing and schematron soon)
- Subscribers are far less exposed to schema changes: ordering, extra fields and/or extra elements don't break anything as you can still just get the parts of the message you care about
- You can improve performance later on by pre-fetching or caching values so you don't hit the underlying message every time, as opposed to deserialing the whole message
- I'd also argue you achieve better encapsulation since, when using serialization the fact this mechanism is involved tends to 'creep' into other parts of the code. i.e. people start passing the serialized objects around instead of just using them for messaging.
Finally having said all of that if you have messages (or parts of messages) which hardly ever change, and when they do you expect code changes, then serialization can probably help.


