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.1.tar.gz (9.9 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.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ditainer-1.2.1.tar.gz
  • Upload date:
  • Size: 9.9 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.1.tar.gz
Algorithm Hash digest
SHA256 a6184a074c4d8f766c060c3df84cf9ca19cfe88d8125220243e8f266dae03f25
MD5 fefff5fa4834858110c5fd2db7246fd0
BLAKE2b-256 dfd2206df6e8c3886cd150a8832c84848b81cc8f7b781f5a27fa88c1629e6b13

See more details on using hashes here.

Provenance

The following attestation bundles were made for ditainer-1.2.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: ditainer-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49965f20a0ea997d91cd9b06dfe7c6990f80320f636e13550ebb694eb8876555
MD5 9363a6a9ac373051d4bf00428dcd9b09
BLAKE2b-256 b3c6dff53a4a8f3460ef0a6a5ea0ae8754b9c6ea7aea36e34bffdf640787e354

See more details on using hashes here.

Provenance

The following attestation bundles were made for ditainer-1.2.1-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