我刚刚开始使用 Prism 和 Unity 学习 WPF MVVM。
将视图与视图模型解耦效果很好,但我不知道如何连接我的视图模型和模型。
我觉得在我的视图模型中创建一个新的 EntityObject 是不对的。我已经浏览过 WAF BookLibrary 示例,但它相当庞大,并且在基本部分(视图和视图模型之间的绑定)周围添加了很多额外的东西,并且 Prism 文档没有说一句话(据我所知)它)关于视图模型 - 模型交互。
有谁知道一个好的来源可以解释如何以干净的方式使用视图模型和模型,或者我可以提供一些建议吗?
最好的问候
杰伊
I've just started learning WPF MVVM using Prism and Unity.
Decoupling the view from the viewmodel works pretty well, but I don't get how wire up my viewmodel and my model.
I doesn't feel right to just create a new EntityObject right in my viewmodel. I have already skim-read the WAF BookLibrary sample but it is quite bulky and adds a lot of extra stuff around the essential part (binding between view and viewmodel), and the Prism docs don't say a word (as far as I read it) about viewmodel - model interaction.
Does anyone know a good source that explains how to use viewmodel and model in a clean way or can me give some advise?
Best Regards
Jay
发布评论
评论(1)
对此每个人都会有自己的看法。就我个人而言,我不介意直接在视图模型中使用模型。对我来说,视图模型的整个想法是以一种可以被视图使用的方式扩展你的模型。
一个简单的例子是人员对象,它具有模型属性,例如姓名和年龄。当我到达视图模型阶段时,我可能会向它添加属性,例如可见性,这对模型本身没有意义。
另一点需要注意的是,我将模型视为数据,将视图模型视为上下文。因此,您可能有一个人的“卡片”视图模型,但也可能有一个“列表项”视图模型,它在不同的上下文中表示相同的模型,具有不同的视图模型特定属性。
我确实倾向于使用相关的接口来构建我的模型,并使用控制反转将它们注入到视图模型中,这样我的视图模型实际上知道的唯一一件事是它需要一个 IPerson 并且它将在构造函数中提供。
正如我所说,其他人会有不同的想法,所有这些都是正确的,由您决定哪一种适合您的需求。
Everyone will have their own opinions on this. Personally I don't mind using the model directly in the view model. For me, the whole idea of a view model is to extend your model in such a way that it can be consumed by a view.
A simple example of this would be person object, It would have model properties like like name and age for instance. When I get to the view model stage I may add properties to it like visibility which would not make sense on the model proper.
Another point to note is I would consider a model to be the data and the view model to be the context. So you may have a "Card" View Model for a person but you may also have a "List Item" view model that represents the same model in a different context, with different view model specific properties.
I do tend to make my models up using interfaces where relevant and use Inversion of control to inject them into the view model, that way the only thing my view model actually knows is that it needs an IPerson and that it will be provided in the constructor.
As I said other people will have different ideas, all are correct and its up to you to work out which one suits your needs.