Skip to main content

Domain-Driven Design for Python

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Coverage Status PyPI - License PyPI PyPI Mypy

pyddd

pyddd - это DDD (Domain-Driven Design) фреймворк для Python, предоставляющий встроенный менеджер зависимостей и шину сообщений. Он упрощает построение сложных доменных моделей и событийное программирование.

Возможности

  • Разделение доменов через Module
  • Использование команд и событий (DomainCommand, DomainEvent)
  • Корневые сущности (RootEntity) и управление идентификаторами (EntityId)
  • Встроенный менеджер зависимостей
  • Событийно-ориентированная архитектура

Быстрый старт

Определение доменной модели

class PetCreated(DomainEvent, domain='pet'):
    pet_id: str
    name: str


class Pet(RootEntity):
    def __init__(self, name: str):
        self.name = name

    @classmethod
    def create(cls, name: str):
        pet = cls(name)
        pet.register_event(PetCreated(name=name, pet_id=pet.__reference__))
        return pet

Репозитории и модуль

class IPetRepository(abc.ABC):
    @abc.abstractmethod
    def save(self, entity: RootEntity):
        ...

    @abc.abstractmethod
    def get(self, name: str) -> Pet:
        ...


class CreatePet(DomainCommand, domain='pet'):
    name: str


pet_module = Module('pet')


@pet_module.register
def create_pet(cmd: CreatePet, repository: IPetRepository):
    pet = Pet.create(cmd.name)
    repository.save(pet)
    return pet.__reference__

Запуск приложения

class InMemoryPetRepo(IPetRepository):
    def __init__(self):
        self.memory = {}

    def get(self, name: str) -> Pet:
        return self.memory.get(name)

    def save(self, entity: Pet):
        self.memory[entity.name] = entity


# Настройка приложения
app = Application()
app.run()
app.include(pet_module)
app.set_defaults('pet', repository=InMemoryPetRepo())
set_application(app)

# Использование
fluff_id = app.handle(CreatePet(name='Fluff'))
print(f'Создан питомец с ID: {fluff_id}')

Запуск интеграционных тестов:

# Поднимаем внешние сервисы
docker-compose up -d

# Запуск тестов
pytest tests

Лицензия

Этот проект распространяется под лицензией MIT. Подробности см. в файле LICENSE.

Контрибьюция

Мы приветствуем ваш вклад! Создавайте issue, присылайте pull request'ы и помогайте развивать pyddd.

Контакты

Если у вас есть вопросы или предложения, свяжитесь с нами через GitHub Issues.

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

ddd_python-0.7.3.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

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

ddd_python-0.7.3-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file ddd_python-0.7.3.tar.gz.

File metadata

  • Download URL: ddd_python-0.7.3.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for ddd_python-0.7.3.tar.gz
Algorithm Hash digest
SHA256 7eec3c78992155a3bdc433cbc5e803d44af5d854cf15520ce36fd713c64a8adb
MD5 37f8e545f552dc807d0d51aa3b142b6c
BLAKE2b-256 0bc984115e93b7b12561c6df70ae4f0effd026adf7ee1b98fa9e767a48f85cdc

See more details on using hashes here.

File details

Details for the file ddd_python-0.7.3-py3-none-any.whl.

File metadata

  • Download URL: ddd_python-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 38.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for ddd_python-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2ece9b6b14d8721565962218d22451d454c297bf2bf87e11eb2f5f84be3cde12
MD5 1edd173a3af64b0d2c4f9c7893976569
BLAKE2b-256 ec068bf5049dd41ff9992d7bbcc8fbfdb8e18e73180268637f7aa1b88fdd21b1

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