ViewModel 的职责 [英] Responsibility of a ViewModel

查看:14
本文介绍了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 以便我可以在列表中显示它们,并添加一些 View-Only 属性.在内部,ParametersSetsViewModels 引用了用作 View Model 成员源的底层 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.

这应该是谁的责任?

我有这样的感觉,如果 ViewModel 过于活跃,通过使用返回下载文件的方法,这将违反 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 或 WebClient 的异步方法在后台下载应该是可行的.

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

推荐答案

在我看来,每个人都认为 MVVM 没有控制器,因为他们忽略了 C.MVVM 实际上是 MVC 的变体,只是添加了 ViewModels".

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?

ViewModel 仅用于从视图中卸载GUI"代码并包含用于绑定的任何数据.ViewModels 不应该做任何处理.一个好的测试是您的 ViewModel 可以通过自动化单元测试进行测试,并且不依赖于数据源等.他们应该不知道数据实际上来自哪里(或谁在显示数据).

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

虽然它可以被忽略/避免,但控制器负责决定显示什么数据模型以及在哪些视图中显示.ViewModel 是模型(MVVM 中的 M)和视图之间的桥梁.这允许更简单的分离"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.

在回答您的问题时,处理应由控制器处理.如果它需要更新 ViewModel 以显示繁忙的指标等,那很好,但这不是 View 或 Model 或 ViewModel 的责任.

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天全站免登陆