Skip to content

Commit

Permalink
Moved hold_event() and collect_events() methods to base Aggregate cla…
Browse files Browse the repository at this point in the history
…sses in aggregate examples 6a and 7a.
  • Loading branch information
johnbywater committed Feb 7, 2024
1 parent 41c1caf commit dc9b7e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
26 changes: 13 additions & 13 deletions eventsourcing/examples/aggregate6a/domainmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ class Aggregate:
created_on: datetime
modified_on: datetime

def hold_event(self, event: DomainEvent) -> None:
all_pending_events[id(self)].append(event)

def collect_events(self) -> List[DomainEvent]:
try:
return all_pending_events.pop(id(self))
except KeyError: # pragma: no cover
return []

def __del__(self) -> None:
with contextlib.suppress(KeyError):
all_pending_events.pop(id(self))


TAggregate = TypeVar("TAggregate", bound=Aggregate)
MutatorFunction = Callable[..., Optional[TAggregate]]
Expand All @@ -55,19 +68,6 @@ class Dog(Aggregate):
name: str
tricks: Tuple[str, ...]

def __del__(self) -> None:
with contextlib.suppress(KeyError):
all_pending_events.pop(id(self))

def hold_event(self, event: DomainEvent) -> None:
all_pending_events[id(self)].append(event)

def collect_events(self) -> List[DomainEvent]:
try:
return all_pending_events.pop(id(self))
except KeyError: # pragma: no cover
return []


@dataclass(frozen=True)
class DogRegistered(DomainEvent):
Expand Down
26 changes: 13 additions & 13 deletions eventsourcing/examples/aggregate7a/domainmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ class Aggregate(BaseModel):
class Config:
frozen = True

def hold_event(self, event: DomainEvent) -> None:
all_pending_events[id(self)].append(event)

def collect_events(self) -> List[DomainEvent]:
try:
return all_pending_events.pop(id(self))
except KeyError: # pragma: no cover
return []

def __del__(self) -> None:
with contextlib.suppress(KeyError):
all_pending_events.pop(id(self))


class Snapshot(DomainEvent):
topic: str
Expand Down Expand Up @@ -78,19 +91,6 @@ class Dog(Aggregate):
name: str
tricks: Tuple[Trick, ...]

def __del__(self) -> None:
with contextlib.suppress(KeyError):
all_pending_events.pop(id(self))

def hold_event(self, event: DomainEvent) -> None:
all_pending_events[id(self)].append(event)

def collect_events(self) -> List[DomainEvent]:
try:
return all_pending_events.pop(id(self))
except KeyError: # pragma: no cover
return []


class DogRegistered(DomainEvent):
name: str
Expand Down

0 comments on commit dc9b7e4

Please sign in to comment.