为什么两个模特不应该互相交谈

发布于 2024-09-30 17:40:21 字数 169 浏览 2 评论 0原文

嗯,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-10-07 17:40:21

如果您打算以任何方式规避 MVVM,您希望有一个充分的理由(否则您不妨将其编写为传统的代码隐藏并节省一些打字):)

有几个好处使用MVVM,主要是为了非常松散的耦合、单元测试和重用。

松耦合的基本原理是这样的:

  • 视图知道如何显示某种形状的数据,但不知道数据来自哪里。
  • ViewModel 提供某种形式的数据和设置,视图可以使用,但不知道谁在显示它。
  • 模型知道如何保存通用数据,但不知道谁在使用它
  • 控制器(大多数 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:

  • A View knows how to display data of a certain shape, but has no idea where the data comes from.
  • A ViewModel provides a certain shape of data and settings, that views can use, but has no idea who is displaying it.
  • A Model knows how to hold generic data, but no idea who is consuming it
  • A Controller (missing from most MVVM explanations) decides what data to fetch, what data to display and where to display it. This is where the decision making belongs.

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.

回心转意 2024-10-07 17:40:21

视图模型之间的互操作通常是非常合适的。毕竟,视图中的父/子关系涉及持有子视图模型集合的父视图模型。例如,在许多情况下,父视图中命令的可用性取决于其子视图的状态。有时将其委托给单独的控制器类是合适的,但通常也不值得为此付出努力。

这实际上取决于应用程序的整体复杂性。有时,这种努力是必要的;有时,这种努力是必要的。有时只是镀金。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文