"""Base value object for DDD domain layer.""" from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Any, Generic, TypeVar T = TypeVar("T") @dataclass(frozen=True, slots=True) class ValueObject(ABC, Generic[T]): """Base class for all value objects.""" value: T def __post_init__(self) -> None: self._validate() @abstractmethod def _validate(self) -> None: """Validate the value object. Raise ValueError if invalid.""" ... def __eq__(self, other: object) -> bool: if not isinstance(other, ValueObject): return False return bool(self.value == other.value) def __hash__(self) -> int: return hash(self.value) def __str__(self) -> str: return str(self.value) def to_primitive(self) -> Any: """Convert value object to primitive type.""" return self.value