Files
python-fun/domain/framework/entity.py

27 lines
667 B
Python

from abc import ABC
from dataclasses import replace
from typing import Self
from domain.marketplace.domain_event import DomainEvent
class Entity(ABC):
_changes: tuple[DomainEvent, ...] = ()
def apply(self, event: DomainEvent) -> Self:
result = self.when(event)
result._ensure_valid_state()
return replace(result, _changes=(*result._changes, event))
def get_changes(self) -> tuple[DomainEvent, ...]:
return self._changes
def _ensure_valid_state(self):
pass
def when(self, event: DomainEvent) -> Self:
return self
def clear_changes(self) -> Self:
return replace(self, _changes=())