模型视图到模型视图的通信
请问,对于以下场景,您建议的最佳(就架构质量而言)方法是什么:
ModelViewA (父级) - 需要从 ModelViewB 收集数据以显示数据 - 即使集合保持不变,也多次访问集合
ModelViewB (子级) -持有物品的集合。该集合在概念上属于 ModelViewB,并且主要在此(模型)视图内进行修改。但是,ModelViewA 也可以修改集合
。我当前采用的方法是在 ModelViewB 中拥有集合,并在 ModelViewA 中拥有副本。使用消息总线(MMVM Light 工具包),我让 ModelView 相互通知更改。然而,这感觉很尴尬,因为我保留了重复的集合并同步它。我宁愿只将它放在一个地方并从 ModelViewA 和 B 访问它。 我在想也许将一个 ModelView 注入到另一个 ModelView 中,但这会增加耦合,并且通常感觉 MVVM 模式是错误的。我也可以只使用对两个 ModelView 的静态引用,因为我有静态定位器(也来自 MVVM Light 工具包)保存引用。 或者也许有更好的解决方案?
谢谢,
奥
Please, what do you suggest is the best (in terms of architectural quality) approach to the following scenario:
ModelViewA (parent) - requires collection from ModelViewB to display data - access collection multiple times even when the collection remains unchanged
ModelViewB (child) - holds collection of items. The collection conceptually belongs to ModelViewB and is primarily modified within this (model)view. However, ModelViewA can also modify the collection
The current approach I take is having collection in ModelViewB and a duplicate in ModelViewA. Using messaging bus (MMVM Light toolkit) I have the ModelViews notify each other of the change. However, this feels awkward since I have keep the duplicate collection and synchronize it. I would much rather have it only in one place and access it from both ModelViewA and B.
I was thinking perhaps injecting one ModelView to another but that would increase coupling and generally feels wrong for MVVM pattern. I could also just use the static reference to both ModelViews since I have the static locator (also from MVVM Light toolkit) holding the references.
Or perhaps there is a better solution?
Thanks,
O
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在(有点)正在做这个。
我有一个 ConsoleViewModel,它记录和存储执行中的事件,以便在 ConsoleView 的 UI 中显示。当然,我的 ViewModel 希望与此控制台通信以记录它们的事件。
为了做到这一点,我创建了一个接口,它公开了我的 ViewModel 可用于将其事件写入控制台的方法。
我的 ConsoleViewModel 实现了此接口,并且所有其他 ViewModel 都公开了 IConsole 类型的公共属性,它们使用该属性写入控制台。
在此过程中,您可以使用多种方法来组合 ViewModel。您可以使用 DI 或简单的服务定位器,或者(正如我所做的那样)在资源中定义它们。
I'm (sort of) doing this right now.
I have a ConsoleViewModel which records and stores events from execution for display in the UI in my ConsoleView. Naturally, my ViewModels wish to communicate with this console so as to record their events.
In order to do this, I created an interface which exposes methods my ViewModels can use to write their events to the console.
My ConsoleViewModel implements this interface, and all other ViewModels expose a public property of type IConsole which they use to write to the console.
In doing this, you can use many methods of compositing your ViewModels. You can use DI, or a simple service locator, or (as I have done), define them in a resource.