rodi


Namerodi JSON
Version 2.0.6 PyPI version JSON
download
home_page
SummaryImplementation of dependency injection for Python 3
upload_time2023-12-09 22:18:22
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords dependency hints injection type typing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![Build](https://github.com/Neoteroi/rodi/workflows/Build/badge.svg)
[![pypi](https://img.shields.io/pypi/v/rodi.svg)](https://pypi.python.org/pypi/rodi)
[![versions](https://img.shields.io/pypi/pyversions/rodi.svg)](https://github.com/Neoteroi/rodi)
[![codecov](https://codecov.io/gh/Neoteroi/rodi/branch/main/graph/badge.svg?token=VzAnusWIZt)](https://codecov.io/gh/Neoteroi/rodi)
[![license](https://img.shields.io/github/license/Neoteroi/rodi.svg)](https://github.com/Neoteroi/rodi/blob/main/LICENSE)

# Implementation of dependency injection for Python 3

**Features:**

* types resolution by signature types annotations (_type hints_)
* types resolution by class annotations (_type hints_)
* types resolution by names and aliases (_convention over configuration_)
* unintrusive: builds objects graph **without** the need to change the
  source code of classes
* minimum overhead to obtain services, once the objects graph is built
* support for singletons, transient, and scoped services

This library is freely inspired by .NET Standard
`Microsoft.Extensions.DependencyInjection` implementation (_ref. [MSDN,
Dependency injection in ASP.NET
Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1),
[Using dependency injection in a .Net Core console
application](https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/)_).
The `ContainerProtocol` for v2 is inspired by [punq](https://github.com/bobthemighty/punq).

## Installation

```bash
pip install rodi
```

## Efficient

`rodi` works by inspecting code **once** at runtime, to generate
functions that return instances of desired types - as long as the object graph
is not altered. Inspections are done either on constructors
(____init____) or class annotations. Validation steps, for
example to detect circular dependencies or missing services, are done when
building these functions, so additional validation is not needed when
activating services.

## Flexible

`rodi` offers two code APIs:

- one is kept as generic as possible, using a `ContainerProtocol` for scenarios
  in which it is desirable being able to replace `rodi` with alternative
  implementations of dependency injection for Python. The protocol only expects
  a class being able to `register` and `resolve` types, and to tell if a type
  is configured in it (`__contains__`). Even if other implementations of DI
  don´t implement these three methods, it should be easy to use
  [composition](https://en.wikipedia.org/wiki/Composition_over_inheritance) to
  wrap other libraries with a compatible class.
- one is a more concrete implementation, for scenarios where it's not desirable
  to consider alternative implementations of dependency injection.

For this reason, the examples report two ways to achieve certain things.

### Examples

For examples, refer to the [examples folder](./examples).

### Recommended practices

All services should be configured once, when an application starts, and the
object graph should *not* be altered during normal program execution.
Example: if you build a web application, configure the object graph when
bootstrapping the application, avoid altering the `Container` configuration
while handling web requests.

Aim at keeping the `Container` and service graphs abstracted from the front-end
layer of your application, and avoid mixing runtime values with container
configuration. Example: if you build a web application, avoid if possible
relying on the HTTP Request object being a service registered in your container.

## Service life style:

* singleton - instantiated only once per service provider
* transient - services are instantiated every time they are required
* scoped - instantiated only once per root service resolution call
  (e.g. once per web request)

## Usage in BlackSheep

`rodi` is used in the [BlackSheep](https://www.neoteroi.dev/blacksheep/)
web framework to implement [dependency injection](https://www.neoteroi.dev/blacksheep/dependency-injection/) for
request handlers.

# Documentation

Under construction. 🚧

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "rodi",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "dependency,hints,injection,type,typing",
    "author": "",
    "author_email": "Roberto Prevato <roberto.prevato@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f6/ad/f3d92b186afec5b2316597692d83472bc7d489d3aed223347fef04b01d9d/rodi-2.0.6.tar.gz",
    "platform": null,
    "description": "![Build](https://github.com/Neoteroi/rodi/workflows/Build/badge.svg)\n[![pypi](https://img.shields.io/pypi/v/rodi.svg)](https://pypi.python.org/pypi/rodi)\n[![versions](https://img.shields.io/pypi/pyversions/rodi.svg)](https://github.com/Neoteroi/rodi)\n[![codecov](https://codecov.io/gh/Neoteroi/rodi/branch/main/graph/badge.svg?token=VzAnusWIZt)](https://codecov.io/gh/Neoteroi/rodi)\n[![license](https://img.shields.io/github/license/Neoteroi/rodi.svg)](https://github.com/Neoteroi/rodi/blob/main/LICENSE)\n\n# Implementation of dependency injection for Python 3\n\n**Features:**\n\n* types resolution by signature types annotations (_type hints_)\n* types resolution by class annotations (_type hints_)\n* types resolution by names and aliases (_convention over configuration_)\n* unintrusive: builds objects graph **without** the need to change the\n  source code of classes\n* minimum overhead to obtain services, once the objects graph is built\n* support for singletons, transient, and scoped services\n\nThis library is freely inspired by .NET Standard\n`Microsoft.Extensions.DependencyInjection` implementation (_ref. [MSDN,\nDependency injection in ASP.NET\nCore](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1),\n[Using dependency injection in a .Net Core console\napplication](https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/)_).\nThe `ContainerProtocol` for v2 is inspired by [punq](https://github.com/bobthemighty/punq).\n\n## Installation\n\n```bash\npip install rodi\n```\n\n## Efficient\n\n`rodi` works by inspecting code **once** at runtime, to generate\nfunctions that return instances of desired types - as long as the object graph\nis not altered. Inspections are done either on constructors\n(__&#95;&#95;init&#95;&#95;__) or class annotations. Validation steps, for\nexample to detect circular dependencies or missing services, are done when\nbuilding these functions, so additional validation is not needed when\nactivating services.\n\n## Flexible\n\n`rodi` offers two code APIs:\n\n- one is kept as generic as possible, using a `ContainerProtocol` for scenarios\n  in which it is desirable being able to replace `rodi` with alternative\n  implementations of dependency injection for Python. The protocol only expects\n  a class being able to `register` and `resolve` types, and to tell if a type\n  is configured in it (`__contains__`). Even if other implementations of DI\n  don\u00b4t implement these three methods, it should be easy to use\n  [composition](https://en.wikipedia.org/wiki/Composition_over_inheritance) to\n  wrap other libraries with a compatible class.\n- one is a more concrete implementation, for scenarios where it's not desirable\n  to consider alternative implementations of dependency injection.\n\nFor this reason, the examples report two ways to achieve certain things.\n\n### Examples\n\nFor examples, refer to the [examples folder](./examples).\n\n### Recommended practices\n\nAll services should be configured once, when an application starts, and the\nobject graph should *not* be altered during normal program execution.\nExample: if you build a web application, configure the object graph when\nbootstrapping the application, avoid altering the `Container` configuration\nwhile handling web requests.\n\nAim at keeping the `Container` and service graphs abstracted from the front-end\nlayer of your application, and avoid mixing runtime values with container\nconfiguration. Example: if you build a web application, avoid if possible\nrelying on the HTTP Request object being a service registered in your container.\n\n## Service life style:\n\n* singleton - instantiated only once per service provider\n* transient - services are instantiated every time they are required\n* scoped - instantiated only once per root service resolution call\n  (e.g. once per web request)\n\n## Usage in BlackSheep\n\n`rodi` is used in the [BlackSheep](https://www.neoteroi.dev/blacksheep/)\nweb framework to implement [dependency injection](https://www.neoteroi.dev/blacksheep/dependency-injection/) for\nrequest handlers.\n\n# Documentation\n\nUnder construction. \ud83d\udea7\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Implementation of dependency injection for Python 3",
    "version": "2.0.6",
    "project_urls": {
        "Bug Tracker": "https://github.com/Neoteroi/rodi/issues",
        "Homepage": "https://github.com/Neoteroi/rodi"
    },
    "split_keywords": [
        "dependency",
        "hints",
        "injection",
        "type",
        "typing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c661f1a429d16c9a24343e14b446c4880b2a74346e59bc5363862779bc805fac",
                "md5": "7fc73cfaeb51aa84ed275642d2b40767",
                "sha256": "d299276be19842133def84dde365771d11a5195b76be0595eae9c1ecad818446"
            },
            "downloads": -1,
            "filename": "rodi-2.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7fc73cfaeb51aa84ed275642d2b40767",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 11829,
            "upload_time": "2023-12-09T22:18:20",
            "upload_time_iso_8601": "2023-12-09T22:18:20.723034Z",
            "url": "https://files.pythonhosted.org/packages/c6/61/f1a429d16c9a24343e14b446c4880b2a74346e59bc5363862779bc805fac/rodi-2.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f6adf3d92b186afec5b2316597692d83472bc7d489d3aed223347fef04b01d9d",
                "md5": "964f922aacefb0446f84529aa5cd41dd",
                "sha256": "f533e315d84b63824efcc67526a156ad5fb0a158f1ccbc41e0db3944d0c08693"
            },
            "downloads": -1,
            "filename": "rodi-2.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "964f922aacefb0446f84529aa5cd41dd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 13284,
            "upload_time": "2023-12-09T22:18:22",
            "upload_time_iso_8601": "2023-12-09T22:18:22.993354Z",
            "url": "https://files.pythonhosted.org/packages/f6/ad/f3d92b186afec5b2316597692d83472bc7d489d3aed223347fef04b01d9d/rodi-2.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-09 22:18:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Neoteroi",
    "github_project": "rodi",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "rodi"
}
        
Elapsed time: 0.14901s