从用户交互和服务事件更新视图模型的好模式是什么?
考虑以下两种场景:
- 用户在视图中按下按钮(例如Fulfill Order),我们希望视图立即更新(禁用按钮、添加进度条等)
- 服务层引发业务事件,最终必须反映在视图上(例如,产品缺货)。
这两种情况都需要某种机制,X,来更新视图模型。使用 MVVM,视图可以通过在事件处理程序中设置视图模型的属性、通过命令绑定或通过某种其他机制来实现此目的。
服务层可以使用某种机制来做到这一点,Y。例如,在业务/域模型中引发事件、创建操作视图模型的命令、调用视图模型上的方法等。
事实上,X 和 Y 可能是相同的机制(或模式)。
这样做有什么好处,既能保持 MVVM 的精神,又能保持 DRY 呢?
Consider these two scenarios:
- a user presses a button in a view (e.g. Fulfill Order) and we want the view to update immediately (disable the button, add a progress bar, etc.)
- a service layer raises a business event, which ultimately must be reflected on the view (e.g. a product has become out-of-stock).
Both cases legitimately require some mechanism, X, to update the viewmodel. With MVVM, the view can do this by setting properties of the viewmodel in an event handler, through command binding, or via some other mechanism.
The service layer can do this using some mechanism, Y. For example, raising events in the business/domain model, creating commands to manipulate the viewmodel, calling methods on the viewmodel etc.
In fact, X and Y could be the same mechanism (or pattern).
What's a good one to do this, that keeps to the spirit of MVVM, but is DRY?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要选择一个 MVVM 框架并遵循它支持的模式。
一般来说:
一个好的通用 MVVM 框架是 MVVM Light。
如果您正在寻找更强大的功能,并且可以处理更复杂的问题,请尝试 Caliburn。
或者,如果您想使用动态并尝试一些前沿的东西,请尝试我的框架:NoMvvm。
I think you need to pick an MVVM framework and follow the pattern for this that it supports.
In general:
A good, general-purpose MVVM framework is MVVM Light.
If you are looking for more power, and can handle more complexity, try Caliburn.
Or if you want to use dynamic and try something cutting edge, try my framework: NoMvvm.