在哪里处理 MVVM 视图模型中的逻辑?

发布于 2024-12-17 19:08:28 字数 656 浏览 0 评论 0原文

在初步阅读了 WPF 和 MVVM 后,我目前正在为数字 IO 控制器开发一个小型调试应用程序。在此应用程序中,我有一个视图模型树,如下所示:

IOControllerViewModel >配置视图模型> PortView模型> ChannelViewModels

这对应于业务对象,其中 IOController 包含一组配置,然后包含一个端口列表,然后端口列表包含一个通道列表,最终代表 IO 控制器的输入/输出通道。

但现在我的问题是在哪里处理“逻辑”?

例如我想切换输出通道的状态。 IOControllerViewModel 是当前唯一具有对业务对象的引用的实例,其中包含所需的 ToggleChannel() 方法。现在我看到三个选项:

  1. 将登录名放入 ChannelViewModel 中。从而通过 从我的根一直到叶的必要业务对象。

  2. 将逻辑放入 IOControllerViewModel 中。因此,一个事件或 ChannelViewModel 必须触发类似的东西,然后 由 IOControllerViewModel 处理。

  3. 引入一个控制器,它负责任何逻辑和 将控制器发布到所有视图模型。

  4. 还有其他选择吗?最佳实践?

多谢!

After some initial reading on WPF and MVVM i'm currently working on a small debug application for a digital IO controller. In this application i'v got a tree of view models, which looks like this:

IOControllerViewModel > ConfigurationViewModels > PortViewModels > ChannelViewModels

This corresponds to the business objects, where the IOController contains a set of Configurations, which then contains a list Ports, which then contains a list of Channels, which finally represent the input/output channels of the IO controller.

But now my question is where to handle the "logic"?

For example i want to toggle the state of a output channel. The IOControllerViewModel is currently the only instance, which has a reference to the business object, that contains the needed ToggleChannel() method. Now i see three options:

  1. Put the login inside the ChannelViewModel. Thus pass the
    necessary business object from my root all way down to the leafs.

  2. Put the logic inside the IOControllerViewModel. Thus an event or
    something similar must be fired by the ChannelViewModel and then
    handled by the IOControllerViewModel.

  3. Introduce a controller, which is responsible for any logic and
    publish the controller to all view models.

  4. Any other option? Best practice?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谁人与我共长歌 2024-12-24 19:08:28

通常我将逻辑放在使用它的层上,并使用消息传递系统(例如 PRISM 的 EventAggregator 或 MVVM Light 的 Messenger )在 ViewModel 之间进行通信。 (如果您有兴趣,我写了一篇关于此的简短文章 此处

在您的情况下,这取决于哪个层处理登录。例如,如果 LoginViewModel 成功验证了用户的身份,它将广播类似 UserAuthenticated 消息的内容,其中包含相关参数。然后,感兴趣的 ViewModel 可以订阅 UserAuthenticated 消息,并相应地处理它们。

请记住,对于 MVVM,您的 ViewModel 就是您的应用程序。该应用程序应该运行得很好,根本不需要任何视图(例如来自测试脚本)

Usually I put logic on the layer that uses it, and use a messaging system such as PRISM's EventAggregator or MVVM Light's Messenger to communicate between ViewModels. (If you're interested, I wrote a brief post about this here)

In your case, it depends on what layer handles the login. For example, if a LoginViewModel successfully authenticates a user, it would broadcast something like a UserAuthenticated message, containing relevant parameters. Interested ViewModels could then subscribe to UserAuthenticated messages, and handle them accordingly.

Keep in mind that with MVVM, your ViewModels are you application. The app should run just fine without any Views at all (such as from a Test script)

昨迟人 2024-12-24 19:08:28

如果我理解正确的话,您正在使用一种分层结构,该结构只能与一个 ViewModel(IOControllerViewModel)一起使用。所有这些配置、端口和通道都可以是其相应项的可观察属性或集合。

然后,您只需使用 IOControllerView,其中包含可访问所有配置的 ItemsControl。在每个配置项的 DataTemplate 中,您将有一个 ItemsControl,它使用端口作为 ItemsSource 等。

拥有超出需要的 ViewModel 并不好,特别是在不需要 View 的情况下,因为您可以使用 DataTemplate 来代替。

我希望我没听错。

If I understood you correctly you're using a hierarchical structure which could live with just one ViewModel, the IOControllerViewModel. All those Configurations, Ports and Channels could be observable properties or collections of its corresponding item.

You would then just use an IOControllerView which contains an ItemsControl which accesses all configurations. In the DataTemplate of each configuration item you would have an ItemsControl which uses the ports as ItemsSource and so on.

It's not good to have more ViewModels than needed, especially if there's no need for a View because you could use a DataTemplate instead.

I hope I got you right.

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