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.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

ditainer-1.2.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ditainer-1.2.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for ditainer-1.2.0.tar.gz
Algorithm Hash digest
SHA256 8ab2682bb9c44b604cb42b0b03231a42071cc5dda804008be2ab4b60d4965c9f
MD5 9e57d2cb33ee49d201c591830cd28e2b
BLAKE2b-256 25fb901e390b18e72a33866da163109b0761cd323f52041bbaf7e5bc894bac40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ditainer-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for ditainer-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f267f5fcd2a5916337e4ccf11907e5f818e0258ee51508a1086e0e7274fd201
MD5 f0291cef5d45633954c6c550ebd0da59
BLAKE2b-256 c96ec65804263f097b30380cd01655a3ce39b34591cfa5198ed509bc391370c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page