To save object state for unit tests we use binary serialization. The object state saved to data file and at the start of the test is loaded from the file. It works OK until the object will be changed in a new version. Fortunately There is no problem when a new field is added, but if a field is deleted or removed, it most likely will cause deserialization exception(see
Version Tolerant Serialization, Net Serialization FAQ, Solving Binary serialization versioning conflicts articles for details).
Also changing return type will break deserialization. E.g. I've changed return type from ArrayList to type-safe List<MyType> and I've got errors.
I wanted to change the storage to use SoapFormatter to be able to read and optionally modify the data file, If it's broken.
(Similar approach described by Mauro Sturaro in discussion here)
But SoapFormatter does not support generics and beginning with the .NET Framework version 3.5, this class is obsolete.
Dino Esposito suggests to consider LosFormatter Class -but it is not human-readable, like binary.
Dictionary with keys from enum.
One of our classes keeps state in a dictionary of attributes, with the keys from enum. Adding new values to enum do not causes deserialization exceptions, But the problem was that enum didn't have explicit assignments for numbers, but used automatic increment. When someone inserted a new key into enum, actual enum values were shifted, and all dictionary was corrupted.
The highly recommended way is to explicitely assign values for enum . Do not forget to have defualt value for 0- e,g, NonInitialized.
I've created a helper functions AreValuesUnique<EnumType, ValueType>() and List<ValueType> FindDuplicates<EnumType, ValueType>() to ensure that explicit values are not duplicated and I am calling it from unit test.
Based on these problems I don't like to store objects for unit test using binary serialization.
I am going to research, which other options are viable.