DDD, value objects and ORM

Deadly 提交于 2019-12-18 00:42:53

问题


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 because ORM lives in a different assembly and moving it to the same assembly is not acceptable).

Thanks in advance.


回答1:


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.




回答2:


When Eric Evans talks about "entities have identity, Value Objects do not", he's not talking about an ID column in the database - he's talking about identity as a concept.

VOs have no conceptual identity. That doesn't mean that they shouldn't have persistence identity. Don't let persistence implementation cloud your understanding of Entities vs VOs.

See my post here.




回答3:


Personally I have the Id field in the value object - I treat it as another attribute of the value object (such as name, location etc).

It may not be true DDD but it works for me.



来源:https://stackoverflow.com/questions/949320/ddd-value-objects-and-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!