对于 MVVM 应用程序中 ModelView 文件不断增大,您会采取什么措施?
我正在编写一个 MVVM 应用程序,其中一个屏幕的尺寸相当大。该屏幕维护具有多个其他对象列表的对象,这些对象也在事务的上下文中进行编辑。
我已将视图分解为多个用户控件。该模型分为不同的类类型。问题出在视图模型上。由于它聚合来自多个对象类型的信息并传递如此多的属性,因此最终可能会包含数千行代码。这些代码都不复杂,只是感觉不对。
这是该模式不可避免的结果吗?
在这种情况下我应该查看多个 ViewModel 吗?可能每个模型类别一个。
人们如何处理现实世界中的重要示例(而不是另一个演示)?
谢谢
顺便说一句:WPF/Prism/C#/MVVM 环境
I am writing a MVVM application and one of the screens is quite significant in size. The screen maintains and object that has multiple lists of other objects that also get editied within the context of the transaction.
I've broken the views up into multiple user controls. The model is broken up into the different class types. The problem is the ViewModel. Because it agregates information from multiple object types and does pass-through on so many properties, it is likely to be several thousand lines of code by the end. None of this code is complex, it just feels wrong.
Is this an unavoidable consequence of the pattern?
Should I be looking at multiple ViewModels in this case? Possibly, one per model class.
How have people handled non-trivial examples in the real world (as opposed to yet another demo)?
thanks
BTW: WPF/Prism/C#/MVVM environment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尝试为每个视图维护一个视图模型。当涉及到 ViewModel 之间的通信时,这似乎对我来说效果很好......有多种方法可以处理这个问题。通常我使用 Josh Smith 的 MVVM Foundation 中的 Messenger 类。
但底线是,ViewModel 确实没有理由变得大得离谱。总有一些方法可以构建一个项目,这样就不会出现任何一个部分完全失控的情况。
华泰
I try to maintain a ViewModel for each View. This seems to work well for me, when it comes to communicating between the ViewModels ... there's a number of ways to handle this. Usually I use the Messenger class from Josh Smith's MVVM Foundation.
Bottom line though, there's really no reason for anyone ViewModel to get ridiculously large. There's always some way of architecting a project so that no one piece gets completely out of hand.
HTH
臃肿的ViewModel往往是View臃肿的标志,也许它可以划分为子视图?
就我个人而言,我经常发现 ViewModel 中的大部分代码通常是样板代码,用于让视图知道某些属性已更新 (INotifyPropertyChanged)。看看 Ayende 解决这种膨胀的方法:
http://ayende.com/Blog/archive/2009/08/08/an-easier-way-to-manage-inotifypropertychanged.aspx
A bloated ViewModel is often a sign of a bloated View, maybe it can be divided into subviews?
Personally I often find that much of the code in the ViewModels is often boiler plate code to let the view know that some property has been updated (INotifyPropertyChanged). Take a look at Ayende's approach to solve that kind of bloating:
http://ayende.com/Blog/archive/2009/08/08/an-easier-way-to-manage-inotifypropertychanged.aspx
为什么你的 ViewModel 文件会膨胀?
与任何其他类一样,您应该能够将代码提取给较小的协作者,然后使用委托。虚拟机之间的通信可以通过命令/事件/方法调用进行。
虚拟机应该将另一个视图或更高级别的虚拟机视为同一事物(另一个客户端)。
Why are your ViewModel files bloating up?
Like any other class you should be able to extract code out to smaller collaborators and then use delegation. Communication between VMs can be via Commands/events/method calls.
A VM should treat another view or a higher-level VM as the same thing (another client).