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
You have 2 options:
For your example, something like:
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; }
}
Option 1 (Pseudo-SQL):
CREATE TABLE Customer (
// aggregate root
customerId int NOT NULL,
lastName VARCHAR(30),
// value object
street VARCHAR(100),
city VARCHAR(50),
zip VARCHAR(10)
CONSTRAINT PK_Customer PRIMARY KEY (customerId)
)
Option 2 (Pseudo-SQL):
// aggregate root
CREATE TABLE Customer (
customerId int NOT NULL,
lastName VARCHAR(30)
CONSTRAINT PK_Customer PRIMARY KEY (customerId)
)
// value object
CREATE TABLE Address (
customerId int NOT NULL, // same ID from Customer
street VARCHAR(100),
city VARCHAR(50),
zip VARCHAR(10)
CONSTRAINT PK_Address PRIMARY KEY (customerId)
)
toDomain(sqlResult) function to convert the query result in your domain objectone-table approach by default