Skip to main content

Python implementation of the Dependency Injection pattern.

Project description

Test Coverage Stable Version Pre-release Version PyPI - Python Version PyPI Downloads

dependency-injection-pattern

A Python implementation of the Dependency Injection pattern.

This project extends python with a real dependency injection or Inversion of Control (IoC) implementation, relying on typing and inspection to achieve a seamless injection of dependencies.

Though not normally associated with Python applications, dependency injection can greatly reduce time spent during development and testing. Usually, in a longrunning Python application or service, you would put the container registration inside the __main_.py file and similar code in test files, where service implementations can easily be substituted.

How it works

When a service is requested, the provider looks it up in the factory dictionary, which then gets or creates a fitting instance. Any parameters needed for instantiation is supplied (injected) by the provider.

A service implementation may be inferred by a service in which case the service constructor is used, and correspondingly a service may be inferred by the return type of an implementation function.

Conventions

  • All dependencies are resolved when container is sealed, which implements a fail-fast pattern. Only exceptions are optional dependencies or dependencies with a default value.

  • Singleton services may not depend on scoped or transient services, as this would negate the "transient" or "scoped" part since a singleton service would keep a reference to it's dependencies indefinitely.

Example:

from logging import Logger
from di import Container

def get_logger() -> Logger:
    return Logger("app")

class Service1:
    def get_value(self) -> str:
        return "Some value"

class Service2:
    def __init__(self, service1: Service1, log: Logger):
        self.service1 = service1
        self.log = log

    def get_value(self) -> str:
        self.log.debug("Someone requested value...")
        return f"Service1 returned: {self.service1.get_value()}"

class Application:
    def __init__(self, service2: Service2, log: Logger):
        self.service2 = service2
        log.info("Application starting")

    def get_value(self) -> str:
        return f"Service2 returned: {self.service2.get_value()}"


container = Container()
container.add_singleton(get_logger)
container.add_transient(Service1)
container.add_transient(Service2)
container.add_transient(Application)

provider = container.provider() # container is sealed beyond this point

app = provider.provide(Application)

value = app.get_value() # => "Service2 returned: Service1 returned: Some value"

Full documentation

Go to documentation

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

dependency_injection_pattern-0.0.6.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

dependency_injection_pattern-0.0.6-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file dependency_injection_pattern-0.0.6.tar.gz.

File metadata

File hashes

Hashes for dependency_injection_pattern-0.0.6.tar.gz
Algorithm Hash digest
SHA256 169df20222e44813c10d98304d3038d3e84431ceb0bf7808733d8a745873af82
MD5 9135b22cafdc2f5ceb0027c981a41867
BLAKE2b-256 8894800f9fb97fe222efebf2aa3edf23037242f51835fe3643a666bc096f2300

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injection_pattern-0.0.6.tar.gz:

Publisher: python-publish.yml on apmadsen/dependency-injection-pattern

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

File details

Details for the file dependency_injection_pattern-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for dependency_injection_pattern-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 72685afee544e3c026fd3462a79f3c300c56b879766586996237dc059aadac5b
MD5 a5da4951640e72272e8e008ffad67d8d
BLAKE2b-256 fb4a72087d1a039fd4a63a8b5ca2f2d0568e40e8bb682191fb4e40b66d0ce520

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injection_pattern-0.0.6-py3-none-any.whl:

Publisher: python-publish.yml on apmadsen/dependency-injection-pattern

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