Skip to main content

Base classes for internal domain events with distributed tracing support

Project description

Python Domain Events

Base classes for internal domain events with distributed tracing support

📦 What is this?

This library provides base classes for implementing internal domain events (in-process) that follow DDD best practices and support distributed tracing.

🎯 When to Use This Library?

Use BaseDomainEvent (this library) for:

  • 🔷 In-process events (same service side effects)
  • 🔷 Internal coordination (send email, update cache, log activity)
  • 🔷 Bounded context events (within one microservice)
  • 🔷 Immediate processing (synchronous event handling)

Do NOT use for:

  • 🌐 Cross-service communication (use IOutboxEvent from python-outbox-core)
  • 🌐 External integrations (analytics, audit services, third-party)
  • 🌐 Message broker events (Kafka, RabbitMQ)

Event Type Decision Tree

Need to publish an event?
│
├─ Same service only?
│  └─ Use BaseDomainEvent (python-domain-events) ⭐ THIS LIBRARY
│     Examples: Send email, log activity, update cache
│
└─ Other services/external?
   └─ Use IOutboxEvent (python-outbox-core)
      Examples: Notify microservices, analytics, audit

🚀 Quick Start

Installation

pip install -e python-web-toolkit/packages/python-domain-events

Basic Usage

from python_domain_events import BaseDomainEvent
from uuid import UUID
from typing import Optional

class InviteCreatedEvent(BaseDomainEvent):
    """Domain event: Invite was created."""
    event_type: str = "invite.created"
    
    invite_id: int
    token: str
    email: Optional[str]
    
    @classmethod
    def from_entity(cls, invite, correlation_id: Optional[UUID] = None):
        return cls(
            invite_id=invite.id,
            token=invite.token,
            email=invite.email,
            correlation_id=correlation_id
        )

# Create event
event = InviteCreatedEvent.from_entity(invite)

# Event has auto-generated fields
print(event.event_id)        # UUID
print(event.occurred_at)     # datetime
print(event.correlation_id)  # Optional UUID for tracing

📚 Features

Auto-generated event_id - UUID for deduplication
Auto-generated timestamps - No manual datetime.utcnow()
Distributed tracing - correlation_id, causation_id support
Immutability - Events are frozen after creation
Type safety - Full Pydantic validation
Extensible metadata - metadata: Dict[str, Any] for custom fields

🏗️ Components

  • BaseDomainEvent - Base class for domain events
  • IDomainEventHandler - Interface for event handlers
  • InProcessEventDispatcher - Dispatcher for routing events to handlers

📖 License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_domain_events-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_domain_events-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file python_domain_events-0.1.0.tar.gz.

File metadata

  • Download URL: python_domain_events-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for python_domain_events-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d53a61215bb636be5002e6a16aec5bf5ca44412fcb789713342977de275ffff8
MD5 8c0e1141eec84886734e8fd784f7a894
BLAKE2b-256 8f5b63a125a252493acdf67f85be0e29e2df17266357fcad2a39a906bddf0987

See more details on using hashes here.

File details

Details for the file python_domain_events-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_domain_events-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8c38f57c5d00ab89c411dafefa0e277705adb4a9ea3c5f37f76fc8b62c580a1
MD5 90304c65b4eb71b1dd77d665e36e3aa3
BLAKE2b-256 84ec0a8694d5d04cffab970d4edcc3bbeba592aedc27b33ba974051fc4a8c350

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page