- Create Value[T] (frozen dataclass + ABC + Generic) in domain/framework/ - Migrate ClassifiedAdTitle and ClassifiedAdText to inherit Value[str] - Standardize all value access via .value instead of type-specific fields
1.7 KiB
1.7 KiB
Plan: Value Object Base Class
Goal
Create a generic Value[T] base class in the framework layer. All value objects standardize on .value access via inheritance.
Design
Value[T] base class (domain/framework/value.py)
@dataclass(frozen=True)
class Value(ABC, Generic[T]):
value: T
- Frozen dataclass → immutability + structural equality (core Value Object semantics)
Generic[T]→ type-safe.valueaccess in subclasses (e.g.,Value[str])ABC→ mirrors theEntitypattern
Subclasses
ClassifiedAdTitle(Value[str])— dropstitle: str, inheritsvalue: Tbound tostrClassifiedAdText(Value[str])— dropstext: str, inheritsvalue: Tbound tostr
Ripple effects
Every .title / .text reference on these objects becomes .value:
classified_ad.py—self.title.title→self.title.value, construction calls, method bodies- Tests — assertions, constructions, equality checks
Files changed
| File | Change |
|---|---|
domain/framework/value.py |
New — Value[T] base class |
domain/framework/__init__.py |
Add Value re-export |
domain/marketplace/classified_ad_title.py |
Inherit Value[str], rename field |
domain/marketplace/classified_ad_text.py |
Inherit Value[str], rename field |
domain/marketplace/classified_ad.py |
Update .title/.text refs → .value |
tests/test_classified_ad_title.py |
Update refs |
tests/test_classified_ad_text.py |
Update refs |
tests/test_classified_ad.py |
Update refs |
Not in scope
ClassifiedAdId, UserId, Money, Price — stay as-is.
Verification
pytest tests/— all 63 tests passruff check .&&ruff format --check .— clean