如何在实体框架中处理值对象? [英] How to deal with value objects in Entity Framework?

查看:53
本文介绍了如何在实体框架中处理值对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不污染域模型的情况下将值对象持久保存在Entity Framework中? EF(一般来说,关系数据库)要求我定义一个键-例如,我的值对象没有现成的键

 公共类Tag:ValueObject< Tag> 
{
私有只读字符串名称;

公共标签(字符串名称)
{
this.name = name;
}

公共字符串Name {get {return this.name; }}
}

另一方面,我不应该在模型。我真的应该创建另一个类,该类包含值对象的所有字段以及一个键属性,然后将它们相互映射吗?我宁愿不这样做。



也许有一个更优雅的解决方案?

解决方案

沃恩·弗农(Vaughn Vernon)在他的绝妙书实施域驱动设计中谈到了持久化价值对象(第248页)。。 p>

ORM和单一值对象


基本概念将值的每个属性存储在存储其父实体的行的单独列中。换句话说,单个值对象被非规范化为其父实体的行。为列命名采用约定可以清楚地标识和标准化序列化对象的命名方式。


ORM和许多由数据库实体支持的值


使用ORM和关系数据库持久保存值实例集合的一种非常简单的方法是将值类型视为数据模型中的实体。 (...)为此,我们可以使用层超类型




可以在这里找到C#中的示例有界上下文: https://github.com/VaughnVernon/ IDDD_Samples_NET


How do I persist value objects in Entity Framework without polluting my domain model? EF (well, relational DBs in general) require me to define a key - which my value objects don't have out of the box, for example

public class Tag : ValueObject<Tag>
{
   private readonly string name;

   public Tag(string name)
   {
      this.name = name;
   }

   public string Name { get { return this.name; }}
}

On the other hand, I shouldn't address persistence concerns in the model. Am I really supposed to create another class that includes all the fields from the value object plus a key property and then map them to each other? I'd rather not.

Is there maybe a more elegant solution?

解决方案

Vaughn Vernon writes about Persisting Value Objects (page 248) in his excellent book Implementing Domain-Driven Design.

ORM and Single Value Objects

The basic idea is to store each of the attributes of the Value in separate columns of the row where its parent Entity is stored. Said another way, a single Value Object is denormalized into its parent Entity's row. There are advantages to employing convention for column naming to clearly identity and standardize the way serialized objects are named.

ORM and Many Values Backed by a Database Entity

A very straightforward approach to persisting a collection of Value instances using an ORM and a relational database is to treat the Value type as an entity in the data model. (...) To accomplish this we can employ a Layer Supertype.

Sample bounded contexts in C# can be found here: https://github.com/VaughnVernon/IDDD_Samples_NET

这篇关于如何在实体框架中处理值对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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