MVVM:我该如何将 ViewModel 转换为 Model?
每个人都在谈论用视图模型来包装模型。到目前为止还好。
但是当我想要持久化我的 Model 时,如何将 ViewModel 转换为 Model ?
将 ViewModel 中的所有属性读取到新的 Model 对象中似乎非常麻烦。
还有更好的方法吗?
everyone speaks about wrapping a model by a viewmodel. fine so far.
But when I want persist my Model, how do I convert a ViewModel into a Model ?
Reading all properties from the ViewModel into a new Model object seems very cumbersome.
Any better method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我目前正在将旧的 winforms 应用程序转换为 wpf mvvm 应用程序,并且我只是在我的视图模型上提供一个指向模型实际实例的属性。
从我的角度来看,在 viewmodel 中创建模型的副本是没有意义的,而且我不认为 MVVM 社区这么说。
当您开始使用实体框架或其他一些数据库orm时,它尤其没有意义,因为实体框架具有支持开箱即用的数据绑定的属性。另外,当您开始更改数据库时,您必须更新模型和视图模型。
编辑:你说得对,ef不支持INotifyCollectionChanged,但据我所知,它确实支持INotifyPropertyChanged,而且从表面上看,微软正在考虑在未来实现INotifyCollectionChanged发布。
我不认为 MVVM 的方式有正确或错误之分 - 我认为网络上每个不同的“权威”对其如何工作都有自己的解释。
I'm currently converting an old winforms app into a wpf mvvm application, and I'm just providing a property on my viewmodel that points to the actual instance of the model.
From my point of view, it doesn't make sense to create a replica of the model in the viewmodel, and I don't think that the MVVM community is saying that anyway.
It particularly won't make sense when you start using entity framework or some other database orm, because entity framework has properties that support databinding out of the box. Plus, when you start making changes to the database, you have to update the model, and the viewmodel.
Edit: You're right in that ef doesn't support INotifyCollectionChanged, but as far as I know, it does support INotifyPropertyChanged, and from the looks of it, Microsoft is thinking of implementing INotifyCollectionChanged in a future release.
I don't think that there is a right or wrong way for MVVM - I think that every different 'authority' on the web has their own interpretation of how it should work.
这个想法是 ViewModel 是为用户界面而设计的。换句话说,一个WPF表单有一个ViewModel,而ViewModel实际上可能使用多个模型(客户、订单等)。如果您的表单仅处理一个模型,那么它将接近于一对一的映射。
The idea is that the ViewModel is made for the user interface. In other words, one WPF form has one ViewModel, while the ViewModel may actually use multiple models (customers, orders, etc.). If your form is only dealing with one model, then it is going to be close to a 1-to-1 mapping.
仅在需要时将模型包装在视图模型中,否则视图将直接绑定到模型的属性。一个例子:
模型1:员工列表(员工有姓名和部门)
模型2:部门列表。
查看:显示员工和部门列表。当用户选择部门时,员工列表将被过滤。
ViewModel:提供当前选定的部门属性和过滤后的员工列表。
View 将直接针对模型 1 对部门列表进行数据绑定,但针对视图模型对当前选定的部门和筛选的员工列表进行数据绑定。 Viewmodel 将根据所选部门过滤员工列表。
Only wrap a model in a viewmodel when needed, otherwise the view will directly bind to the properties of your model. An example:
Model 1: list of employees (An employee has a name and department)
Model 2: list of departments.
View: Show list of employees and departments. When a user selects a department, the employee list gets filtered.
ViewModel: Provide a current selected department property and a filtered employee list.
View would databind the department list directly against the model 1 but the current selected department and filtered employee list against the viewmodel. Viewmodel will filter employee list depending on selected department.
如果您不想复制属性,我建议使用常规模型和 Django>=1.4 代理模型:
通过继承原始模型并将
proxy = True
添加到模型Meta
中,可以轻松定义代理模型,例如,请注意,代理模型不需要在
中定义>models.py
——我经常在管理站点上使用代理模型并在admin.py
中定义它们。希望这会有所帮助,或者像我这样的其他后来者也可以使用它。
If you don't want to replicate the properties, I'd suggest to use regular models and Django>=1.4 proxy models:
Proxy models are easily defined by inheriting the original model and adding
proxy = True
to your modelMeta
, e.g.Note that proxy models don't need to be defined in the
models.py
-- I often use proxy models on the admin site and define them in theadmin.py
.Hope this helps or that other late-comers like me can use this.