spakky-domain¶
spakky-domain은 DDD 모델과 CQRS UseCase 계약을 제공하는 core 패키지입니다.
DDD 빌딩 블록 — Aggregate Root, Entity, Value Object, Domain Event, CQRS
플러그인 진입점¶
모델¶
AbstractDomainModel
¶
Bases: ABC
Base class for all domain models.
Aggregate root model for domain-driven design.
This module provides AbstractAggregateRoot for representing DDD aggregate roots that manage domain events and maintain consistency boundaries.
AbstractAggregateRoot
¶
Bases: AbstractEntity[EquatableT], 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 |
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, 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.
persisted_version
property
¶
Version this entity is known to have in its backing store.
Business-field changes keep regenerating version, while this value
stays at the version the entity was constructed with (the stored
version for an entity rebuilt from persistence) until
mark_persisted() is called. A repository compares this value with
the stored version to detect concurrent writes.
For an entity that has never been stored, this is simply its initial version, which no stored record can match.
Returns:
| Type | Description |
|---|---|
UUID
|
The version that the last known stored state carries. |
next_id()
abstractmethod
classmethod
¶
Generate next unique identifier for this entity type.
Returns:
| Type | Description |
|---|---|
EquatableT
|
New unique identifier. |
mark_persisted()
¶
Record that the current version is now the stored version.
Repositories call this after a successful write so that the same instance can be changed and saved again within one use case.
Source code in core/spakky-domain/src/spakky/domain/models/entity.py
validate()
¶
Validate entity state.
Subclasses may override this method to enforce entity invariants.
__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__()
¶
Compute hash based on entity identity.
Returns:
| Type | Description |
|---|---|
int
|
Hash of uid. |
__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
Value object model for domain-driven design.
This module provides AbstractValueObject for representing immutable domain concepts compared by their attributes rather than identity.
UnhashableFieldTypeError(field_name)
¶
Bases: AbstractSpakkyDomainError
Raised when a value object field type is not hashable.
Source code in core/spakky-domain/src/spakky/domain/models/value_object.py
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()
¶
Create copy of this value object.
Returns:
| Type | Description |
|---|---|
Self
|
Cloned value object. |
validate()
¶
Validate value object state.
Subclasses may override this method to enforce value object invariants.
__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
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(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. |
validate()
¶
Validate event state.
Subclasses may override this method to enforce event invariants.
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.
partition_key
property
¶
Get the key that pins this event to a single broker partition.
Returns:
| Type | Description |
|---|---|
str | None
|
None by default, which lets the broker spread events round-robin. |
str | None
|
Override to return a stable value (typically the aggregate id) so |
str | None
|
that every event of the same aggregate lands on one partition and |
str | None
|
keeps its relative publication order. |
애플리케이션 (CQRS)¶
Command pattern abstractions for CQRS.
This module provides base classes and protocols for implementing command use cases in CQRS architecture.
AbstractCommand
¶
Bases: ABC
Base class for command DTOs.
Commands represent intent to change system state.
ICommandUseCase
¶
Bases: ABC
Interface 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
Interface for asynchronous command use cases.
run(command)
abstractmethod
¶
Execute command asynchronously and return result.
The declaration uses Awaitable[ResultT_co] instead of
async def so that the covariant ResultT_co remains sound
under pyrefly's variance analysis. Concrete implementations may
still use async def run because Coroutine is a subtype of
Awaitable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
CommandT_contra
|
The command to execute. |
required |
Returns:
| Type | Description |
|---|---|
Awaitable[ResultT_co]
|
Awaitable resolving to the result of command execution. |
Source code in core/spakky-domain/src/spakky/domain/application/command.py
Query pattern abstractions for CQRS.
This module provides base classes and protocols for implementing query use cases in CQRS architecture.
AbstractQuery
¶
Bases: ABC
Base class for query DTOs.
Queries represent intent to read system state without modification.
IQueryUseCase
¶
Bases: ABC
Interface 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
Interface for asynchronous query use cases.
run(query)
abstractmethod
¶
Execute query asynchronously and return result.
The declaration uses Awaitable[ResultT_co] instead of
async def so that the covariant ResultT_co remains sound
under pyrefly's variance analysis. Concrete implementations may
still use async def run because Coroutine is a subtype of
Awaitable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
QueryT_contra
|
The query to execute. |
required |
Returns:
| Type | Description |
|---|---|
Awaitable[ResultT_co]
|
Awaitable resolving to the query result. |