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.5.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.5-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for dependency_injection_pattern-0.0.5.tar.gz
Algorithm Hash digest
SHA256 152b0908c3886de46d16fc12d967acdfa253f54ad477398967f7d27ca4cd3c36
MD5 86adc8ca89c57350bcb43c4f6b5c84e6
BLAKE2b-256 9e615c384f1b63bbecf492e33a2a346c4a6eea25d5bb5c5159b5a2a8caaa5a3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injection_pattern-0.0.5.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.5-py3-none-any.whl.

File metadata

File hashes

Hashes for dependency_injection_pattern-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fb7069f6a08a3bd5920b561381ea7e6362b1c807e6c875c9e321bebca9ca1f24
MD5 d4bff15a921bf5a84539e94be97cad56
BLAKE2b-256 97037862c21579deb859dc2d88c1e929c432a6de8f740effa1286668142a4c0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injection_pattern-0.0.5-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