MVVM更新计算的属性 [英] MVVM update of calculated properties

查看:88
本文介绍了MVVM更新计算的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习MVVM,并且我正在尝试工作如何显示对计算所得属性的更改,这些更改是由于计算所得值的变化而引起的.到目前为止,我所看到的所有解决方案都严重违反了封装,我想知道是否还有更好的方法.

I'm just learning MVVM, and I'm trying to work how to display changes to a calculated property as a result of changes to the values from which it's calculated. All of the solutions I've seen so far seriously violate encapsulation, and I'm wondering whether there's anything better.

假设我需要显示的一件事是复杂税收计算的结果.计算(可能还有它的依赖关系)会不时更改,因此我想严格封装它.

Suppose one of the things I need to display is the results of a complex tax calculation. The calculation (and possibly its dependencies) will change from time to time, so I would like to keep it strictly encapsulated.

这里最常提供的解决方案似乎是获取税项所依赖的所有属性,以在ModelView中针对属性本身以及依赖它的每个属性调用PropertyChanged.这意味着每个属性都需要知道使用或可能使用它的所有内容.当我的税收规则以某种方式改变,使计算依赖于以前不依赖的事物时,我将需要触摸计算中使用的所有那些新属性(可能在其他类中,可能不在我的控制下),让他们调用PropertyChanged作为税额.这完全浪费了封装的希望.

The solution most often offered here seems to be to get all of the properties on which the tax value depends to invoke PropertyChanged in the ModelView for the property itself and for every property that depends on it. That means that every property needs to know everything that uses or might use it. When my tax rules change in a way that makes the calculation dependent on things it was not previously dependent on, I will need to touch all those new properties that go into my calculation (possibly in other classes, possibly not under my control), to get them to invoke PropertyChanged for the tax value. That completely trashes any hope of encapsulation.

我能想到的最好的解决方案是使进行计算的类接收 PropertyChanged事件,并在计算中发生任何变化时针对税额引发一个新的PropertyChanged事件.至少可以保留 class 级别的封装,但是仍然违反 method 封装:类不必知道方法的工作方式.

The best solution I can think of is to make the class that does the calculation receive PropertyChanged events, and raise a new PropertyChanged event for the tax value when anything changes that goes into the calculation. That at least preserves encapsulation at class level, but it still breaches method encapsulation: the class shouldn't have to know about how a method does its work.

所以,我的问题是,有没有更好的方法(如果是,这是什么)?还是封装表示(MVVM)防止封装业务逻辑?我要面对一个选择吗?

So, my question is, is there a better way (and if so, what is it)? Or does encapsulation of presentation (MVVM) prevent encapsulation of the business logic? Am I faced with an either/or choice?

推荐答案

这里最常提供的解决方案似乎是获取税项所依赖的所有属性,以在ModelView中为属性本身以及依赖于此属性的每个属性调用PropertyChanged. ....

The solution most often offered here seems to be to get all of the properties on which the tax value depends to invoke PropertyChanged in the ModelView for the property itself and for every property that depends on it. ....

是的,但仅适用于该对象:每个属性都应在设置器中触发其自己的属性更改事件.另外,setter应该以某种方式触发依赖于其自身值的属性.您不应尝试主动触发其他对象的更新:它们应侦听此对象PropertyChanged.

Yes, but only for that object: Each property should fire its own property change event in the setter. Additionally the setter should somehow trigger properties that depend on that value on itself. You should not try to trigger updates on other objects proactively: they should listen to this objects PropertyChanged.

我能想到的最好的解决方案是使进行计算的类接收PropertyChanged事件,并在计算中发生任何更改时引发一个新的PropertyChanged事件以获取税额.至少可以保留类级别的封装,但是仍然违反了方法封装:类不必知道方法的工作方式.

The best solution I can think of is to make the class that does the calculation receive PropertyChanged events, and raise a new PropertyChanged event for the tax value when anything changes that goes into the calculation. That at least preserves encapsulation at class level, but it still breaches method encapsulation: the class shouldn't have to know about how a method does its work.

这确实是标准方法.每个类都有责任监视其依赖的属性,并为其引发属性更改事件.

This is indeed the standard way. Each class has the responsibility to monitor the properties that it depends on, and fire property change events for the properties it.

可能有一些框架可以帮助您做到这一点,但是值得知道应该发生什么.

There are probably frameworks that will help to do this for you, but it is worthwhile to know what should happen.

这篇关于MVVM更新计算的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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