[](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test.yml)
[](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test-coverage.yml)
[](https://github.com/apmadsen/dependency-injection-pattern/releases)


[](https://pepy.tech/projects/dependency-injection-pattern)
# 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:
```python
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](https://github.com/apmadsen/dependency-injection-pattern/blob/main/docs/documentation.md)
Raw data
{
"_id": null,
"home_page": null,
"name": "dependency-injection-pattern",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "windows, linux, dependency injection, di, dependency, injection, IoC",
"author": null,
"author_email": "Anders Madsen <anders.madsen@alphavue.com>",
"download_url": "https://files.pythonhosted.org/packages/1b/83/d9266f2d82479e0e644a9ef2307a686305105385d91ea610e7f09cd5c50b/dependency_injection_pattern-0.0.4.tar.gz",
"platform": null,
"description": "[](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test.yml)\n[](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test-coverage.yml)\n[](https://github.com/apmadsen/dependency-injection-pattern/releases)\n\n\n[](https://pepy.tech/projects/dependency-injection-pattern)\n\n# dependency-injection-pattern\n> A Python implementation of the Dependency Injection pattern.\n\nThis 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.\n\nThough 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.\n\n## How it works\nWhen 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.\n\nA 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.\n\n## Conventions\n- 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.\n\n- 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.\n\n## Example:\n\n```python\nfrom logging import Logger\nfrom di import Container\n\ndef get_logger() -> Logger:\n return Logger(\"app\")\n\nclass Service1:\n def get_value(self) -> str:\n return \"Some value\"\n\nclass Service2:\n def __init__(self, service1: Service1, log: Logger):\n self.service1 = service1\n self.log = log\n\n def get_value(self) -> str:\n self.log.debug(\"Someone requested value...\")\n return f\"Service1 returned: {self.service1.get_value()}\"\n\nclass Application:\n def __init__(self, service2: Service2, log: Logger):\n self.service2 = service2\n log.info(\"Application starting\")\n\n def get_value(self) -> str:\n return f\"Service2 returned: {self.service2.get_value()}\"\n\n\ncontainer = Container()\ncontainer.add_singleton(get_logger)\ncontainer.add_transient(Service1)\ncontainer.add_transient(Service2)\ncontainer.add_transient(Application)\n\nprovider = container.provider() # container is sealed beyond this point\n\napp = provider.provide(Application)\n\nvalue = app.get_value() # => \"Service2 returned: Service1 returned: Some value\"\n```\n\n## Full documentation\n\n[Go to documentation](https://github.com/apmadsen/dependency-injection-pattern/blob/main/docs/documentation.md)\n",
"bugtrack_url": null,
"license": null,
"summary": "Python implementation of the Dependency Injection pattern.",
"version": "0.0.4",
"project_urls": {
"repository": "https://github.com/apmadsen/dependency-injection-pattern"
},
"split_keywords": [
"windows",
" linux",
" dependency injection",
" di",
" dependency",
" injection",
" ioc"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "d528e9f4c5d395d73e062a3c05ddfcbf42325b8814e17ed97285cd2b8f7abc6f",
"md5": "b35c4c07107ca3f5ff5ec27ef63b1046",
"sha256": "8e1c6bfb90f4858e175ba0ae585e0adbe8c2c99148d38546063b5cd82f70ddfe"
},
"downloads": -1,
"filename": "dependency_injection_pattern-0.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b35c4c07107ca3f5ff5ec27ef63b1046",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 20033,
"upload_time": "2025-07-29T07:10:09",
"upload_time_iso_8601": "2025-07-29T07:10:09.155690Z",
"url": "https://files.pythonhosted.org/packages/d5/28/e9f4c5d395d73e062a3c05ddfcbf42325b8814e17ed97285cd2b8f7abc6f/dependency_injection_pattern-0.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "1b83d9266f2d82479e0e644a9ef2307a686305105385d91ea610e7f09cd5c50b",
"md5": "95519c2c92abc9da17aee9412c7274d6",
"sha256": "18b78974594330f829672bf2ebdb8fff19121c0918add6d0850e18cb963f9287"
},
"downloads": -1,
"filename": "dependency_injection_pattern-0.0.4.tar.gz",
"has_sig": false,
"md5_digest": "95519c2c92abc9da17aee9412c7274d6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 15892,
"upload_time": "2025-07-29T07:10:10",
"upload_time_iso_8601": "2025-07-29T07:10:10.959017Z",
"url": "https://files.pythonhosted.org/packages/1b/83/d9266f2d82479e0e644a9ef2307a686305105385d91ea610e7f09cd5c50b/dependency_injection_pattern-0.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-29 07:10:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "apmadsen",
"github_project": "dependency-injection-pattern",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "dependency-injection-pattern"
}