Skip to main content

Dependency Injection Container for Python

Project description

Ditainer: Dependency Injection Container for Python

Ditainer is a lightweight and flexible Dependency Injection (DI) container for Python, designed to streamline the management of dependencies in your applications.

Key Features

  • Load dependencies (services) from YAML files.
  • Inject dependencies via constructor arguments, factories, or tags.
  • Easily reference other services or groups of services by tag.
  • Handles circular dependencies.
  • Supports importing multiple YAML files for modular configurations.

Installation

Install Ditainer via PyPI:

pip install ditainer

How to Use

Service Definitions in YAML

In Ditainer, a dependency is referred to as a service. A service is typically a class that may require arguments (other services or literal values) for its initialization. Services can also be instantiated via a factory function, which can similarly receive arguments.

Additionally, services can be tagged, allowing for easy retrieval of groups of services later on.

There are three types of arguments that a service can receive:

  • Literal arguments: Direct values such as strings, numbers, etc.
  • References to other services: Use the prefix !ref followed by the ID of the service.
  • Tagged services: Use the prefix !tagged followed by the tag to reference all services with that tag.

Example YAML Configuration

services:
  - id: DBSession
    module: my_module.db
    class: DBSession
    factory: create_session

  - id: UserRepository
    module: my_module.user.repository
    class: MySqlUserRepository
    arguments:
      - !ref DBSession

  - id: QueryBus
    module: my_module.query_bus
    class: InMemoryQueryBus
    arguments:
      - !tagged query_handler

  - id: UserFindByIDQueryHandler
    module: my_module.user.find_by_id_query_handler
    class: UserFindByIDQueryHandler
    arguments:
      - !ref UserRepository
    tags:
      - query_handler

  - id: UserFindByCodeQueryHandler
    module: my_module.user.find_by_code_query_handler
    class: UserFindByCodeQueryHandler
    arguments:
      - !ref UserRepository
    tags:
      - query_handler

In this example:

  • The DBSession service is created using the create_session factory.
  • The UserRepository service depends on DBSession.
  • The QueryBus service aggregates all services tagged as query_handler, such as UserFindByIDQueryHandler and UserFindByCodeQueryHandler.

Importing Multiple Service Configurations

Ditainer supports modular configuration through multiple YAML files:

imports:
  - { resource: "./services_1.yaml" }
  - { resource: "./services_2.yaml" }
  - { resource: "./services_3.yaml" }

Using Ditainer in Your Python Code

Once your YAML configuration files are ready, load them into the container as follows:

import os
from ditainer.container import Container
from ditainer.loader import YAMLLoader


# Initialize container
container = Container()

# Load services from YAML
loader = YAMLLoader(container)
loader.load(os.path.join(os.path.dirname(__file__), "imports.yaml"))

Accessing Services

You can now access services in your application:

from my_module import container
from my_module.user import User
from my_module.user.find_by_id_query import UserFindByIdQuery


def get_user() -> User:
    query_bus = container.find("QueryBus")
    query = UserFindByIdQuery(123)

    # Dispatch query to QueryBus
    user = query_bus.ask(query)
    return user

Searching for Services by Tag

If you need to retrieve all services with a specific tag:

from my_module import container


query_handlers = container.search_tagged("query_handler")

If no services are found for the given tag, the result will be an empty list.

Additional Examples

You can find more examples and test cases in the tests folder of the repository.

Credits

Inspired by Node Dependency Injection (https://www.npmjs.com/package/node-dependency-injection)

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

ditainer-1.2.3.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

ditainer-1.2.3-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file ditainer-1.2.3.tar.gz.

File metadata

  • Download URL: ditainer-1.2.3.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ditainer-1.2.3.tar.gz
Algorithm Hash digest
SHA256 602f705c735d375c7af8da6b64823d9e4169d154eb72530c5f1a8e125ec41a1d
MD5 b9f6e5b286a17f39494a440b646d9a9c
BLAKE2b-256 b6cf9fb6f498240c3b92d9f2badea924034d8f365a506729b965235d6d529705

See more details on using hashes here.

Provenance

The following attestation bundles were made for ditainer-1.2.3.tar.gz:

Publisher: release.yaml on damian-ontivero/ditainer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ditainer-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: ditainer-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ditainer-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 33bd1b5a618369dd6c2f70a080a4bbfedef00413a65bb270529aed601dd86e0f
MD5 e926efa45e3889e8f62bbf78ea149268
BLAKE2b-256 eb8d225d52b23f17688535488f28afa751e3a6ce9db6ce61efbd2b1b6ca34fda

See more details on using hashes here.

Provenance

The following attestation bundles were made for ditainer-1.2.3-py3-none-any.whl:

Publisher: release.yaml on damian-ontivero/ditainer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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