Justin
Justin

Reputation: 2479

MVVM- Where should I implement my save logic?

I have two different models that need saving; a TextFile object and a static Settings object.

Right now, I have the save logic implemented in the models themselves.

I like how clean this looks, when calling the save methods:

Settings.Save();

and

_currentFile.Save(filePath);

However, from what I have read it sounds like I am supposed to implement the Save methods in the ViewModel.

Is what I am doing right now improper?

Upvotes: 4

Views: 1053

Answers (3)

jbe
jbe

Reputation: 7031

You might be interested in the Writer sample application of the WPF Application Framework (WAF). It uses the MVVM pattern and it shows where the Save operation can be implemented. Additionally, it uses a static Settings object to save the user preferences (language).

Upvotes: 0

Steve Danner
Steve Danner

Reputation: 22158

If what you are doing is persisting the state of one of your models to a file, then I would say what you're doing is perfectly logical. If the file you're saving is saving data this is view-specific, then it really should go in the ViewModel.

Upvotes: 1

kyoryu
kyoryu

Reputation: 13055

Saving goes in the model. But, it might not go in that particular class (the Model consists of all of your actual workload).

The ViewModel only exists to translate from the Model to the View. It should have no business logic.

Upvotes: 3

Related Questions