EF4 code-第一和模型,先用问候验证模型 [英] EF4 code-first vs model-first with regards to model validation

查看:136
本文介绍了EF4 code-第一和模型,先用问候验证模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个人的ASP.NET MVC 3项目(我有超过数据库架构和code完全控制),我试图将数据库第一和POCO瓦特/间来决定我的EF4模型,或者我应该去瓦特/ code-第一。

I'm working on a one-man ASP.NET MVC 3 project (I have complete control over database schema and code), and I'm trying to decide between going database-first and POCO w/ my EF4 models, or if I should go w/ code-first.

我想实现的主要是装饰我的机型DataAnnotation属性,这样我就可以强制执行之前,做任何持久性方案验证。综观<一个href=\"http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-$c$c-first.aspx\"相对=nofollow>斯科特Guthrie的有关模型验证文章 W / MVC 2,关于他的文章谈到与code一(步骤2),并用做做模型的第一个(或数据库-first)使用的哥们班(第五步)。

The main thing I'm trying to achieve is decorating my models with DataAnnotation attributes so that I can enforce schema validation prior to doing any persistence. Looking at Scott Guthrie's article about model validation w/ MVC 2, he talks about article about doing it with code-first (Step 2), and doing it with model-first (or database-first) using "buddy classes" (Step 5).

我历史上使用SQL Server设计GUI(和脚本)做我的数据库设计,所以我肯定与更高效,严格,当涉及到数据库设计。但是,除非我抛弃装饰我的模型W的想法/ DataAnnotation属性验证,我将不仅具有两类模型的属性,但不必在本质上,建立我的模式在两个地方违反干。

I historically have done my database design using the SQL Server designer GUI (and scripts), so I'm definitely more productive with that, strictly when it comes to database design. However, unless I ditch the idea of decorating my models w/ DataAnnotation attributes for validation, I will be violating DRY by not only having model properties in two classes, but having to, in essence, build my schema in two places.

我在找任何人的用了两种方法(甚至是一种方法)经验,可以提供哪种方式他们去,为什么他们决定的反馈,以及他们如何发现它的工作。我也想知道如果我可能是最好去一个完全不同的,使用工具,如流利的验证,或者甚至完全放弃域模型级验证,并保持我的验证的服务和视图模型。

I'm looking for anyone that's had experience with both methods (or even one method), and can offer feedback on which way they went, why they decided that, and how they found it to work. I'd also like to know if I might be better off going a completely different, using tools like Fluent Validation, or maybe even abandoning domain model-level validation altogether, and keeping my validation in the services and view models.

推荐答案

首先code首先是CTP,因此没有一个go-live许可。如果这是在未来几个月内交付项目则决定为示范第一。

Firstly Code First is in CTP and therefore doesn't have a go-live licence. If this is a project to be delivered in the next couple of months then the decision is Model first.

说了这么多,使用DataAnnotations code首先班比模型优先POCO吸尘器哥们类,但在我的经验中最重要的是明确意图。只要你的设计是明确的,最重要的是一致两种方法是合适的。

Having said that, Code First classes using DataAnnotations are cleaner than Model First POCO with buddy classes but the most important thing in my experience is explicit intent. As long as your design is clear and most importantly consistent either approach is suitable.

对于一个小项目(即一个人如你说)我会说你很可能会更富有成效与模型优先,并通过EDMX设计。这也对你的感觉从模式第一背景的更舒适。然而有一些篮球的你需要通过跳跃来获得POCO类很好的工作,如安装POCO T4模板,然后修改项目中的拉POCO的成一个单独的程序创建的T4模板。你不希望他们在DAL组件,它在那里他们将开始。你再与你是多么舒服部分类实现DataAnnotations决定离开;原因我不同意很多人看到这些穷的设计。

For a small project (i.e one man as you've stated) I'd say you're likely to be more productive with Model First and designing through an edmx. This will also feel more comfortable for you coming from a schema first background. There are however a number of hoops you'll need to jump through to get the POCO classes working nicely such as installing the POCO T4 template then modifying the T4 template created in your project to pull the POCO's into a separate assembly. You don't want them in the DAL assembly which is where they will start off. You're then left with the decision of how comfortable you are with partial classes for implementing DataAnnotations; for reasons I don't agree with many people see these as poor design.

在一个MVC项目,你会打与使用两种方法DataAnnotations当您决定使用的ViewModels无处不在干的问题。在这一点上,你会突然意识到,如果你是快乐的,直接送这些类视图模型进行验证的广泛注释是唯一有用的。如果您决定保留的意见,重量轻,使用的ViewModels必须重复的视图模型的DataAnnotations否则你留下了在模型级别验证错误,但没有进入的ModelState这个以外手动添加方式。无论是code第一或模型,因此需要相应的设计首先要解决这个问题尚未。我们亲自去与混合并接受打破DRY的水平。

In an MVC project you'll hit the ubiquitous DRY problem using DataAnnotations with either approach when you decide to use ViewModels. At this point you'll suddenly realise that extensive annotation of your Model for validation is only useful if you are happy sending these classes directly to the view. If you decide to keep the views lightweight and use ViewModels you have to repeat the DataAnnotations on the ViewModel otherwise you're left with validation errors at the model level but no way of getting this into ModelState other than manually adding. Neither code first or model first solve this problem as yet so you need to design accordingly. We personally went with a mix and accepted a level of breaking DRY.

这篇关于EF4 code-第一和模型,先用问候验证模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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