MVVM ViewModel 层的复杂数据结构

发布于 2024-09-06 16:13:26 字数 515 浏览 5 评论 0原文

我有大量的 MyFile 对象集合,它们之间以各种方式链接,就像意大利面条一样。 此外,我从这个集合中创建了一些按某些标准相等的项目的较小子集合。 (例如扩展名为 .txt 的所有文件、属于某个目录的所有文件等...)

基本上,我的业务逻辑中有复杂的链表结构。现在我想为此创建 ViewModel 为了准备查看,这就是我碰壁的地方。我只是不知道如何准备这个烂摊子 并仍然保持一切高效且有条理。

第一个问题是通过逐项枚举并创建 itemViewModel 将每个集合包装在 collectionViewModel 中 将为每个项目创建重复的 itemViewModel (因为一个项目可以包含在多个集合中)

第二个问题是如何保持所有内容更新?例如,如果业务逻辑中的 item1 更改了 item2 的引用 到 item3,然后 ViewModels 应该相应地更新它们。

我真的很想打破 MVVM 模式,尽管我不想要它,并把业务 + 表示逻辑 在一个对象/类中,因为这种意大利面结构对于我对 MVVM 的理解水平来说似乎有点太多了。

谢谢

I have large collection of MyFile objects which are linked in all kind of ways between each other like spaghetti.
In addition, from this collection I create smaller subcollections of some items that are equal by some criteria.
(for example all files with the extension .txt, all files that belong to certain directory etc...)

Basically I have complex structure of linked lists in my business logic. Now I want to create ViewModel for this
in order to prepare it for View and this is where I hit the wall. I just can't figure out how to prepare this mess
and still keep everything efficient and organized.

First problem is that wrapping each collection in collectionViewModel by enumerating item by item and creating itemViewModel
will create duplicate itemViewModel for each item (since one item can be contained in several collections)

Second problem is how to keep everything updated? If for example an item1 in business logic changes its reference from item2
to item3, then ViewModels should update them accordingly.

I am really tempted to break from the MVVM pattern here even though I dont want it, and put bussines + presentation logic
in one object/class since this spaghetti structure seems a bit too much for my level of understanding of MVVM.

Thanks

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

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

发布评论

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

评论(1

萤火眠眠 2024-09-13 16:13:26

也许我在这里叫错了树,但就这样吧。

您可以有一个模型,它充当所有文件对象的存储库,并且还公开 ItemAdded 和 ItemRemoved 事件以及 Query 方法。然后,您可以拥有一个通用的 ViewModel 类型来表示您在此模型上的视图(ViewModel),但通过编写查询来进行专门化。通过这种方式,您可以为您需要表示的每个视图拥有 ViewModel+Query(例如扩展名为 txt 的所有文件)实例。 ViewModel 将负责在模型上执行查询(通过调用查询方法),然后将结果转换为可观察的文件项(或您拥有的内容)集合。您可以通过订阅 ItemAdded 和 ItemRemoved 事件来更新 ViewModel 以响应模型更改。如果在 ItemRemoved 事件中您的 ViewModel 文件项集合包含该项,则将其删除。如果在 ItemAdded 事件中该项目与该 ViewModel 实例的查询条件匹配,则将其添加到集合中。

这允许您为所有文件使用一个模型,然后为您希望表示的每种视图类型提供一个 ViewModel(+Query) 实例。 ItemAdded 和 ItemRemoved 事件允许您更新 ViewModel。由于 ViewModel 中的项目是可观察的集合,因此您的数据绑定视图将自行更新。

Maybe I'm barking up the wrong tree here, but here goes.

You could have a Model which acts as a repository for all your file objects, and that also exposes an ItemAdded and ItemRemoved event, plus a Query method. You can then have a common ViewModel type that represents your view on this model (a ViewModel), but specializes by composing a query. In this way you can have ViewModel+Query (e.g. all files with extension txt) instance for each view you need to represent. The ViewModel would be responsible for executing the query on your Model (by calling the query method) and then turning the results into an observable collection of file items (or what-have-you). You can update your ViewModel in response to Model changes by subscribing to ItemAdded and ItemRemoved events. If on an ItemRemoved event your ViewModel file items collection contains the item, then remove it. If on an ItemAdded event the item matches the query condition for that ViewModel instance, then add it to the collection.

This allows you to have a single Model for all your files, and then a ViewModel(+Query) instance for each type of view you wish to represent. The ItemAdded and ItemRemoved event allow you to update your ViewModel. As the items in the ViewModels are observable collections, your databound views will update themselves.

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