Search code examples
domain-driven-designspecificationsinvariants

Domain Driven Design; Can ValueObject contains invariants or specifications?


I'm starting to play with Domain Driven Design and have a question about ValueObjects :

Can they contains invariants or other specifications ?

Consider an immutable ValueObject :

ValueObject (

  prop integer: Int
  prop string: String

  // Value and copy constructor

  // Observers for integer and string

  // Equality methods on integer and string value

)

Can I add some invariants such that integer > 0 & < 42. Or do they have to be simple transfer without any logic ?

I hope they can but need a confirmation.


Solution

  • A value object (VO) encapsulates a value and its business requirements . This is its purpose: to model a business concept (with its constraints) which happens to be a simple (not always single) value.

    A VO is not a Data transfer object (DTO) precisely because it defines a business concept that is valid only in the containing bounded context, while a DTO is meant to cross boundaries.