简体   繁体   English

实体框架数据注释是否对DDD和关注点分离不利?

[英]Is Entity Framework Dataannotations bad for DDD and separation of concerns?

I've been working quite som time now to learn how to implement DDD, IoC and ensure there is a separation of concerns in my application. 我现在已经花了相当多的时间来学习如何实现DDD,IoC并确保应用程序中的关注点分离。 I'm also trying to get in to the way of Test First Development. 我还试图进入测试优先开发的方式。

Somehow Entity Framework gives med some trouble. 实体框架不知何故给医疗带来了麻烦。 I have a domain project that keeps my domain objects (many of them POCO classes now, but with some behavior). 我有一个保留我的域对象的域项目(现在有许多POCO类,但是有一些行为)。 It's legacy code, and I try to refactor it, but MVC realies heavily on Dataannotations for validation. 它是旧代码,我尝试对其进行重构,但是MVC大量依靠Dataannotations进行验证。

The datavalidation-attributes is set on the domain objects, which ties them to the entity framework. 在域对象上设置datavalidation-attributes,将其与实体框架联系起来。 Most instructional videos and documentation on EF also uses a lot of attributes on the domain objects. 关于EF的大多数教学视频和文档在域对象上也使用了很多属性。

So I wonder if this is really a good thing? 所以我想知道这真的是一件好事吗?

I like the idea of many of the attributes, but the consequence of putting them on the domian objects makes me feel that entity framework is the core of the application and not the domain objects. 我喜欢许多属性的概念,但是将它们放在domian对象上的结果使我感到实体框架是应用程序的核心,而不是域对象。

Do you have any tips on solving this, or is this how it should be? 您是否有解决此问题的提示,或者应该如何解决?

UPDATE: 更新:

The suggested answers below led me to this post answered by Ladislav Mrnka 以下建议的答案将我Ladislav Mrnka回答的帖子

To me that probably sounds like the best way to go if it solves my problem. 对我来说,这可能是解决我的问题的最佳方法。 He also points out the same problem I have with the attributes in the domain layer. 他还指出了域层中的属性存在的相同问题。

使用Code First Fluent API验证会更好吗?

Some attributes are relevant to EF and MVC - attributes that describe your class. 一些属性与EF和MVC有关-描述您的班级的属性。 A required field, minimum and maximum lengths, etc... If you really want to separate everything, I'd keep these attributes on your domain objects, put database stuff (foreign keys, etc...) in FluentAPI and MVC stuff (display names, etc...) on the view model. 必填字段,最小和最大长度等...如果您真的想分隔所有内容,我会将这些属性保留在域对象上,将数据库内容(外键等)放在FluentAPI和MVC内容中(在视图模型上显示名称等)。

If MVC 4 happens to support FluentAPI (I'm using MVC 3, which doesn't, don't know about 4), you should definitely use it. 如果MVC 4恰好支持FluentAPI(我正在使用MVC 3,但我不了解MVC 3),则一定要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM