Reputation: 213
On a preparation for a workshop for some apprentices, I came across a problem with saving the current state while following the MVVM pattern.
First the class diagramm used for this question:
Secondly the GUI, so you can imagine what I expect the user to do:
The MainMV creates a IngredientsVM for every ingredients there is (hard coded) and adds them into a list. Then this list is bound to the UI. As the user changes the checkbox status the 'IsChecked' property of the IngredientVM changes accordingly.
The chosen ingredients via the UI are being saved in the Pizza object. So the pizza object contains a list of the chosen ingredients.
The user shall be able to export and import his choice. This sounds like a pretty easy thing to do but the problem comes with the MVVM pattern which I'd like to use for this exercise.
Since I have a list of Ingredients that is set, I already have instances of these classes. If I save and load them by serialization I get another instance of the serialized classes. So I end up with a list of all ingredients and a list with the chosen ingredients which are two different objects!
I came across some solutions but I don't know which one fits the idea of the MVVM pattern best:
So, does any of you have a better solution to this problem?
Thanks for your inputs!
Upvotes: 3
Views: 926
Reputation: 6570
You would need an id for an ingredient, that's for sure. If the name is unique, then it works just fine and it is human-readable as well, so option 3, which is the same as option 1 in this case. If the name is not unique, like you have different types of tomato or something, you use some other unique identifier.
MVVM handles the way the UI is separated from the underlying processes, importing and exporting data is not part of its purview, so the title of the question is a bit misleading, if you ask me.
Upvotes: 1