I'm working on a text-based game in WPF using the MVVM pattern. I would like to use Binary Serialization to save and load the game as I did when I was using WinForms, but with this pattern I don't know how to do this properly. Which classes need to be serialized to preserve the necessary data? Is there anything else that I need to be aware of?
Which classes need to be serialized to preserve the necessary data?
This might sound circular, but: whatever you need preserved. Sometimes this can be your domain model - the M in MVVM. If this isn't immediately obvious from you model, then I would say: add a new DTO layer. This DTO model would be simply: what you want stored. Then you can save and load without affecting the rest of the system. Obviously you need some code to map to/from the DTO model and whatever model you are using.
Is there anything else that I need to be aware of?
If, by "binary serialization" you mean BinaryFormatter
, then be very very careful - it can be brittle as you version your app. There are other serializers that read/write in a binary- (rather than text) based format, but which are less … fussy than BinaryFormatter
.