如何自动生成 DataValidation“Buddy” ASP.NET MVC3 中数据库中的类?

发布于 2024-10-15 23:04:40 字数 267 浏览 1 评论 0原文

我有一个网站,其中的视图直接从实体框架创建的对象生成。阅读 此博客后Scott GU 看来让 DataValidation 工作的最好方法是从 POCO 对象开始。 (在链接上搜索“好友类”一词)

我可以以某种方式自动生成我的好友类吗?还是我一开始涉足 MVC 就走错了路?

I have a website that has the views generated directly from objects created by Entity Framework. After reading this blog by Scott GU it seems the best way to get DataValidation to work is to have started with POCO objects instead. (search for the word Buddy Class on the link)

Can I auto-generate my buddy classes somehow? Or did I start my foray into MVC on the wrong foot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清旖 2024-10-22 23:04:40

还是我一开始涉足 MVC 就走错了路?

恐怕情况确实如此,下面这句话证实了这一点:

我有一个网站,其中的视图直接从实体框架创建的对象生成

。您不应直接在视图中使用 EF 模型。拥有视图模型被认为是很好的做法。这些类是专门为满足给定视图的需要而设计的,并且可以包含格式化和验证。典型的场景可能如下:

  1. 请求控制器操作
  2. 控制器查询存储库以获取模型(如果愿意,可以使用 EF 模型)
  3. 控制器使用映射层将此模型转换为视图模型(这可以通过 < a href="http://automapper.codeplex.com/" rel="nofollow">AutoMapper)。
  4. 控制器将视图模型传递到视图

当控制器操作从视图接收视图模型、检查模型状态是否有效、将其映射回模型并将模型传递到存储库时,此场景也适用。

Or did I start my foray into MVC on the wrong foot?

I am afraid that this might be the case and the following sentence confirms it:

I have a website that has the views generated directly from objects created by Entity Framework

You shouldn't use your EF models in the views directly. It is considered good practice to have view models. Those are classes that are specifically designed to the needs of a given view and could contain formatting and validation. A typical scenario might be the following:

  1. A controller action is requested
  2. The controller queries the repository to fetch a model (EF model if you will)
  3. The controller converts this model into a view model using a mapping layer (this could be easily achieved with AutoMapper).
  4. The controller passes the view model to the view

This scenario works also when a controller action receives a view model from the view, checks whether the modelstate is valid, maps it back to a model and passes the model to a repository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文