ASP.NET MVC - LINQ到实体模型视图模型 - 这是很好的做法? [英] ASP.NET MVC - Linq to Entities model as the ViewModel - is this good practice?

查看:221
本文介绍了ASP.NET MVC - LINQ到实体模型视图模型 - 这是很好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快捷提问真的。

我目前正在建设使用asp.net MVC和实体框架的网站。我有一对夫妇在地方库其中返回实体或实体的名单。我发现,在大多数我的网页我有从各种相关的表中提取数据。这是确定的,只要我使用加载的相关实体在我的查询'包括' - 但是这是很好的做法。

I am currently building a site using asp.net MVC and the entity framework. I have a couple of repositories in place which return entities or lists of entities. I am finding that in the majority of my pages I'm having to pull data from a variety of related tables. This is ok so long as I load up the related entities using 'include' in my queries - but is this good practice?

它会更好,以创建包含的信息,我需要的只是位自定义视图模型对象,或者是有什么错误与拉的对象图,也许是5 - 6表深只是为了显示你需要什么在你的看法呢?

Would it be better to create a custom viewmodel object that contains just the bits of info I need, or is there nothing 'wrong' with pulling an object graph that is perhaps 5 - 6 tables deep just to display what you need to in your view?

道歉,如果这个问题没有什么太大的意义。我可能从根本上误解了怎样的模式应该用在这里:)

Apologies if this question doesn't make too much sense. I may have fundamentally misunderstood how a model should be used here :)

感谢

推荐答案

我建议审查你的观点呈现code和在控制器张贴code。他们是由所采用的方法做过于复杂?如果没有你可能确定以保持事物的本来面目。如果视图和控制器code将通过引入一个自定义的视图模型可以大大简化再考虑创建之一。自定义视图模型基本上是抽象的一些是可能得到的那一刻处理别的地方的复杂性。

I would suggest reviewing the rendering code in your views and the posting code in your controllers. Are they made overly complex by the approach you are taking? If not you're probably ok to keep things as they are. If the view and controller code would be greatly simplified by introducing a custom view model then consider creating one. Custom view models essentially abstract some of that complexity that is probably getting handled somewhere else at the moment.

这篇关于ASP.NET MVC - LINQ到实体模型视图模型 - 这是很好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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