Best practice to serialize and deserialize .net objects across versions

后端 未结 2 975
一个人的身影
一个人的身影 2021-02-10 02:52

Objects are serialized to a database using the .NET XML serializer. The object can change over time, and therefore multiple versions exist in the database at one time.

A

相关标签:
2条回答
  • 2021-02-10 03:18

    Have a schema version number in the serialized object. Using custom deserialization, check the version attribute first, and if it turns out to be an old version, upgrade it to the latest schema before deserializing.

    0 讨论(0)
  • 2021-02-10 03:31

    This all depends on your application. Is it a distributed rich application where old applications may encounter new data objects from a central database or other source? (Much like older versions of office applications need to have some ways of dealing with newer document formats.)

    If so, custom serialization and deserialization with explicit schema version numbering, I'd say. I'd put explicit metadata on each element and attribute, stating whether a reader must understand the element/attribute (and default values if not). This can of course consume quite a lot of space and increase code complexity...

    But the answer really depends on why you are serializing to a database. You're not interested in using the database for its relational capabiities? Otherwise, an O/R mapping solution might be of interest.

    0 讨论(0)
提交回复
热议问题