EF4 + MVVM - 揭露实体视图模型? [英] EF4 + MVVM - Expose entities in ViewModel?

查看:242
本文介绍了EF4 + MVVM - 揭露实体视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与模型 - 视图 - 视图模型的一些不同的实现发挥各地和一致遇到的情况是,我不知道要进行正确的方法。我知道MVVM的目标之一就是从应用程序逻辑中分离视图,使逻辑可以在不景色的presence进行测试。把逻辑在一个ViewModel它没有相关性的视图解决了这个问题。大。甚至更好,如果该模型可以从视图模型以这样一种方式,它可以被模拟去耦

I've played around with some different implementations of Model-View-ViewModel and consistently come across a situation where I am not sure of the correct way to proceed. I know one of the goals of MVVM is to decouple the View from the application logic so that the logic can be tested without the presence of a View. Putting the logic in a ViewModel which has no dependencies on the View solves this problem. Great. Even better if the Model can be decoupled from the ViewModel in such a way that it can be mocked.

所以我的问题是,如果分离视图模型从视图模型?换句话说,它是确定的EntityFramework实体暴露通过视图模型视图?例如,假设有一个在视图中,用户可以选择一个国家对一地址的组合框。在AddressViewModel,国家应该公开为一个真正的实体类型的属性,还是应该公开为StateViewModel?如果它应该是一个StateviewModel类型的财产,我不明白怎么底层的模式应该是在AddressViewModel.State二传手内管理(因为什么被在属性设置为一个StateViewModel而不是一个国家实体)。

So my question is, should the ViewModel decouple the Model from the View? In other words, is it "ok" to expose EntityFramework entities to the View through the ViewModel? For example, say there is a combobox in the view where the user can choose a State for an address. In the AddressViewModel, should State be exposed as a real entity-type property, or should it be exposed as a StateViewModel? If it should be a StateviewModel-typed property, I don't understand how the underlying model should be managed within the AddressViewModel.State setter (because what is being set in the property is a StateViewModel and not a State entity).

在我看来,这可能会有两种方式,但似乎更一致从不直接暴露模型视图。思考?

It seems to me that this could go either way, but seems more consistent to never expose the model directly to the view. Thoughts?

推荐答案

视图模型的目的是为了分离从数据模型的观点。如果有在的偶合到数据模型视图中没有的功能,没有视图模式是必要的。

The purpose of the view model is to decouple the view from the data model. If there's no functionality in the view that's coupled to the data model, no view model is necessary.

如果您有其属性它的创建之后就不会改变,数据模型的对象的观点是不会改变,而且可以psented在UI $ P $无需格式化或转换,那么你再没有任何连接它的功能,通过直接将其公开的观点。你并不需要一个视图模型这一点。

If you have an object in the data model whose properties don't change after it's created, and that the view is not going to modify, and that can be presented in the UI without formatting or conversion, then you're not coupling any of its functionality to the view by exposing it directly. You don't need a view model for this.

在你的榜样,你也许可以蒙混过关,而无需创建一个 StateViewModel 类,因为这样一类不会真的做任何事情。

In your example, you probably can get away without creating a StateViewModel class, since such a class wouldn't really do anything.

这篇关于EF4 + MVVM - 揭露实体视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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