neoteroi-di


Nameneoteroi-di JSON
Version 0.0.4 PyPI version JSON
download
home_page
SummaryImplementation of dependency injection for Python 3
upload_time2022-12-29 11:42:05
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/neoteroi-di.svg)](https://pypi.python.org/pypi/neoteroi-di)
[![versions](https://img.shields.io/pypi/pyversions/neoteroi-di.svg)](https://github.com/Neoteroi/neoteroi-di)
[![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 neoteroi-di
```

`neoteroi-di` is the new version of the library that was previously named
[`rodi`](https://pypi.org/project/rodi/). It is currently `alpha` and still
subject to change.

## Efficient

`neoteroi-di` 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

`neoteroi-di` 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 `neoteroi-di` 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, following the previous implementation
  in `rodi`, 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

`neoteroi-di` is used in the second version of [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": "neoteroi-di",
    "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/cd/63/716fae4d7c71aec3d30e0ae88acb872f87fdfc82962ad6f0a45292c7f5c7/neoteroi_di-0.0.4.tar.gz",
    "platform": null,
    "description": "![Build](https://github.com/Neoteroi/rodi/workflows/Build/badge.svg)\n[![pypi](https://img.shields.io/pypi/v/neoteroi-di.svg)](https://pypi.python.org/pypi/neoteroi-di)\n[![versions](https://img.shields.io/pypi/pyversions/neoteroi-di.svg)](https://github.com/Neoteroi/neoteroi-di)\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 neoteroi-di\n```\n\n`neoteroi-di` is the new version of the library that was previously named\n[`rodi`](https://pypi.org/project/rodi/). It is currently `alpha` and still\nsubject to change.\n\n## Efficient\n\n`neoteroi-di` 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`neoteroi-di` 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 `neoteroi-di` 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, following the previous implementation\n  in `rodi`, for scenarios where it's not desirable to consider alternative\n  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`neoteroi-di` is used in the second version of [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": "0.0.4",
    "split_keywords": [
        "dependency",
        "hints",
        "injection",
        "type",
        "typing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "40c271f86e93eb5209377584e3ec54cc",
                "sha256": "3266b0617cf0005463c5ae34ddfdd83448b2cf1a266ed6f6da25ad0fbb640f80"
            },
            "downloads": -1,
            "filename": "neoteroi_di-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "40c271f86e93eb5209377584e3ec54cc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 14649,
            "upload_time": "2022-12-29T11:42:03",
            "upload_time_iso_8601": "2022-12-29T11:42:03.554166Z",
            "url": "https://files.pythonhosted.org/packages/46/b2/0489ca3d2427d21b7f3b7140b9d8e2bb658fd9d804b8471c821dcfce2280/neoteroi_di-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "ba5d05bfc26813046c6bcbacf8cfc42f",
                "sha256": "109eff8f72e340bc44088a0799ca06f60f3e33bf4358aba0eb0841e64bb205b3"
            },
            "downloads": -1,
            "filename": "neoteroi_di-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "ba5d05bfc26813046c6bcbacf8cfc42f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 16294,
            "upload_time": "2022-12-29T11:42:05",
            "upload_time_iso_8601": "2022-12-29T11:42:05.089495Z",
            "url": "https://files.pythonhosted.org/packages/cd/63/716fae4d7c71aec3d30e0ae88acb872f87fdfc82962ad6f0a45292c7f5c7/neoteroi_di-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-29 11:42:05",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "neoteroi-di"
}
        
Elapsed time: 0.02491s