spakky-domain¶
DDD 빌딩 블록 — Aggregate Root, Entity, Value Object, Domain Event, CQRS
Models¶
spakky.domain.models.aggregate_root
¶
Aggregate root model for domain-driven design.
This module provides AbstractAggregateRoot for representing DDD aggregate roots that manage domain events and maintain consistency boundaries.
AggregateRootT = TypeVar('AggregateRootT', bound=(AbstractAggregateRoot[IEquatable]))
module-attribute
¶
Type variable for aggregate root types (invariant for repositories).
AggregateRootT_co = TypeVar('AggregateRootT_co', bound=(AbstractAggregateRoot[IEquatable]), covariant=True)
module-attribute
¶
Type variable for aggregate root types (covariant for read-only operations).
AggregateRootT_contra = TypeVar('AggregateRootT_contra', bound=(AbstractAggregateRoot[IEquatable]), contravariant=True)
module-attribute
¶
Type variable for aggregate root types (contravariant for input parameters).
AbstractAggregateRoot
¶
Bases: AbstractEntity[EquatableT_co], Generic[EquatableT_co], ABC
Base class for DDD aggregate roots.
Aggregate roots are entities that serve as entry points to aggregates, maintaining consistency boundaries and managing domain events.
events
property
¶
Get copy of all domain events raised by this aggregate.
Returns:
| Type | Description |
|---|---|
Sequence[AbstractDomainEvent]
|
Sequence of domain events. |
add_event(event)
¶
Add a domain event to this aggregate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AbstractDomainEvent
|
The domain event to add. |
required |
remove_event(event)
¶
Remove a domain event from this aggregate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AbstractDomainEvent
|
The domain event to remove. |
required |
options: show_root_heading: false
spakky.domain.models.entity
¶
Entity model for domain-driven design.
This module provides AbstractEntity base class for DDD entities with identity, validation, and immutability enforcement.
CannotMonkeyPatchEntityError
¶
Bases: AbstractSpakkyDomainError
Raised when attempting to add attributes not defined in the entity schema.
AbstractEntity
¶
Bases: AbstractDomainModel, IEquatable, Generic[EquatableT_co], ABC
Base class for DDD entities with identity and validation.
Entities are objects with unique identity that maintain consistency through validation and prevent unauthorized modifications.
uid
instance-attribute
¶
Unique identifier for this entity.
version = field(default_factory=uuid7)
class-attribute
instance-attribute
¶
Version identifier for optimistic locking.
created_at = field(default_factory=(lambda: datetime.now(UTC)))
class-attribute
instance-attribute
¶
Timestamp when entity was created.
updated_at = field(default_factory=(lambda: datetime.now(UTC)))
class-attribute
instance-attribute
¶
Timestamp when entity was last updated.
next_id()
abstractmethod
classmethod
¶
Generate next unique identifier for this entity type.
Returns:
| Type | Description |
|---|---|
EquatableT_co
|
New unique identifier. |
validate()
abstractmethod
¶
__eq__(other)
¶
Compare entities by identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Object to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if same entity type and uid. |
Source code in core/spakky-domain/src/spakky/domain/models/entity.py
__hash__()
¶
__post_init__()
¶
__setattr__(__name, __value)
¶
Set attribute with validation and rollback on failure.
Automatically updates updated_at and version when any attribute
(except metadata fields) is modified after initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__name
|
str
|
Attribute name. |
required |
__value
|
Any
|
New value. |
required |
Raises:
| Type | Description |
|---|---|
CannotMonkeyPatchEntityError
|
If attribute not in schema. |
Source code in core/spakky-domain/src/spakky/domain/models/entity.py
options: show_root_heading: false
spakky.domain.models.value_object
¶
Value object model for domain-driven design.
This module provides AbstractValueObject for representing immutable domain concepts compared by their attributes rather than identity.
AbstractValueObject
¶
Bases: AbstractDomainModel, IEquatable, ICloneable, IDataclass, ABC
Base class for immutable value objects.
Value objects represent domain concepts without identity, compared by their attributes. All fields must be hashable.
clone()
¶
validate()
abstractmethod
¶
Validate value object state.
Raises:
| Type | Description |
|---|---|
AbstractDomainValidationError
|
If validation fails. |
__eq__(__value)
¶
Compare value objects by attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__value
|
object
|
Object to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if same type and all attributes equal. |
Source code in core/spakky-domain/src/spakky/domain/models/value_object.py
__hash__()
¶
Compute hash from all hashable attributes.
Returns:
| Type | Description |
|---|---|
int
|
Hash of tuple containing all attributes (order-preserving). |
__post_init__()
¶
__init_subclass__()
¶
Verify all attributes are hashable.
Raises:
| Type | Description |
|---|---|
TypeError
|
If any attribute type is not hashable. |
Source code in core/spakky-domain/src/spakky/domain/models/value_object.py
options: show_root_heading: false
spakky.domain.models.event
¶
Domain event models for event-driven architecture.
This module provides base classes for domain and integration events in event-driven systems.
AbstractEvent
¶
Bases: AbstractDomainModel, IEquatable, IComparable, ICloneable, ABC
Base class for domain events.
event_id = field(default_factory=uuid4)
class-attribute
instance-attribute
¶
Unique identifier for this event.
timestamp = field(default_factory=(lambda: datetime.now(timezone.utc)))
class-attribute
instance-attribute
¶
When the event occurred.
event_name
property
¶
Get event type name.
Returns:
| Type | Description |
|---|---|
str
|
Class name of the event. |
clone()
¶
__eq__(other)
¶
Compare events by id and timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Object to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if same event type, id, and timestamp. |
Source code in core/spakky-domain/src/spakky/domain/models/event.py
__hash__()
¶
Compute hash from event id and timestamp.
Returns:
| Type | Description |
|---|---|
int
|
Hash of tuple containing event id and timestamp. |
__lt__(__value)
¶
Compare events by timestamp (less than).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__value
|
Self
|
Event to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this event occurred before the other. |
Source code in core/spakky-domain/src/spakky/domain/models/event.py
__le__(__value)
¶
Compare events by timestamp (less than or equal).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__value
|
Self
|
Event to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this event occurred before or at same time as the other. |
Source code in core/spakky-domain/src/spakky/domain/models/event.py
__gt__(__value)
¶
Compare events by timestamp (greater than).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__value
|
Self
|
Event to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this event occurred after the other. |
Source code in core/spakky-domain/src/spakky/domain/models/event.py
__ge__(__value)
¶
Compare events by timestamp (greater than or equal).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__value
|
Self
|
Event to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this event occurred after or at same time as the other. |
Source code in core/spakky-domain/src/spakky/domain/models/event.py
AbstractDomainEvent
¶
Bases: AbstractEvent, ABC
Base class for domain events.
Domain events represent state changes within the domain that other parts of the system may be interested in.
AbstractIntegrationEvent
¶
Bases: AbstractEvent, ABC
Base class for integration events.
Integration events are published across bounded contexts or services to communicate significant domain changes.
options: show_root_heading: false
Application (CQRS)¶
spakky.domain.application.command
¶
Command pattern abstractions for CQRS.
This module provides base classes and protocols for implementing command use cases in CQRS architecture.
CommandT_contra = TypeVar('CommandT_contra', bound=AbstractCommand, contravariant=True)
module-attribute
¶
Contravariant type variable for command types.
ResultT_co = TypeVar('ResultT_co', bound=Any, covariant=True)
module-attribute
¶
Covariant type variable for result types.
AbstractCommand
¶
Bases: ABC
Base class for command DTOs.
Commands represent intent to change system state.
ICommandUseCase
¶
Bases: ABC, Generic[CommandT_contra, ResultT_co]
Protocol for synchronous command use cases.
run(command)
abstractmethod
¶
Execute command and return result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
CommandT_contra
|
The command to execute. |
required |
Returns:
| Type | Description |
|---|---|
ResultT_co
|
Result of command execution. |
Source code in core/spakky-domain/src/spakky/domain/application/command.py
IAsyncCommandUseCase
¶
Bases: ABC, Generic[CommandT_contra, ResultT_co]
Protocol for asynchronous command use cases.
run(command)
abstractmethod
async
¶
Execute command asynchronously and return result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
CommandT_contra
|
The command to execute. |
required |
Returns:
| Type | Description |
|---|---|
ResultT_co
|
Result of command execution. |
Source code in core/spakky-domain/src/spakky/domain/application/command.py
options: show_root_heading: false
spakky.domain.application.query
¶
Query pattern abstractions for CQRS.
This module provides base classes and protocols for implementing query use cases in CQRS architecture.
QueryT_contra = TypeVar('QueryT_contra', bound=AbstractQuery, contravariant=True)
module-attribute
¶
Contravariant type variable for query types.
ResultT_co = TypeVar('ResultT_co', bound=Any, covariant=True)
module-attribute
¶
Covariant type variable for result types.
AbstractQuery
¶
Bases: ABC
Base class for query DTOs.
Queries represent intent to read system state without modification.
IQueryUseCase
¶
Bases: ABC, Generic[QueryT_contra, ResultT_co]
Protocol for synchronous query use cases.
run(query)
abstractmethod
¶
Execute query and return result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
QueryT_contra
|
The query to execute. |
required |
Returns:
| Type | Description |
|---|---|
ResultT_co
|
Query result. |
IAsyncQueryUseCase
¶
Bases: ABC, Generic[QueryT_contra, ResultT_co]
Protocol for asynchronous query use cases.
run(query)
abstractmethod
async
¶
Execute query asynchronously and return result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
QueryT_contra
|
The query to execute. |
required |
Returns:
| Type | Description |
|---|---|
ResultT_co
|
Query result. |
Source code in core/spakky-domain/src/spakky/domain/application/query.py
options: show_root_heading: false
Errors¶
spakky.domain.error
¶
options: show_root_heading: false