如何在 MVC 中的 EF CodeFirst Poco 类和 ViewModel 类之间共享 DataAnnotations?
我正在使用 EF-CodeFirst 从我的 Poco 对象及其配置创建数据库。 我大量使用 Dataannotation(Required、DataLength、RegularExpression、DataType)将元数据获取到 MVC,并让错误消息指向资源以便稍后对其进行本地化。
对于简单的 CRUD 操作这不是问题。
但是一旦我需要 ViewModel 类,我就会从 Poco 对象中释放数据注释。
如何将这两种设计结合起来而又不损害 DRY(不要重复自己)原则?
谢谢 安德烈亚斯
I am using EF-CodeFirst to create the database from my Poco objects and their configuration.
I have heavily used Dataannotation (Required, DataLength, RegularExpression, DataType) to get metadata to MVC and have the error messages pointing to a ressource to localize them later.
For simple CRUD operations this is not a problem.
But as soon as I have the need for ViewModel classes I loose the Dataannotations from my Poco objects.
How can those 2 designs be combined without having to hurt the DRY (dont repeat yourself) principle?
Thanks
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两种设计不能也不应该结合在一起。您的视图模型与视图相关联,因此我个人在视图模型上重新定义了所有必要的属性,无论您相信与否,在这种情况下我并不关心 DRY。我这样做是因为我的视图的要求经常变化(是的,客户是反复无常的),而我总是重用相同的域模型。对我来说,视图永远不应该知道域模型的存在。视图应该是虚拟的,它应该知道的只是控制器传递给它的视图模型。
我知道有些人可能有不同的想法,我尊重他们的意见。因此,我这样说只是表达我个人的观点和我使用 ASP.NET MVC 的方式。老实说,我的模型是 POCO 对象,没有任何 DataAnnotations,所以我不会遇到这样的问题。我在视图模型上使用 DataAnnotations 仅用于格式化目的(例如
DataType
、DisplayName
和DisplayFormat
等),没有验证属性。我使用 FluentValidation.NET 来实现此目的。Those two designs can't and shouldn't be combined. Your view models are tied to the views so personally I redefine all the necessary attributes on my view models and believe it or not I don't care about DRY in this case. I do this because the requirements of my views change quite often (yeah customers are capricious) whereas I reuse always the same domain models. For me a view should never know about the existence of a domain model. A view should be dummy and all that it should know about is the view model which is passed to it by the controller.
I know that there are people who might think different and I respect their opinion. So by saying this I am only exposing my personal point of view and my way of working with ASP.NET MVC. And to be quite honest with you my models are POCO objects without any DataAnnotations so I don't encounter such problems. I use DataAnnotations on my view models only for formatting purposes (stuff like
DataType
,DisplayName
andDisplayFormat
) and no validation attributes. I use FluentValidation.NET for this purpose.您可以很容易地完成您所要求的事情。 Darin 有一个非常有效的观点,非常适用于较大的项目,但我认为对于较小的项目来说,这听起来有点矫枉过正。这似乎在很大程度上取决于您是使用 DataAnnotations 进行验证还是使用第三方库。恕我直言,不这样做所涉及的重复会给维护带来相当大的麻烦。还有一个事实是,您花费大量代码行只是将数据从数据库复制到视图模型; ORM 应该可以帮助消除这个问题,但到目前为止,解决方案似乎并不是很好。
这是关于如何执行您想要的操作的示例代码片段。第一个类是部分类,主体位于 EF 生成的代码中(我首先是模型,而不是代码)。另外,作为一个例子,Question 有一个到 QuestionGroup 的 FK。在数据库中,这是一个名为 GroupID 的列,但在 ViewModel 中,有一个名为 GroupName 的属性,用于在一个漂亮的小网格中显示(实际上是 Telerik 网格,如果您尝试传入 EF 对象,它基本上会中断)。
恕我直言,EF 和 Telerik 都强迫我在这里不要太干燥,但我无能为力。 EF 应该默认生成这些 DataAnnotations(或者至少有选项),因为它显然知道哪些字段是必需的(不为空)或各种字符串的最大长度是多少。然后 Telerik 强迫我创建 ViewModel,这也很痛苦。
You can accomplish what you're asking pretty easily. Darin has a very valid point that applies well to larger projects, but I think for the smaller projects it sounds like overkill. It also seems like it depends a great deal on whether you're using DataAnnotations for validation or using a 3rd party library. The repitition involved in not doing this would make for quite a maintenance nuisance, IMHO. There's also the fact that you spend a lot of lines of code just copying data from the DB to the view model; ORMs are supposed to help eliminate that, but so far the solution doesn't appear very good.
Here's an example snip of code on how to do what you want. The first class is partial, with the main body being in the EF generated code (I'm going model first, not code first). Also, as an example, Question has a FK to QuestionGroup. In the DB this is a column named GroupID, but in the ViewModel there's a property named GroupName for displaying in a nice little grid (Telerik Grid, actually, which basically breaks if you try to pass in an EF object).
IMHO both EF and Telerik are forcing me to not be very DRY here, but there's nothing I can do. EF should be generating these DataAnnotations by default (or at least have the option), since it obviously knows which fields are required (not null) or what the max length of various strings are. Then Telerik forces me to create the ViewModel, which is also painful.