MBit
MBit

Reputation: 213

MVVM and serialization of model components

On a preparation for a workshop for some apprentices, I came across a problem with saving the current state while following the MVVM pattern.

Environment

First the class diagramm used for this question:
class diagramm

Secondly the GUI, so you can imagine what I expect the user to do:
GUI

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 Problem

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! workflow

Possible Solutions

I came across some solutions but I don't know which one fits the idea of the MVVM pattern best:

  1. Set the checkbox status according to the imported ingredient objects.
    That means I check if the name of an ingredient from the import is the same as in one of the IngredientVMs. If so I set the IsChecked to true.
    But I feel like this is very poor way to code.
  2. Serialize the all IngredientVMs
    I just serialize one level above. This way I get all Ingredients with their state (checked or not).
    The downside with this solution is that if the user loads an older save file only the old ingredients are being loaded since it overloads the usual 'createIngrediants' process.
  3. Only export the ids
    In my opinion this is the most suitable solution so far. I only export the ids of the selected ingredients. (I'd have to add ids to the class first of course) While importing I just look for the ids in the list and set those to checked.
    But somehow I feel like adding ids to something as simple as ingredients is wrong...

So, does any of you have a better solution to this problem?

Thanks for your inputs!

Upvotes: 3

Views: 926

Answers (1)

Siderite Zackwehdex
Siderite Zackwehdex

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

Related Questions