In my WPF MVVM application my model is a complex tree of Model objects wich constantly changes at runtime. Model instances come and go at runtime, change their position within the tree and of course change their many properties. My View is almost a one-to-one visual representation of that tree. Every Model instance is in 80% of the cases also a node in the tree.
在我的WPF MVVM应用程序中,我的模型是一个复杂的Model对象树,它在运行时不断变化。模型实例在运行时来来去去,改变它们在树中的位置,当然也会改变它们的许多属性。我的视图几乎是该树的一对一视觉表示。每个Model实例在80%的情况下也是树中的节点。
My question is now how I would design the ViewModel around this? My problem is that there are quite a lot of different Model types with each quite a lot of properties. If I understood MVVM corretcly the view should not communicate with the Model directly so this would mean that I would have to create a ViewModel type for each Model type and have to rewrap each property of the Model type in the ViewModel.
我现在的问题是如何围绕这个设计ViewModel?我的问题是,有很多不同的模型类型,每个都有很多属性。如果我理解MVVM,则视图不应直接与Model通信,这意味着我必须为每个Model类型创建一个ViewModel类型,并且必须在ViewModel中重新包装Model类型的每个属性。
Also the ViewModel would need to "bind" to the propertychanges of the Model to pass it along to the view (using wpf datatbinding). I would need some factory that creates and introduces a ViewModel instance for each Model that appears anew and I would habe to dispose each ViewModel instance when the corresponding Model disappears. I end up keeping track of all instances I created. It is unbelievable how much bloat code is generated dues to this double wrapping. Is this really a good approach? Each entity and each property more ore less exists twice and I have a lot of extra code keeping Model and View in sync. How do you handle this? Is there a more clever way to solve this?
此外,ViewModel需要“绑定”到Model的属性更改以将其传递给视图(使用wpf datatbinding)。我需要一些工厂为每个重新出现的Model创建并引入一个ViewModel实例,并且当相应的Model消失时,我会安排处理每个ViewModel实例。我最终跟踪我创建的所有实例。令人难以置信的是,这种双重包装会产生多少膨胀代码。这真是一个好方法吗?每个实体和每个属性更少的存在两次,我有很多额外的代码保持模型和视图同步。你怎么处理这个?有没有更聪明的方法来解决这个问题?
Does anyone have a reference/sample implementation for this that does it better than I do?
有没有人有这样的参考/样本实现比我做得更好?
1 个解决方案
#1
9
I think you may run into trap of paradigm if you follow this path. MVVM is nothing more than a pattern, which simplifies development in WPF world. If it doesn't - don't use it or revise your approach. I wouldn't spend 80% of my time just to check the "Using MVVM" field.
如果你遵循这条道路,我想你可能会陷入范式陷阱。 MVVM只不过是一种模式,它简化了WPF世界的开发。如果没有 - 不要使用它或修改你的方法。我不会花80%的时间来检查“使用MVVM”字段。
Now back to your question. Correct me if I'm wrong, but it sounds like you are looking at MVVM from opposite direction: you don't need Model to ViewModel one-to-one correspondence. Usually you create ViewModels based on your View first, and only then on a Model.
现在回到你的问题。如果我错了,请纠正我,但听起来你正在从相反的方向看MVVM:你不需要Model to ViewModel一对一的对应关系。通常,您首先根据View创建ViewModel,然后才在Model上创建。
Generally you look on a screen mockup from graphic designers, and create corresponding ViewModel, which takes all necessary fields from the Model, wraps/modify/format/combine them to make View development as easy as possible.
通常,您从图形设计器查看屏幕样机,并创建相应的ViewModel,它从模型中获取所有必需的字段,包装/修改/格式化/组合它们以使View开发尽可能简单。
You said that your View is almost one-to-one visual representation of the Model. In this case it may have sense to create a very simple ViewModel which exposes root object of your model-tree, and let View consume model directly via that property. Then if you need some View customizations or commands processing you can delegate that to ViewModel.
您说您的视图几乎是模型的一对一视觉表示。在这种情况下,可能有意义创建一个非常简单的ViewModel,它暴露模型树的根对象,并让View直接通过该属性使用模型。然后,如果您需要一些View自定义或命令处理,您可以将其委托给ViewModel。
Sorry for very vague answer. Maybe if you ask more specific question we could dispel the confusion :)...
很抱歉非常模糊的回答。也许如果你问更具体的问题,我们可以消除混乱:))
#1
9
I think you may run into trap of paradigm if you follow this path. MVVM is nothing more than a pattern, which simplifies development in WPF world. If it doesn't - don't use it or revise your approach. I wouldn't spend 80% of my time just to check the "Using MVVM" field.
如果你遵循这条道路,我想你可能会陷入范式陷阱。 MVVM只不过是一种模式,它简化了WPF世界的开发。如果没有 - 不要使用它或修改你的方法。我不会花80%的时间来检查“使用MVVM”字段。
Now back to your question. Correct me if I'm wrong, but it sounds like you are looking at MVVM from opposite direction: you don't need Model to ViewModel one-to-one correspondence. Usually you create ViewModels based on your View first, and only then on a Model.
现在回到你的问题。如果我错了,请纠正我,但听起来你正在从相反的方向看MVVM:你不需要Model to ViewModel一对一的对应关系。通常,您首先根据View创建ViewModel,然后才在Model上创建。
Generally you look on a screen mockup from graphic designers, and create corresponding ViewModel, which takes all necessary fields from the Model, wraps/modify/format/combine them to make View development as easy as possible.
通常,您从图形设计器查看屏幕样机,并创建相应的ViewModel,它从模型中获取所有必需的字段,包装/修改/格式化/组合它们以使View开发尽可能简单。
You said that your View is almost one-to-one visual representation of the Model. In this case it may have sense to create a very simple ViewModel which exposes root object of your model-tree, and let View consume model directly via that property. Then if you need some View customizations or commands processing you can delegate that to ViewModel.
您说您的视图几乎是模型的一对一视觉表示。在这种情况下,可能有意义创建一个非常简单的ViewModel,它暴露模型树的根对象,并让View直接通过该属性使用模型。然后,如果您需要一些View自定义或命令处理,您可以将其委托给ViewModel。
Sorry for very vague answer. Maybe if you ask more specific question we could dispel the confusion :)...
很抱歉非常模糊的回答。也许如果你问更具体的问题,我们可以消除混乱:))