ValueObject

Marks a class as a value object in domain-driven design (DDD).

Value objects represent descriptive aspects of the domain that have no conceptual identity. They are defined entirely by their values and should be immutable. Two value objects with the same values are considered equal and interchangeable.

Key characteristics of value objects:

  • No identity - equality based on values, not identity

  • Immutable - state cannot be changed after creation

  • Self-contained - all data needed for behavior is encapsulated

  • Replaceable - can be replaced with another instance having same values

  • Side-effect free - operations don't modify external state

Example usage:

@ValueObject
data class Money(
val amount: BigDecimal,
val currency: Currency
) {

operator fun plus(other: Money): Money {
require(currency == other.currency) { "Currency mismatch" }
return Money(amount + other.amount, currency)
}

companion object {
val ZERO = Money(BigDecimal.ZERO, Currency.USD)
}
}

@ValueObject
data class Address(
val street: String,
val city: String,
val postalCode: String,
val country: String
)

See also

for objects with identity

for aggregate root entities