DDD,值对象和ORM [英] DDD, value objects and ORM

查看:116
本文介绍了DDD,值对象和ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值对象没有身份. ORM需要标识来更新数据库.

Value objects do not have identity. ORM needs identity to update the database.

如何欺骗ORM?

(将值对象的ID标记为Internal无效,因为ORM位于不同的程序集中,并且不能将其移动到相同的程序集.)

(Marking Id for value object as internal won't work because ORM lives in a different assembly and moving it to the same assembly is not acceptable).

谢谢.

推荐答案

就我对DDD的理解而言,值对象只是对实体进行分区的一种方法.如果值对象应使用ID存储在数据库中,则它不是值对象.

As far as my understanding of DDD goes value objects are just a way to partition your entities. If a value object should be stored with an ID in the database it's not a value object.

示例:

域模型如下所示(C#):

The domain model looks like this (C#):

public class Customer : Entity
{
    public Guid CustomerID { get; }

    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address : ValueObject
{
    public string Street { get; set; }

    public string City { get; set; }

    public string ZipCode { get; set; }
}

相应的数据库表应如下所示(伪SQL):

The corresponding database table would look something like this (Pseudo-SQL):

CREATE TABLE Customers
(
    CustomerID,

    LastName,

    HomeAddress_Street,

    HomeAddress_City,

    HomeAddress_ZipCode,
)

要将地址存储在单独的表中,可以将其设置为具有ID的实体.

To store the addresses in a seperate table you would make it an entity which has an ID.

这篇关于DDD,值对象和ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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