ValueObject
Base class for immutable value objects.
Concept
Value Objects are DDD patterns where:
- Identity does not matter, only values
- Immutable after creation
- Equality based on all properties
- No side effects
Methods
| Method | Description |
|---|---|
| GetEqualityComponents() | Override to return equality properties |
| Copy() | Creates a shallow copy |
| Equals(object?) | Value-based equality |
| GetHashCode() | Hash based on values |
Usage
csharp
public class Email : ValueObject
{
public string Value { get; }
public Email(string value)
{
Guard.ValidEmail(value);
Value = value.ToLowerInvariant();
}
protected override IEnumerable<object?> GetEqualityComponents()
{
yield return Value;
}
}Examples
- Email, Address, Money
- DateRange, Coordinates
- Any immutable concept