DDD building blocks for Spakky Framework (Entity, AggregateRoot, ValueObject, DomainEvent, CQRS)
Project description
Spakky DDD
Domain-Driven Design building blocks for Spakky Framework.
Installation
pip install spakky-domain
Features
- Entities: Objects with unique identity and lifecycle
- Value Objects: Immutable objects compared by attributes
- Aggregate Roots: Consistency boundaries with event management
- Domain Events: Immutable events for event-driven architecture
- CQRS: Command and Query use case abstractions
Quick Start
Entity
Entities are objects with a unique identity that persists over time:
from dataclasses import dataclass
from uuid import UUID, uuid4
from spakky.domain.models.entity import AbstractEntity
@dataclass
class User(AbstractEntity[UUID]):
name: str
email: str
@classmethod
def next_id(cls) -> UUID:
return uuid4()
def validate(self) -> None:
if not self.email:
raise ValueError("Email is required")
Value Object
Value Objects are immutable and compared by their attributes:
from dataclasses import dataclass
from spakky.domain.models.value_object import AbstractValueObject
@dataclass(frozen=True)
class Money(AbstractValueObject):
amount: int
currency: str
def validate(self) -> None:
if self.amount < 0:
raise ValueError("Amount cannot be negative")
Aggregate Root
Aggregate Roots are entities that manage domain events:
from dataclasses import dataclass
from uuid import UUID, uuid4
from spakky.domain.models.aggregate_root import AbstractAggregateRoot
from spakky.domain.models.event import AbstractIntegrationEvent
@dataclass
class OrderCreatedEvent(AbstractIntegrationEvent):
order_id: UUID
customer_id: UUID
@dataclass
class Order(AbstractAggregateRoot[UUID]):
customer_id: UUID
total: int
@classmethod
def next_id(cls) -> UUID:
return uuid4()
def validate(self) -> None:
if self.total < 0:
raise ValueError("Total cannot be negative")
@classmethod
def create(cls, customer_id: UUID, total: int) -> "Order":
order = cls(uid=cls.next_id(), customer_id=customer_id, total=total)
order.add_event(OrderCreatedEvent(order_id=order.uid, customer_id=customer_id))
return order
Domain Events
Domain events represent state changes in the domain:
from dataclasses import dataclass
from spakky.domain.models.event import AbstractDomainEvent, AbstractIntegrationEvent
# Internal domain event
@dataclass
class UserRegistered(AbstractDomainEvent):
user_id: str
email: str
# Cross-boundary integration event
@dataclass
class UserRegisteredIntegration(AbstractIntegrationEvent):
user_id: str
email: str
CQRS Use Cases
Separate read and write operations:
from dataclasses import dataclass
from uuid import UUID
from spakky.domain.application.command import AbstractCommand, IAsyncCommandUseCase
from spakky.domain.application.query import AbstractQuery, IAsyncQueryUseCase
# Command
@dataclass
class CreateUserCommand(AbstractCommand):
name: str
email: str
class CreateUserUseCase(IAsyncCommandUseCase[CreateUserCommand, UUID]):
async def execute(self, command: CreateUserCommand) -> UUID:
# Business logic here
...
# Query
@dataclass
class GetUserQuery(AbstractQuery):
user_id: UUID
class GetUserUseCase(IAsyncQueryUseCase[GetUserQuery, User | None]):
async def execute(self, query: GetUserQuery) -> User | None:
# Business logic here
...
API Reference
Models
| Class | Description |
|---|---|
AbstractEntity[T] |
Base class for entities with identity type T |
AbstractAggregateRoot[T] |
Entity that manages domain events |
AbstractValueObject |
Immutable value object |
AbstractDomainEvent |
Base class for domain events |
AbstractIntegrationEvent |
Events for cross-boundary communication |
Application
| Class | Description |
|---|---|
AbstractCommand |
Base class for command DTOs |
AbstractQuery |
Base class for query DTOs |
ICommandUseCase |
Sync command use case interface |
IAsyncCommandUseCase |
Async command use case interface |
IQueryUseCase |
Sync query use case interface |
IAsyncQueryUseCase |
Async query use case interface |
Related Packages
| Package | Description |
|---|---|
spakky-data |
Repository and transaction abstractions |
spakky-event |
Event publisher/consumer interfaces and @EventHandler stereotype |
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spakky_domain-5.0.1.tar.gz.
File metadata
- Download URL: spakky_domain-5.0.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5556cc002423bf675f79756a484574664b7ae947d03b85a0fabea0908eddda2d
|
|
| MD5 |
4a08ae34b6fc399ff495fd341bf50d65
|
|
| BLAKE2b-256 |
ee35004055c6aee6aba10982325bb46dab526c537eb7e4516a8b94049d634e99
|
Provenance
The following attestation bundles were made for spakky_domain-5.0.1.tar.gz:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_domain-5.0.1.tar.gz -
Subject digest:
5556cc002423bf675f79756a484574664b7ae947d03b85a0fabea0908eddda2d - Sigstore transparency entry: 747186316
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@8f29a1a51bb96bbc2d471d88362357d886e543ea -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8f29a1a51bb96bbc2d471d88362357d886e543ea -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spakky_domain-5.0.1-py3-none-any.whl.
File metadata
- Download URL: spakky_domain-5.0.1-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfbcebfd91f65685f63730b63d709ca4ae72b06537ba46fa3c27ab511e6fc637
|
|
| MD5 |
c53fa742539ec7fcda1d57fdfb2e19c8
|
|
| BLAKE2b-256 |
1e2e5bdd387310ad9aca2e263d22e9f43a1e476595b95550337ccf0c0c970ae2
|
Provenance
The following attestation bundles were made for spakky_domain-5.0.1-py3-none-any.whl:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_domain-5.0.1-py3-none-any.whl -
Subject digest:
dfbcebfd91f65685f63730b63d709ca4ae72b06537ba46fa3c27ab511e6fc637 - Sigstore transparency entry: 747186317
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@8f29a1a51bb96bbc2d471d88362357d886e543ea -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8f29a1a51bb96bbc2d471d88362357d886e543ea -
Trigger Event:
workflow_dispatch
-
Statement type: