为什么两个模特不应该互相交谈
嗯,在 MVVM 模式中,两个模型不应该相互了解或相互交谈是一种很好的做法。这意味着您不应该在另一种模型中创建一个模型的对象,然后注册事件或执行类似的事情。
但是为什么,如果两个模型彼此认识有什么问题呢?我正在编写代码,如果我的两个模型彼此认识会发生什么。
Well , In MVVM pattern it is good practice that two models should not know or talk with each other. That means you should not create a object of one model in other and then register for event or performing similar things.
But why, what is wrong if two models know each other. I am writing the code and what will happen if both my models know each other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您打算以任何方式规避 MVVM,您希望有一个充分的理由(否则您不妨将其编写为传统的代码隐藏并节省一些打字):)
有几个好处使用MVVM,主要是为了非常松散的耦合、单元测试和重用。
松耦合的基本原理是这样的:
如果您需要 ViewModel 相互了解,那么您很可能缺少一个管理 2 个 ViewModel 和关联模型/视图的 Controller 对象。
如果您可以提供有关模型当前链接原因的更多信息,我也许可以建议替代方案。
If you are going to circumvent MVVM in any way you want to have a good reason (otherwise you might as well just write it as traditional code-behind and save some typing) :)
There are several benefits of using MVVM, which are mainly to do with very loose coupling, unit testing and reuse.
The basic principal of loose coupling goes something like this:
If you require your ViewModels to know about each other, then you are most likely missing a Controller object that manages your 2 ViewModels and associated Models/Views.
If you can provide more information about why your models are currently linked, I may be able to suggest an alternative.
视图模型之间的互操作通常是非常合适的。毕竟,视图中的父/子关系涉及持有子视图模型集合的父视图模型。例如,在许多情况下,父视图中命令的可用性取决于其子视图的状态。有时将其委托给单独的控制器类是合适的,但通常也不值得为此付出努力。
这实际上取决于应用程序的整体复杂性。有时,这种努力是必要的;有时,这种努力是必要的。有时只是镀金。
It's often perfectly appropriate for view models to interoperate with each other. Parent/child relationships in the view involve the parent view model holding a collection of child view models, after all. There are any number of circumstances in which, for instance, the availability of commands in the parent view depend on the state of its children. It's sometimes appropriate to delegate this to a separate controller class, but it's also often not worth the effort to do this.
It really depends on the overall complexity of your application. Sometimes that kind of effort is essential; sometimes it's just gold-plating.