的ViewModels和渲染 [英] ViewModels and rendering

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

问题描述

在一些样本项目,我已经看到了正在使用的ViewModels数据对象转换为字符串,在视图中使用。

In several sample projects, I've seen ViewModels being used to convert data objects into strings, for use in the View.

的视图模型通常具有接收一个参数的构造 - 一个数据对象。然后,构造函数将填充视图模型的各种属性(主要是字符串和整数)。

The ViewModel will typically have a constructor that receives one parameter - a data object. The constructor will then populate various properties of the ViewModel (mostly strings and ints).

这$ P $从视图中发生任何pvents复杂的逻辑。

This prevents any complex logic from occurring in the View.

乍一看,这似乎是一个好主意,对我来说,因为它更全面地实施了查看从复杂的逻辑的分离。

At first glance, this seems like a good idea to me, as it more fully enforces separation of the View from complex logic.

举例来说,说我的看法是想呈现一个数据对象的属性尺寸,尺寸为1和3重presenting之间的数字。小/中/大。

For example, say my view was trying to render a property 'Size' of a data object, Size being a number between 1 and 3 representing 'Small/Medium/Large'.

而不必在我看来,一个IF / switch语句,我只想有一个'SizeString或在我的ViewModel类似的东西,而如果/ switch语句会包含在视图模型构造。

Instead of having a if/switch statement in my view, I would just have a 'SizeString' or something similar in my ViewModel, and the if/switch statement would go in the ViewModel constructor.

有谁这种做法不同意?

会不会更好用一些其他的方法,如佣工?如果是这样,为什么?

Would it be better to use some other approach, such as helpers? And if so, why?

推荐答案

视图模型的目的是重新present(的一部分)分解为能以某种形式之外的其他渲染图元的复域模型

The purpose of the ViewModel is to represent (a part of) the complex Domain Model decomposed as primitives that can be rendered in some form other other.

本分解必须发生的地方。它可能涉及一些简单的逻辑,如我最喜欢的例子:转换离散值(确定警告错误的)转换成颜色(绿色,黄色,红色)。这是一个什么样的视图模型做的精华,所以我的默认方法是将封装这种逻辑到视图模型本身。

This decomposition must take place somewhere. It may involve some simple kind of logic, such as my favorite example: converting a discrete value (OK, warning, error) into colors (Green, Yellow, Red). This is the essence of what a ViewModel does, so my default approach would be to encapsulate this logic into the ViewModel itself.

考虑替代方案:如果视图模型不落实,又在哪里?如果你把逻辑别的地方,你结束了一个视图模型这基本上只是一个结构,无逻辑。域对象的让一个视图模型封装的转变/分解与单一职责原则合身。

Consider the alternative: If not implemented in the ViewModel, then where? If you put the logic somewhere else, you end up with a ViewModel that's basically just a structure without logic. Letting a ViewModel encapsulate the transformation/decomposition of a Domain Object fits well with the Single Responsibility Principle.

虽然这是我的默认的态度,我总是知道,逻辑可能需要在多个的ViewModels被重用。在这种情况下,这可能是一个迹象,原来的视图模型确实是由几个子视图复杂视图模型。在这种情况下,你可以提取常见的逻辑放到一个子视图模型,封装只有少部分。

Although this is my default approach, I'm always aware that the logic may need to be reused across multiple ViewModels. In such cases, this may be an indication that the original ViewModel is really a complex ViewModel made up of several sub-views. In such cases, you can extract the common logic into a sub-ViewModel that encapsulates only that little part.

这篇关于的ViewModels和渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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