用asp.net mvc的4和的ViewModels的EntityFramework什么点 [英] ViewModels with asp.net mvc 4 and EntityFramework whats the Point

查看:121
本文介绍了用asp.net mvc的4和的ViewModels的EntityFramework什么点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与自己辩论什么在它使用实体框架项目创建视图模型类的地步?

I'm debating with myself whats the point in creating ViewModel classes in a project which uses Entity Framework?

目前,我有它使用的EntityFramework项目。我的解决方案基本上是结构是这样的:

I currently have a project which uses EntityFramework. My solution is structured basically like this:


  • UI项目(包含控制器和视图)

  • 示范项目(包含的EntityFramework模型)

  • 服务项目(包含跟示范项目来提供实体出来的样板工程向UI项目服务类)

我的控制器传递实体框架直来创建视图的实体。

My controllers pass the entities that Entity Framework creates straight to the view.

这是不错的,简单的。

在过去,我会创建单独的视图模型类,并从创建的EntityFramework这些视图模型实体映射。但是现在我在努力明白这一点。

In the past I would of created separate view model classes and mapped from the entities that EntityFramework creates to these view models. But now I'm struggling to see the point.

我目前正在帮助了上由实体框架生成查看模型实体映射的项目。实际上它使用 AutoMapper 来做到这一点。

I'm currently helping out on a project which map from the entities produced by entity framework to view models. It actually uses AutoMapper to do this.

现在这一切似乎想了很多的努力和code为非常小的收益。

Now this all seems like a lot of effort and code for very little gain.

我失去了一些东西在这里?

Am I missing something here?

推荐答案

我能想到的一对夫妇的情况下,当视图模型类的单独的层是一个很好的路要走,我会尽力解释他们而言一般的ORM工具和一般的MVC框架 - 请注意,无论是这两种情况是针对ASP.NET MVC框架与实体框架(甚至也不是编程在.NET中...)

I can think of a couple of cases when a separate layer of view model classes is a good way to go, and I'll try to explain them in terms of a general ORM tool and a general MVC framework - note that neither of these two cases are specific to ASP.NET MVC Framework with Entity Framework (nor even to programming in .NET...).

另外:请注意,下面的几段我的指专门查看车型。我将在这篇文章的末尾ADRESS像质量分配漏洞问题。

Also: please note that in the following few paragraphs I'm only referring to view models specifically. I'll adress problems like mass assignment vulnerabilities at the end of this post.

这是一个有点纯粹的目标 - 在一个真正的MVC应用程序,视图层只需要数据访问,它需要的时刻,而不是其他。视图模型对象现在变成了的规范的从视图层到控制器:,这是我要告诉你请求​​的视图中的数据的为了坚持基本原则MVC,要确保在什么数据来显示所有的决定都是由控制器进行。

This is a somewhat "purist" goal - in a true MVC application, the view layer only has access to data that it needs at the moment, and nothing else. The view model object now becomes a specification from the view layer to the controller: "This is the data I need to show the view you requested." In order to adhere to fundamental MVC principles, you want to make sure that all decisions on what data to display are made by the controller.

在换句话说,如果你想显示用户的姓和名,用户名和图片,你不需要(或希望)给视图层的对象,其中也有对用户的密码信息,角色(或者,采取一些属性可能不那么敏感,高度或中间名)。相反,你给视图为姓,名,用户名和图片属性的对象,并认为只决定如何present的数据。这样的话,你肯定就决定的什么的数据是在控制层presented住宿。

In other words, if you want to display a user's first and last name, username and picture, you don't need (or want) to give the view layer an object which also has information about the user's password, roles (or, to take some properties that might not be so sensitive, height or middle names). Instead, you give the view an object with properties for first name, last name, username and picture, and the view decides only how to present the data. That way, you're sure that the decision on what data is presented stays in the controller layer.

一些ORM工具 - 甚至有的那些返回规则物体的 1 - 使用非常复杂的方法来保持你从数据层获取对象的变化的轨迹,以使更改记录更轻松。例如,你可能会从你的数据存储中获取一个对象,该实例上改变一些属性,然后调用保存()法别的地方,而对象是在更新数据库。根据不同的ORM工具,转发你的ORM,实体视图层可以有consequenses中的任何范围,从性能问题(最糟糕的情况:数据库连接保持开放),有害的结果(例如,在视图层的错误改变了性质数据存储)。为了避免这些,把他们太远了应用程序管道之前重新映射你的实体真有规则物体,也没有关系你ORM工具。查看车型(许多)的一种方式来实现这一目标。

Some ORM tools - even some of those that return regular objects1 - use quite sophisticated methods to keep track of changes in the objects you get from the data layer, in order to make changing the records easier. For instance, you might get an object from your data store, change some properties on that instance and then call a save() method somewhere else, and the object is updated in the database. Depending on the ORM tool, forwarding your ORM-entities to the view layer can have any range of consequenses, from performance issues (worst case: database connections kept open) to unwanted effects (say, a bug in the view layer changes properties in the data store). To avoid those, re-map your entities to "true regular objects", that have no relation to your ORM tool, before sending them too far down the application pipeline. View models is one way (of many) to achieve this goal.

请注意,这是否必要与否完全取决于你的ORM工具。我不知道实体框架的内部运作近不够好,知道你有没有关心 - 但EF的(非常)早期的化身,这是一个问题,而不是使用code-First方法至少在。

Note that whether this is necessary or not is entirely dependent on your ORM tool. I don't know the inner workings of Entity Framework nearly well enough to know if you have to care - but in (very) early incarnations of EF this was a problem, at least when not using the Code-First approach.

不,不一定。您可以做无视图模型就好了,在这种情况下,他们的抽象只是另一层不真的,但复杂性添加任何东西到你的应用程序。这一切都归结为wheter你的ORM工具使您code的任何要求,以及你是否是一个纯粹的MVC。

No, not necessarily. You could be doing just fine without view models, and in that case they're just another layer of abstraction which doesn't really add anything but complexity to your application. It all boils down to wheter your ORM tool puts any requirements on your code, and whether you're an "MVC purist".

雀替,Mporta已经中的指出的质量分配,这些漏洞可能是一个问题。我同意这是一个严重的问题,但我不同​​意,它是通过使用解决的视图模式

Queti-Mporta already pointed out that mass assignment vulnerabilities could be a problem. I agree that this is a serious concern, but I don't agree that it is solved by using view models.

在我看来,一个的视图模式的是数据传输对象的从控制器到视图的,帮助控制器进行梳理和总结,应该是数据的显示的。为了避免这样质量分配漏洞的问题,我通常使用的编辑模型的,这是非常相似,查看模型,但的在另一个方向 - 即控制器。不是每个人都使得这个区别 - 如果你做还是不做,我不很在乎。但是使用这个词汇,我推荐的总是的使用的修改的,当你让用户改变你的数据,并且使用的视图的模型,只有当它有助于模型你。

To me, a view model is a data transfer object from the controller to the view, helping the controller to sort out and summarize the data that should be displayed. To avoid problems like mass assignment vulnerabilities, I usually use edit models, which are very similar to view models but in the other direction - i.e. to the controller. Not everyone makes this distinction - and I don't care very much if you do or not. But using this vocabulary, I'd recommend always using edit models when you let users alter your data, and using view models only when it helps you.

1 在.NET中通常被称为POCO的,还是普通老式CLR对象。 Java有它的POJO的(普通Java对象)等价,如果你能想到,可以在面向对象的编程中使用的语言,这种语言也有一个等价。

1 In .NET usually referred to as "POCO's", or Plain Old CLR Objects. Java has its equivalence in POJO's (Plain Old Java Objects) and if you can think of a language that can be used in object oriented programming, that language also has an equivalence.

这篇关于用asp.net mvc的4和的ViewModels的EntityFramework什么点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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