【发布时间】:2016-05-17 07:51:57
【问题描述】:
我对这个问题有点困惑。我有一个在数据库中表示的实体Product。它看起来像 POCO。这是示例(为简单起见,我使用属性而不是 fluent api)。
public class Product
{
[Key]
public int Id { get; set; }
//other properties that have mapping to db
}
但现在我想避免AnemicDomainModel anti-pattern
所以我要用没有映射到数据库的方法和属性来填充Product 模型,所以我应该使用[Ignore]。
public class Product
{
[Key]
public int Id { get; set; }
[Ignore]
public object FooProperty { get; set; }
//other properties that have mapping to db
//other properties and methods that do not have mapping to db
}
我认为这种方式会破坏我的模型。在this article 我找到了可接受的解决方法。它的想法是将Product(域模型)和ProductState(存储在数据库中的产品状态)分开。所以Product 是ProductState 的包装器。
我真的很想知道其他开发者的看法。非常感谢您的回答。
我知道我真正的问题听起来是这样的:“我应该将数据模型和域模型分开吗?我可以将 EF 实体从 Anemic 更改为 Rich 吗?”
【问题讨论】:
标签: c# entity-framework orm domain-driven-design