DDD, value objects and ORM

前端 未结 5 1956
情书的邮戳
情书的邮戳 2020-12-14 09:56

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

How to trick ORM?

(Marking Id for value object as internal won\'t work bec

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 10:33

    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.

    Example:

    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; }
    }
    

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

    CREATE TABLE Customers
    (
        CustomerID,
    
        LastName,
    
        HomeAddress_Street,
    
        HomeAddress_City,
    
        HomeAddress_ZipCode,
    )
    

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

提交回复
热议问题