Skip to content

loryth-domain — Consumer Guide

Audience: Consumers — developers using loryth-domain in their services.

A library that implements domain-driven design concepts and patterns.

Overview

The loryth-domain library provides a foundation for implementing domain-driven design (DDD) in your applications. It includes base classes and utilities for working with aggregates, events, and other DDD concepts.

Key Components

Base Aggregate

  • BaseAggregate: A base implementation for domain aggregates, providing common functionality for managing entity state and domain events.

Event Sourcing

The library includes comprehensive support for event sourcing:

  • EventConsumer: A component that consumes events from an event log and processes them.
  • EventConsumerConfig: Configuration for event consumers.
  • EventLog: A representation of an event in the event log.
  • EventLogFilter: A filter for querying events from the event log.
  • EventLogRepository: A repository for storing and retrieving events from the event log.

Outbox Pattern

  • OutboxRepository: An implementation of the outbox pattern for reliable message publishing, ensuring that messages are published even if the initial attempt fails.

Domain Mixins

The library includes various mixins that can be used to add common domain behaviors to your aggregates and entities:

  • Auditing behavior
  • Versioning behavior
  • Soft deletion behavior
  • And more

Usage

To use the loryth-domain library, import the specific components you need:

from loryth.domain.core import BaseAggregate, EventLog, EventLogRepository

# Example usage of BaseAggregate
class User(BaseAggregate):
    def __init__(self, user_id, name, email):
        super().__init__(user_id)
        self.name = name
        self.email = email

    def change_email(self, new_email):
        self.email = new_email
        self.record_event("EmailChanged", {"new_email": new_email})

# Example usage of EventLogRepository
event_log_repo = EventLogRepository()
events = event_log_repo.get_events(
    filter=EventLogFilter(aggregate_id="user-123")
)

Integration with Other Libraries

The loryth-domain library is designed to work seamlessly with other libraries in the Loryth ecosystem:

  • It uses loryth-common for common utilities and patterns.
  • It can be used with loryth-persistence for storing domain entities and events.
  • It integrates with loryth-messages for publishing domain events to other services.