开发者

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开发者_JAVA百科 sounds like I am supposed to implement the Save methods in the ViewModel.

Is what I am doing right now improper?


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.


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜