避免重复数据&视图模型中的计算
我正在开发一个使用 MVVM 的 Flex 应用程序。据我了解,建议在视图和视图模型之间建立一对一的映射。如果多个视图中需要多次执行相同的计算和存储相同的数据,如何避免多次执行相同的计算和存储相同的数据?
例如,假设我有一个程序可以加载由项目列表组成的项目文件。我的应用程序的主选项卡有一个导出按钮,仅当项目有效时才应启用该按钮,因此我在主选项卡的视图模型上创建了一个 isExportEnabled
属性。此计算涉及通过迭代项目中的每个项目并验证每个项目的某些属性来验证项目。我有另一个选项卡,其中有一个“打印项目摘要”按钮,因此该选项卡的视图模型具有 isPrintEnabled
。无论项目是否有效,此标志的值都基于相同的标准。
我可以将用于确定这些值的逻辑放在各自的视图模型中,但是当加载项目时,我最终会计算相同的值两次。
我可以将
isValid
移动到域模型,但随后模型就会变得非规范化。如果在不更新“isValid”标志的情况下编辑已保存的项目会怎样?我可以为整个项目创建一个全局“项目视图模型”,它计算项目是否有效,然后将
isExportEnabled
和isPrintEnabled
委托给它。然而,我最终得到了本文建议避免的视图模型层次结构: 在 Flex 中应用演示模型
感觉没有正确的方法来做到这一点。
I'm working on a Flex application that uses MVVM. I understand that it's recommended to have a 1-to-1 mapping between views and view-models. How then do I avoid performing the same calculations and storing the same data multiple times if it is needed in multiple views?
For example, imagine I have a program that can load project files consisting of a list of items. The main tab of my application has an export button that should only be enabled if the project is valid so I create an isExportEnabled
property on the view-model for the main tab. The calculation for this involves validating the project by iterating though every item in the project and verifying some attribute on each. I have another tab which has a "Print Project Summary" button so the view-model for this tab has isPrintEnabled
. The value for this flag is based on the same criteria, whether or not the project is valid.
I could put the logic for determining these values in their respective view-models, but then I end up calculating the same value twice when a project is loaded.
I could move
isValid
to the domain model but then the model becomes denormalized. What if a saved project is edited without updating the "isValid" flag?I could make a global "Project View-Model" for the entire project which calculates whether the project is valid and then have
isExportEnabled
andisPrintEnabled
delegate to that. However, then I end up with a hierarchy of view-models which this article recommends avoiding: Applying the Presentation Model in Flex
It feels like there is no correct way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以你是说你有两个用户手势,“导出”和“打印项目摘要”,它们基于相同的验证功能?如果我正确地阅读了您的文章,那么您可以计算一次并有两个 getter
现在有些人可能会说,由于它们返回相同的值,所以我们应该摆脱 getter 并返回一个简单的 [Bindable] 公共值。但由于这似乎是此功能的首次实现,因此拥有两个独立的函数可以让您在面对不断变化的 UI 和验证要求时具有一定的鲁棒性。
就我个人而言,如果验证逻辑变得太重,我会将该值保留在我的模型“public var isValidProject”中,因为模型应该知道它是否有效。然后,表示层将使用该值来确定如何向用户表示这是一个无效项目(弹出窗口、警报、错误字符串)。
在其他情况下,我会在这些按钮“set selectedProject”的演示者上创建一个设置器,并运行我的验证和/或更改那里的按钮启用状态。
我真的很想听听其他人对此的想法。
So you are saying you have two user gestures, "Export" and "Print Project Summary" which are based on the same validation function ? If I read your post right then you can calculate it once and have two getters
Now some may say that since they are returning the same value that we should get rid of the getters and return a simple [Bindable] public value. But since this seems like the first implementation of this feature, having two separate functions allows you some robustness in the face of changing UI and validation requirements.
Personally, if the validation logic got too heavy, I would keep that value on my model "public var isValidProject" since the model should know if it is valid or not. Then the presentation layer would use that value to determine how to represent to the user that this is an invalid project (popups,alerts,error strings ).
In other cases, I would make a setter on the presenter of those buttons "set selectedProject" and run my validation and/or change my buttons enablement state there.
I would really be interested to hear others thoughts on this.