当一个视图有一个视图模型时,客户-订单-产品关系/gui 可能会变得非常糟糕
我想我得到了某种知识闪现,但现在我完全困惑了。我查看了许多 mvvm 实现,例如 Ocean、Stuff、BBQShack、MVVM demo、WAF、Chinch1,2 等...
每个人都以不同的方式使用 MVVM...
有一个场景让我抓狂。我缺少理解,我希望有人能拨开我脑海中的阴云。
但在我想发表 Jeremy Likness 的评论之前:
”我相信大多数开发人员都同意视图应该只有一个视图模型。没有必要将多个视图模型附加到一个如果您考虑关注点分离,这是有道理的,因为如果屏幕上有一个绑定到“联系人视图模型”的“联系人小部件”,并且有一个绑定到“公司视图模型”的“公司小部件”,那么这些应该是这样的。是单独的视图,而不是具有两个视图模型的单个视图。”
场景:当一个视图有一个 ViewModel 或一个 ViewModel 绑定到一个 UserControl...
问题:我需要为每个 ViewModel 创建 3 个 UserControls CustomerView、OrderView、ProductView 吗?如果是这样,如何在后台同步 3 个 UserControls 和 3 个 ViewModels 之间的 GUI 中的数据绑定?使用消息系统?
如果您的答案是是,那么我为什么不创建一个绑定到 BillingViewModel 的大 UserControl 作为控制器,该控制器公开了 ObservableCollection CustomerListWithAllOrdersAndProducts 属性,该属性可以轻松绑定到顶部控件的数据上下文和所有 3 个网格(客户等...)在其下方绑定到当前数据上下文。然后,客户的更改会自动更改订单和产品...
当屏幕中不仅有 3 个用户控件(例如 DataGrid),而且还有带有 5 个文本框的客户公式和带有 3 个文本框的订单公式时,您将如何处理这种情况,您是否会将这些公式放入另外 2 个 UserControls 中,以便在窗口中拥有 5 个 UserControls,并且所有这些都由与 Messenger 系统通信的 ViewModel 进行“映射”。这可能会让人非常困惑……他们在大型 mvvm 应用程序中真的是这样做的吗?
I think I got some sort of knowledge flash and now I am totally confused. I looked at many mvvm implementations like Ocean,Stuff,BBQShack,MVVM demo,WAF,Chinch1,2 etc...
Everyone is doing MVVM differently somehow...
There is ONE scenario that drives me nuts. I am missing understanding and I hope someone can clear the clouds in my head.
But before I want to post a comment from Jeremy Likness:
" I believe most developers agree that a view should have exactly one viewmodel. There is no need to attach multiple viewmodels to a single view. If you think about separation of concerns, this makes sense, because if you have a "contact widget" on the screen bound to a "contact viewmodel" and a "company widget" bound to a "company viewmodel", these should be separate views, not a single view with two viewmodels."
Scenario: When a View has ONE ViewModel or a ViewModel is bound to ONE UserControl...
Question: Do I need to create 3 UserControls CustomerView, OrderView, ProductView for each ViewModel ? If so how do you synchronize the databinding in the GUI between the 3 UserControls with the 3 ViewModels in the background? Using a Messaging System?
If your answer is YES, then why should I not create ONE big UserControl bound to BillingViewModel acting as Controller which exposes the property ObservableCollection CustomerListWithAllOrdersAndProducts which can easily be bound to the top control`s datacontext and all 3 Grids(customer etc...) below it bind to the current datacontext. Then the changing of the customer would automatically change the orders and products ...
How would you handle this scenario when there are not only 3 UserControls like DataGrids in a Screen but there is also Customer formular with 5 textboxes and a Order formular with 3 textboxes, would you put those formulars in further 2 UserControls so you have 5 UserControls in the Window and all together are "mapped" by its ViewModel communicating with a Messenger system. That can get very confusing... Is it really this way they do it in big mvvm apps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您建议您需要创建 3 个控件(客户/订单/产品),因为您的设计要求您这样做。因此,这意味着您的 GUI 设计人员无法选择将客户字段与订单或产品字段交错。
现在这可能是也可能不是一个好主意......但我认为这是一个奇怪的限制。 (这意味着您不能将客户地址与订单确认一起重复,因为它位于不同的表中)。
这些 MVVM 设计风格之一比其他设计风格更正确并不那么简单。我建议您查看替代方案并选择能够帮助您解决问题的 MVVM 风格。这个 1:1 的教条似乎对你没有帮助,所以我会放弃它。
You are suggesting that you need to create 3 controls (Customer/Order/Product) because your design requires you to do so. So that means your GUI designer can't choose to interleave customer fields with Order or Product fields.
Now this may or may not be a good idea... But I think it is a weird restriction. (It kind of implies you can't repeat the customer adress together with the order confirmation because it is in a different table).
It just isn't as simple that one of these MVVM design flavors is more correct than others. I reccomend that you review the alternatives and choose the MVVM flavor that helps you to solve your problem. And this 1:1 dogma does not appear to help you, so I'd ditch it.