Skip to content

Commit

Permalink
Remove EventSourcedAggregate related files, attrs and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Jul 19, 2024
1 parent 1a93783 commit eea02e5
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 222 deletions.
4 changes: 1 addition & 3 deletions src/protean/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__version__ = "0.12.1"

from .core.aggregate import BaseAggregate, atomic_change
from .core.aggregate import BaseAggregate, apply, atomic_change
from .core.application_service import BaseApplicationService
from .core.command import BaseCommand
from .core.command_handler import BaseCommandHandler
Expand All @@ -9,7 +9,6 @@
from .core.entity import BaseEntity, invariant
from .core.event import BaseEvent
from .core.event_handler import BaseEventHandler
from .core.event_sourced_aggregate import BaseEventSourcedAggregate, apply
from .core.model import BaseModel
from .core.queryset import Q, QuerySet
from .core.repository import BaseRepository
Expand All @@ -33,7 +32,6 @@
"BaseEntity",
"BaseEvent",
"BaseEventHandler",
"BaseEventSourcedAggregate",
"BaseModel",
"BaseRepository",
"BaseSerializer",
Expand Down
44 changes: 43 additions & 1 deletion src/protean/core/aggregate.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Aggregate Functionality and Classes"""

import functools
import inspect
import logging
import typing
from collections import defaultdict
from typing import List

from protean.core.entity import BaseEntity
from protean.core.event import BaseEvent
from protean.core.value_object import BaseValueObject
from protean.exceptions import NotSupportedError
from protean.exceptions import IncorrectUsageError, NotSupportedError
from protean.fields import HasMany, HasOne, Integer, Reference, ValueObject
from protean.fields import List as ProteanList
from protean.reflection import fields
Expand Down Expand Up @@ -235,3 +237,43 @@ def __exit__(self, *args):
# Validate on exit to trigger invariant checks
self.aggregate._disable_invariant_checks = False
self.aggregate._postcheck()


def apply(fn):
"""Decorator to mark methods in EventHandler classes."""

if len(typing.get_type_hints(fn)) > 2:
raise IncorrectUsageError(
{
"_entity": [
f"Handler method `{fn.__name__}` has incorrect number of arguments"
]
}
)

try:
_event_cls = next(
iter(
{
value
for value in typing.get_type_hints(fn).values()
if inspect.isclass(value) and issubclass(value, BaseEvent)
}
)
)
except StopIteration:
raise IncorrectUsageError(
{
"_entity": [
f"Apply method `{fn.__name__}` should accept an argument annotated with the Event class"
]
}
)

@functools.wraps(fn)
def wrapper(*args):
fn(*args)

setattr(wrapper, "_event_cls", _event_cls)

return wrapper
183 changes: 0 additions & 183 deletions src/protean/core/event_sourced_aggregate.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/protean/core/event_sourced_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from protean import BaseEventSourcedAggregate, UnitOfWork
from protean import BaseAggregate, UnitOfWork
from protean.container import Element, OptionsMixin
from protean.exceptions import (
IncorrectUsageError,
Expand Down Expand Up @@ -30,7 +30,7 @@ def __new__(cls, *args, **kwargs):
def __init__(self, domain) -> None:
self._domain = domain

def add(self, aggregate: BaseEventSourcedAggregate) -> None:
def add(self, aggregate: BaseAggregate) -> None:
if aggregate is None:
raise IncorrectUsageError(
{"_entity": ["Aggregate object to persist is invalid"]}
Expand Down Expand Up @@ -63,7 +63,7 @@ def add(self, aggregate: BaseEventSourcedAggregate) -> None:
if own_current_uow:
own_current_uow.commit()

def get(self, identifier: Identifier) -> BaseEventSourcedAggregate:
def get(self, identifier: Identifier) -> BaseAggregate:
"""Retrieve a fully-formed Aggregate from a stream of Events.
If the aggregate was already loaded in the current UnitOfWork,
Expand Down
26 changes: 3 additions & 23 deletions src/protean/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ def factory_for(self, domain_object_type):
from protean.core.entity import entity_factory
from protean.core.event import domain_event_factory
from protean.core.event_handler import event_handler_factory
from protean.core.event_sourced_aggregate import event_sourced_aggregate_factory
from protean.core.event_sourced_repository import (
event_sourced_repository_factory,
)
Expand All @@ -463,7 +462,6 @@ def factory_for(self, domain_object_type):
DomainObjects.COMMAND_HANDLER.value: command_handler_factory,
DomainObjects.EVENT.value: domain_event_factory,
DomainObjects.EVENT_HANDLER.value: event_handler_factory,
DomainObjects.EVENT_SOURCED_AGGREGATE.value: event_sourced_aggregate_factory,
DomainObjects.EVENT_SOURCED_REPOSITORY.value: event_sourced_repository_factory,
DomainObjects.DOMAIN_SERVICE.value: domain_service_factory,
DomainObjects.EMAIL.value: email_factory,
Expand Down Expand Up @@ -568,7 +566,6 @@ def _resolve_references(self):
field_obj.to_cls,
(
DomainObjects.AGGREGATE,
DomainObjects.EVENT_SOURCED_AGGREGATE,
DomainObjects.ENTITY,
),
)
Expand All @@ -584,10 +581,7 @@ def _resolve_references(self):
cls = params
to_cls = self.fetch_element_cls_from_registry(
cls.meta_.part_of,
(
DomainObjects.AGGREGATE,
DomainObjects.EVENT_SOURCED_AGGREGATE,
),
(DomainObjects.AGGREGATE,),
)
cls.meta_.part_of = to_cls
case _:
Expand Down Expand Up @@ -810,12 +804,10 @@ def _validate_domain(self):
def _assign_aggregate_clusters(self):
"""Assign Aggregate Clusters to all relevant elements"""
from protean.core.aggregate import BaseAggregate
from protean.core.event_sourced_aggregate import BaseEventSourcedAggregate

# Assign Aggregates and EventSourcedAggregates to their own cluster
for element_type in [
DomainObjects.AGGREGATE,
DomainObjects.EVENT_SOURCED_AGGREGATE,
]:
for _, element in self.registry._elements[element_type.value].items():
element.cls.meta_.aggregate_cluster = element.cls
Expand All @@ -830,9 +822,7 @@ def _assign_aggregate_clusters(self):
part_of = element.cls.meta_.part_of
if part_of:
# Traverse up the graph tree to find the root aggregate
while not issubclass(
part_of, (BaseAggregate, BaseEventSourcedAggregate)
):
while not issubclass(part_of, BaseAggregate):
part_of = part_of.meta_.part_of

element.cls.meta_.aggregate_cluster = part_of
Expand Down Expand Up @@ -978,10 +968,7 @@ def _setup_event_handlers(self):

def _generate_fact_event_classes(self):
"""Generate FactEvent classes for all aggregates with `fact_events` enabled"""
for element_type in [
DomainObjects.AGGREGATE,
DomainObjects.EVENT_SOURCED_AGGREGATE,
]:
for element_type in [DomainObjects.AGGREGATE]:
for _, element in self.registry._elements[element_type.value].items():
if element.cls.meta_.fact_events:
event_cls = element_to_fact_event(element.cls)
Expand Down Expand Up @@ -1029,13 +1016,6 @@ def event_handler(self, _cls=None, **kwargs):
**kwargs,
)

def event_sourced_aggregate(self, _cls=None, **kwargs):
return self._domain_element(
DomainObjects.EVENT_SOURCED_AGGREGATE,
_cls=_cls,
**kwargs,
)

def domain_service(self, _cls=None, **kwargs):
return self._domain_element(
DomainObjects.DOMAIN_SERVICE,
Expand Down
Loading

0 comments on commit eea02e5

Please sign in to comment.