简体   繁体   English

模型或ViewModel中的MVVM计算字段?

[英]MVVM Calculated Fields in Model or ViewModel?

I am trying to determine the correct pattern for handling Calculated fields or methods in MVVM. 我正在尝试确定用于处理MVVM中计算字段或方法的正确模式。 I have a number of calculated fields that will be required for both the presentation layer and some back end processing. 我有许多计算字段,它们对于表示层和某些后端处理都是必需的。 Let say it is " CalculateTotal " which is based on summing a number of different values from the Data object and its sub objects. 假设它是“ CalculateTotal ”,它基于对来自Data对象及其子对象的多个不同值进行求和。

I would like this calculation in one location. 我想在一个位置进行此计算。 The first thought is that it belongs in the model (ie, not the viewModel) since the model object will be passed to other back end processing systems. 首先想到的是,它属于模型(即,不属于viewModel),因为模型对象将传递给其他后端处理系统。 What is then the best way to make this available to the ViewModel ? 那么将其提供给ViewModel的最佳方法是什么?

Option 1, is that I statically load the viewModel when I create it based off the Model (eg, vwModel.Total = model.CalculateTotal() ) This suffers if I am needing to update the ViewModel dynamcally, for example I modify the underlying data and try to get the new total. 选项1是,当我基于Model创建视图模型时,我会静态加载它(例如, vwModel.Total = model.CalculateTotal() ),这在我需要动态更新ViewModel遇到问题,例如,修改基础数据并尝试获取新的总数。

Option 2: More service oriented and each calculation calls a service to return the values. 选项2:面向服务,每次计算都调用一个服务以返回值。 The issue I see with this is more performance based. 我看到的这个问题更多是基于性能。 Once I load my object once, I have all the data needed to perform the calculation. 一次加载对象后,我便拥有了执行计算所需的所有数据。 It seems each call would require the data object to be reloaded 似乎每个调用都需要重新加载数据对象

Option 3: Have the ViewModel contain the data model and create methods to call the data model methods 选项3:让ViewModel包含数据模型并创建方法以调用数据模型方法

Thoughts? 思考? Suggestions? 建议?

I would do all calculations in the model. 我会在模型中进行所有计算。 The view model should subscribe to events (eg via INotifyPropertyChanged ) on the model, so when the back-end values change, the view model will be notified. 视图模型应该订阅模型上的事件(例如,通过INotifyPropertyChanged ),因此,当后端值更改时,将通知视图模型。

The view model can, of course, interrogate the model for calculations and doesn't have to get all information through events. 视图模型当然可以查询模型以进行计算,而不必通过事件获取所有信息。 The above is only for when the data changes come directly from the model or the underlying data layer. 以上仅适用于数据更改直接来自模型或基础数据层的情况。

I would go with something like option 2. Moving the calculation logic out of the model and view model will simplify things and make that logic more reusable by other classes. 我将使用类似选项2的内容。将计算逻辑移出模型和视图模型将简化事情,并使该逻辑可被其他类重用。 From your question it seems like you already have an idea of how to implement that. 从您的问题看来,您似乎已经对如何实现这一想法有所了解。 Keeping the model "dumb" and having services/utility/helper classes that know how to process the model will help you in the long run if the model is being passed around a lot. 如果模型被大量传递,从长远来看,保持模型“哑巴”并拥有知道如何处理模型的服务/实用程序/帮助程序类将对您有长远的帮助。 Just something to think about but keeping the responsibility of a class very limited makes for easier code maintenance down the road. 只需考虑一下,但将类的职责限制在非常有限的范围内,可以简化代码的维护。 You might end up with more classes but I personally find it easier to focus and work on two or three small classes than one very large class. 您可能会获得更多的课程,但是我个人发现集中精力和专注于两个或三个小课程比一个非常大的课程要容易。 I don't know what type of application you have or how heavy the calculations are but unless you are having measurable performance issues I wouldn't worry about trying to pre-optimize things. 我不知道您拥有哪种类型的应用程序或计算量如何,但是除非您遇到可测量的性能问题,否则我不会担心尝试进行预优化。

I would like this calculation in one location. 我想在一个位置进行此计算。

If there is a dedicated Service Layer , then almost all calculations should be done in Service Layer . 如果有专用的Service Layer ,则几乎所有计算都应在Service Layer

Option 1 选项1

Instead of using CalculateTotal method on Model , it's better make a read only property Total and calculate it in a service layer method every time Model is requested. 与其在Model上使用CalculateTotal方法,不如使它成为只读属性Total并在每次请求Model时在服务层方法中对其进行计算。 This is the easiest and the best option to use if there are no performance issues, since it drastically simplifies ViewModel. 如果没有性能问题,这是最容易使用的最佳选择,因为它可以大大简化ViewModel。

Option 2 选项2

Calling a dedicated CalculateTotal service method from ViewModel is the only option available if calculation process is resource intensive and calculated total is not used every time Model is requested. 如果计算过程是资源密集型的并且每次请求Model都不使用计算的总数时,从ViewModel调用专用的CalculateTotal服务方法是唯一可用的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM