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

查看:14
本文介绍了计算属性的 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 事件.这至少保留了级别的封装,但它仍然违反了方法封装:类不应该知道一个方法是如何工作的.

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 中触发自己的属性更改事件.此外,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天全站免登陆