Skip to main content

Convention-driven repository binding for domain-oriented Django architectures.

Project description

django-domain-repositories

Automatic repository discovery and container binding for domain-driven Django applications.

This package eliminates manual wiring of repository contracts to their implementations. It scans your configured domains package on startup, applies a strict naming convention, and registers everything automatically — no decorators, no explicit registration calls.

Installation

pip install django-domain-repositories

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_domain_repositories",
]

Naming Convention

Role Pattern
Contract module <domains_pkg>.<domain>.contracts.<domain>_repository
Contract class <Domain>Repository (abstract)
Concrete module <domains_pkg>.<domain>.repositories.<domain>_<driver>
Concrete class <Domain><Driver>

Example with foo domain and default django_orm driver:

myproject/domains/foo/contracts/foo_repository.py   → class FooRepository (ABC)
myproject/domains/foo/repositories/foo_django_orm.py → class FooDjangoOrm(FooRepository)

If a contract or concrete class does not exist for a domain, that domain is silently skipped.

Domain Structure

myproject/
  domains/
    __init__.py
    foo/
      __init__.py
      contracts/
        foo_repository.py
      repositories/
        foo_django_orm.py
      models/
      services/
    bar/
      __init__.py
      contracts/
        bar_repository.py
      repositories/
        bar_sqlalchemy.py

Configuration

# settings.py

DOMAIN_REPOSITORIES = {
    "DEFAULT_DRIVER": "django_orm",        # default driver suffix
    "DEFAULT_BINDING": "transient",        # new instance per resolve call
    "BINDING_OPTIONS": ["transient", "singleton"],
    "DOMAINS_PACKAGE": "myproject.domains",

    # Per-domain overrides
    "DOMAINS": {
        "foo": {"driver": "django_orm", "binding": "transient"},
        "bar": {"driver": "sqlalchemy", "binding": "singleton"},
    },
}

transient returns a new instance on every resolve() call. singleton returns the same instance throughout the process lifetime.

Resolving a Repository

from django_domain_repositories import resolve
from myproject.domains.foo.contracts.foo_repository import FooRepository

repo = resolve(FooRepository)

Directory Scaffolding

from django_domain_repositories import ensure_domain_directories

ensure_domain_directories("/path/to/myproject/domains/foo")

Creates the conventional subdirectories: contracts, repositories, models, services, enums, events, exceptions, helpers, jobs, listeners, observers, views.

Testing

from django_domain_repositories.registry import clear_registry, _registry

def test_something():
    clear_registry()
    # register a fake manually, test, then clear again

Out of Scope

This package handles repository registration only. Signals, middleware, and other domain concerns are intentionally left out.

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

django_domain_repositories-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

django_domain_repositories-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_domain_repositories-0.1.0.tar.gz
Algorithm Hash digest
SHA256 92547a9559f777312b7146417174c7595b05d195a61616c922748cd8038a2061
MD5 77ad1082d5071b66d9da86203abca9dc
BLAKE2b-256 507d66798e67cdf222572839efab210e19e7de75bf67a5e87beb4d669629c969

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_domain_repositories-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 076676e1dadf792508820bf6a00e9227032fe785dbf46f3dfe79ab55ee7c4e05
MD5 f7c877e61f86e8e883e63a46b90621ee
BLAKE2b-256 46ce8f59128cfa93f79768999e340940899557deae4124cd046e724d34136167

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