MVVM的ViewModel层中的复杂数据结构 [英] Complex data structure in ViewModel layer of the MVVM

查看:249
本文介绍了MVVM的ViewModel层中的复杂数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我收集了许多MyFile对象,这些对象以各种方式彼此链接,例如意大利面条.
此外,从这个集合中,我创建了一些符合某些条件的项的较小子集合.
(例如,所有扩展名为.txt的文件,属于特定目录的所有文件等...)

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

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

第二个问题是如何使所有内容保持最新?例如,如果业务逻辑中的item1更改了对item2的引用
到item3,则ViewModels应该相应地对其进行更新.

我真的很想摆脱MVVM模式,即使我不想要它,而将bussines +表示逻辑放在
因为我对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

推荐答案

您好shtrber,

Hi shtrber,

同意您的想法,我认为需要为每个项目创建ViewModel并在ItemViewModel中声明一个引用该项目的成员.

Agree with your idea, I think it need to create the ViewModel for each item and declare a member that refernce to the item in the ItemViewModel.

对于第二个问题,我认为您需要使用INotifyPropertyChanged接口来实现观察者模式.它可以通知更新其他项目的不同项目属性,并且可以在ViewModel中设计逻辑.

For the second question, I think you need to use INotifyPropertyChanged interface to achieve an observer pattern. It can notify to update the different items proerties form the other item and the logic can be designed in the ViewModel.

CollectionViewModel
  |
  ---- Collection< ItemViewModel>

CollectionViewModel
 |
 ----Collection<ItemViewModel>

ItemViewModel:INotifyPropertyChanged 
   |
   ----项目

ItemViewModel : INotifyPropertyChanged 
   |
   ----Item

此致

鲍勃包


这篇关于MVVM的ViewModel层中的复杂数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆