dependency-injection-pattern


Namedependency-injection-pattern JSON
Version 0.0.3 PyPI version JSON
download
home_pageNone
SummaryPython implementation of the Dependency Injection pattern.
upload_time2025-07-14 11:19:52
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords windows linux dependency injection di dependency injection ioc
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            [![Test](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test.yml/badge.svg)](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test.yml)
[![Coverage](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test-coverage.yml/badge.svg)](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test-coverage.yml)
[![Stable Version](https://img.shields.io/pypi/v/dependency-injection-pattern?label=stable&sort=semver&color=blue)](https://github.com/apmadsen/dependency-injection-pattern/releases)
![Pre-release Version](https://img.shields.io/github/v/release/apmadsen/dependency-injection-pattern?label=pre-release&include_prereleases&sort=semver&color=blue)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dependency-injection-pattern)
[![PyPI Downloads](https://static.pepy.tech/badge/dependency-injection-pattern/week)](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/67/83/2ad41be0ebbd4c5cdbe1aba2b569d067f86d512368f2eb190c146d590664/dependency_injection_pattern-0.0.3.tar.gz",
    "platform": null,
    "description": "[![Test](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test.yml/badge.svg)](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test.yml)\n[![Coverage](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test-coverage.yml/badge.svg)](https://github.com/apmadsen/dependency-injection-pattern/actions/workflows/python-test-coverage.yml)\n[![Stable Version](https://img.shields.io/pypi/v/dependency-injection-pattern?label=stable&sort=semver&color=blue)](https://github.com/apmadsen/dependency-injection-pattern/releases)\n![Pre-release Version](https://img.shields.io/github/v/release/apmadsen/dependency-injection-pattern?label=pre-release&include_prereleases&sort=semver&color=blue)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dependency-injection-pattern)\n[![PyPI Downloads](https://static.pepy.tech/badge/dependency-injection-pattern/week)](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.3",
    "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": "7b1d2fb86c29a36e7352b50f4528f460d21f3f551709c6af5cde71e74051cdc5",
                "md5": "2309ff6d7316c27ab2529f7bd1f964d2",
                "sha256": "d87a3802101ff9fa1343756b6489de3ab4a403fa857ecb8a24b09a4d3f57f19b"
            },
            "downloads": -1,
            "filename": "dependency_injection_pattern-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2309ff6d7316c27ab2529f7bd1f964d2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 19942,
            "upload_time": "2025-07-14T11:19:50",
            "upload_time_iso_8601": "2025-07-14T11:19:50.538651Z",
            "url": "https://files.pythonhosted.org/packages/7b/1d/2fb86c29a36e7352b50f4528f460d21f3f551709c6af5cde71e74051cdc5/dependency_injection_pattern-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "67832ad41be0ebbd4c5cdbe1aba2b569d067f86d512368f2eb190c146d590664",
                "md5": "02d8aadc70b96518c1d6d70a4a12d9ff",
                "sha256": "031c3b53c15770fb11a547a0e81216aff6ce53ef4bbc117955fdb7debfa1e1c8"
            },
            "downloads": -1,
            "filename": "dependency_injection_pattern-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "02d8aadc70b96518c1d6d70a4a12d9ff",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 15822,
            "upload_time": "2025-07-14T11:19:52",
            "upload_time_iso_8601": "2025-07-14T11:19:52.309358Z",
            "url": "https://files.pythonhosted.org/packages/67/83/2ad41be0ebbd4c5cdbe1aba2b569d067f86d512368f2eb190c146d590664/dependency_injection_pattern-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-14 11:19:52",
    "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"
}
        
Elapsed time: 0.96468s