Python implementation of the Dependency Injection pattern.
Project description
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dependency_injection_pattern-0.0.6.tar.gz.
File metadata
- Download URL: dependency_injection_pattern-0.0.6.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
169df20222e44813c10d98304d3038d3e84431ceb0bf7808733d8a745873af82
|
|
| MD5 |
9135b22cafdc2f5ceb0027c981a41867
|
|
| BLAKE2b-256 |
8894800f9fb97fe222efebf2aa3edf23037242f51835fe3643a666bc096f2300
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dependency_injection_pattern-0.0.6.tar.gz -
Subject digest:
169df20222e44813c10d98304d3038d3e84431ceb0bf7808733d8a745873af82 - Sigstore transparency entry: 644201285
- Sigstore integration time:
-
Permalink:
apmadsen/dependency-injection-pattern@ff483e2fd2edb64f900a7559bd4dcb9337a1ca52 -
Branch / Tag:
refs/tags/v0.0.6 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ff483e2fd2edb64f900a7559bd4dcb9337a1ca52 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dependency_injection_pattern-0.0.6-py3-none-any.whl.
File metadata
- Download URL: dependency_injection_pattern-0.0.6-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72685afee544e3c026fd3462a79f3c300c56b879766586996237dc059aadac5b
|
|
| MD5 |
a5da4951640e72272e8e008ffad67d8d
|
|
| BLAKE2b-256 |
fb4a72087d1a039fd4a63a8b5ca2f2d0568e40e8bb682191fb4e40b66d0ce520
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dependency_injection_pattern-0.0.6-py3-none-any.whl -
Subject digest:
72685afee544e3c026fd3462a79f3c300c56b879766586996237dc059aadac5b - Sigstore transparency entry: 644201489
- Sigstore integration time:
-
Permalink:
apmadsen/dependency-injection-pattern@ff483e2fd2edb64f900a7559bd4dcb9337a1ca52 -
Branch / Tag:
refs/tags/v0.0.6 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ff483e2fd2edb64f900a7559bd4dcb9337a1ca52 -
Trigger Event:
release
-
Statement type: