应该在的ViewModels使用MVC每一个视图中使用吗? [英] Should ViewModels be used in every single View using MVC?

查看:166
本文介绍了应该在的ViewModels使用MVC每一个视图中使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白你使用一个ViewModel存储从其他型号和来源的视图中使用不同的数据,作为一种模式不会总是抱着你想要的东西。

I understand you use a ViewModel to store different data from other Models and sources to be used in a View, as a Model will not always hold what you want.

我想弄清楚它是否正确使用视图模型为每一个视图。我问的原因是出于一致性。你可以有一种观点认为,只需要对模型本身,而且必须有一个ViewModel另一个View。它的好处是这些所有的意见之间的混淆?还是应该每个View有一个视图模型?

I'm trying to figure out if it is proper to use a ViewModel for every single View. The reason I am asking is for consistency reasons. You could have a View that only needs the Model itself and another View that has to have a ViewModel. Is it good to mix these up between all your Views? Or should every View have a ViewModel?

这是很重要的,要知道因为我使用的实体框架4.1 code首先因为我的模型都直接关系到数据库中。

This is important to know since my Models are directly related to the database, as I'm using Entity Framework 4.1 Code First.

推荐答案

这不是必须的,但它是一个最好的做法。

It's not a requirement but it is a best practice.

您想尽可能多地从presentation分离数据库并具有视图模型(即使是相同的)为您提供了分离。这也让事情保持一致,这样你就不会有模型和一些一些看法没有。这种类型的设计让你觉得所有你希望你的意见,adhear并找出优化,看看是否重复数据可能发生的,不断considated一个位置等数据的数据。

You want to decouple your database from your presentation as much as possible and having a ViewModel (even if it is identical) gives you that seperation. It also keeps things consistent so you don't have some views with models and some without. This type of design makes you think about all the data you want your views to adhear to and figure out optimizations, see if duplication of data might be occurring, keep data considated to one location, etc.

想想你的视图模型像一个合约查看 ....这查看需要X的工作。

Think of your ViewModel like a contract with the View.... this View requires X to work.

多做点工作前面,但它会在年底还清。

Little more work up front but it will pay off in the end.

这篇关于应该在的ViewModels使用MVC每一个视图中使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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