Appearance
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
using System.Collections.Generic;
using Gryd.Core;
using Gryd.Domain.Primitives;
public sealed class Email : ValueObject
{
public string Value { get; }
public Email(string value)
{
Value = Guard.ValidEmail(value).ToLowerInvariant();
}
protected override IEnumerable<object?> GetEqualityComponents()
{
yield return Value;
}
}Examples
- Email, Address, Money
- DateRange, Coordinates
- Any immutable concept