shesharp
shesharp

Reputation: 345

Serializing View or ViewModel

I've started developing in UWP recently. We are developing software that heavily relies on graphics and colleague suggests that we should serialize each View in a single XML and I think this is not a good idea due to property values, static type handling and I'm not certain that we can handle serialization process with async processes correctly and we have multiple UserControls that are reused in other Views. I want to serialize data in our ViewModel, maybe in our Model. I would like to ask:

What are the advantages of View serialization over ViewModel serialization?

If ViewModel serialization is disadvantageous, Why?

edit: we are using dependency objects and I'm not sure if we can serialize them.

Upvotes: 1

Views: 1016

Answers (1)

Emond
Emond

Reputation: 50682

The advantages of ViewModel serialization are in my experience:

  • because you code the ViewModels, you are in full control of the serialisation
  • because the ViewModels do not depend on the View (when MVVM is properly applied) testing the (de)serialisation can be done using unit tests

Yes, getting the serialisation to execute in proper order might be tricky when triggering it from the View but just bubble it up to the top of the ViewModel hierarchy and serialise the entire ViewModel in one go.

If (dependency) properties of a View need to be serialiased, first try to bind these properties to the ViewModel. If you succeed at that, all that needs to be done is to serialise the ViewModel.

If there are properties of the View that cannot be bound to the ViewModel add an interface to the view that can be called from the View's ViewModel that provides a way to read and write these properties from the view to the ViewModel and back again. Call these methods when (de)serialising the ViewModel.

Upvotes: 2

Related Questions