一个ViewModel的责任 [英] Responsibility of a ViewModel

查看:150
本文介绍了一个ViewModel的责任的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个工具,它的目标是基于一些参数来下载文件。

I am building a tool which goal is to download a file based on a few parameters.

第一步是设置(或检索)的参数。

First step is to set (or retrieve) those parameters.

检索套不同的参数(让我们通过配置文件说)由FileDownloadsManager:它确切地知道哪些参数,以便下载正确的文件使用

The different parameters sets are retrieved (let's say via configuration files) by a FileDownloadsManager : it knows exactly which parameters to use in order to download the right file.

这些参数都存储在一个类中,我有这个类的实例列表。

those parameters are stored in a class, and I have a list of instances of this class.

这意味着,我可以下载我的文件有多个可能的参数集。

That means that I can download my file with multiple possible parameters sets.

围绕这些ParameterSets,我已经建立ParametersSetsViewModels,这样我可以在列表中显示出来,并加入一些仅查看属性。在内部,ParametersSetsViewModels必须用作用于查看模型的成员源底层ParametersSets参考。

Around those ParameterSets, I've built ParametersSetsViewModels so that I can display them in a list, and add some View-Only properties. Internally, the ParametersSetsViewModels have a reference to the underlying ParametersSets used as a source for the members of the View Model.

现在,当我选择我的参数进行设置,我想相关的文件被下载。

now, when I select my parameters set, I would like the related file to be downloaded.

谁的责任应该是这样?

我有这种感觉,如果视图模型过于活跃,由具有返回下载的文件的方法,这将是对MVVM模式;什么是您对此怎么看?

I have this feeling that if the ViewModel is too active, by having a method that returns the downloaded file, this would be against the MVVM pattern; what is your take on this ?

奖励:下载应与BackgroundWorkers或Web客户端的异步方法进行背景可行

Bonus : the download should be feasible in the background with BackgroundWorkers or WebClient's asynchronous methods.

推荐答案

在我看来,每个人都假定,他们离开了C. MVVM实际上是MVC,只是增加的ViewModels的变化MVVM没有控制器。

It seems to me that everyone assumes MVVM has no controllers as they left out the C. MVVM is actually a variation of MVC "that just adds ViewModels".

也许它应该被称为MVCVM呢?

Maybe it should have been called MVCVM instead?

的ViewModels只有那里卸载从视图中的图形用户界面code和包含任何数据绑定。的ViewModels不应该做任何处理。一个好的例子是,你的视图模型是通过自动装置进行测试了,并且对数据源等,他们应该不知道那里的数据其实是来自(或谁是显示它)来没有依赖关系。

ViewModels are only there to offload the "GUI" code from the view and to contain any data for binding. ViewModels should not do any processing. A good test is that your ViewModel is testable via automated unit tests and has no dependencies on data sources etc. They should have no idea where the data is actually coming from (or who is displaying it).

虽然可以忽视/避免,控制器负责决定哪些数据模型显示和其中的看法。该视图模型是模型(并购在MVVM)和视图之间的桥梁。这使得简单的分隔XAML创作。

Although it can be overlooked/avoided, a Controller is responsible for deciding what data model to display and in which views. The ViewModel is a bridge between Models (the M in MVVM) and Views. This allows simpler "separated" XAML authoring.

在回答你的问题的处理应该由一个控制器处理。如果需要更新视图模型显示忙指示器等这很好,但它不是视图或模型或视图模型的责任。

In answer to your question the processing should be handled by a controller. If it needs to update the ViewModel to show busy indicators etc that is fine, but it is not the View or the Model or the ViewModel's responsibility.

这篇关于一个ViewModel的责任的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆