Value Objects

As discussed thoroughly in Value Objects (6), this is a pattern that can greatly simplify development and the evolution of rich domain models. Using Value Objects, we compose cohesive primitive types into an explicitly named immutable type. For instance, instead of declaring the identity of a project as a long, we would model an explicit ProjectId:

public struct ProjectId {   public readonly long Id { get; private set; }   public ProjectId(long id)   {     Id = id   }   public override ToString() {     return string.Format("Project-{0}", Id);   } }

We still use a long type to hold the actual identity number, but we use the ProjectId type to distinguish it from all others. Value types are certainly ...

Get Implementing Domain-Driven Design now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.