Overview
Value Objects are immutable objects that represent descriptive aspects of the domain with no conceptual identity. Unlike entities, they are defined by their attributes rather than a unique identifier.What are Value Objects?
In Domain-Driven Design, a Value Object is:- Immutable - Once created, it cannot be changed
- Identity-less - Equality is based on attribute values, not an ID
- Self-validating - Ensures it’s always in a valid state
- Replaceable - To “change” a value object, create a new one
When to Use Value Objects
Use Value Objects when:- The concept has no identity (e.g., Money, Address, DateRange)
- It measures, quantifies, or describes something in the domain
- It can be immutable
- Equality should be based on value, not identity
- It groups related attributes that should always be consistent together
Value Object Pattern
While the architecture doesn’t include a baseValueObject class, you can implement value objects following these patterns:
Basic Value Object Implementation
Advanced Value Object with Operations
Common Value Object Examples
Address Value Object
DateRange Value Object
Enums and Constants
The architecture includes examples of domain enums inDomain/Enums/Enums.cs:
Domain/Constants/Constants.cs:6:
Utility Classes
The architecture provides utility classes inDomain/Others/Utils/:
EnumUtils
Helper methods for working with enums (Domain/Others/Utils/EnumUtils.cs:5):
Best Practices
Immutability
Use readonly properties and private constructors. Create new instances instead of modifying existing ones.
Validation
Validate in the factory method to ensure invalid value objects cannot exist.
Equality
Implement
IEquatable<T> and override equality operators based on value, not reference.Factory Methods
Use static
Create() methods instead of public constructors for controlled instantiation.Value Objects vs Entities
| Aspect | Value Object | Entity |
|---|---|---|
| Identity | No unique identifier | Has unique ID |
| Equality | Based on values | Based on ID |
| Mutability | Immutable | Mutable through methods |
| Lifespan | Short-lived | Long-lived |
| Example | Money, Address | Customer, Order |
Integration with Entities
Use value objects as properties in your entities:Related Resources
- Domain Entities - Learn about entities that use value objects
- Domain Validators - Validate value objects within entities
- Domain Constants - Use constants in value objects