Skip to main content

Implementation of dependency injection for Python 3

Project description

Build pypi versions codecov license

Implementation of dependency injection for Python 3

Features:

  • types resolution by signature types annotations (type hints)
  • types resolution by class annotations (type hints) (new in version 1.1.0 :star:)
  • types resolution by constructor parameter names and aliases (convention over configuration)
  • unintrusive: builds objects graph without the need to change classes source code (unlike some other Python implementations of dependency injection, like inject)
  • minimum overhead to obtain services, once the objects graph is built
  • support for singletons, transient, and scoped services
  • compatible with Python 3.10.0a5 (still in development at the time of rodi 1.1.1)

This library is freely inspired by .NET Standard Microsoft.Extensions.DependencyInjection implementation (ref. MSDN, Dependency injection in ASP.NET Core, Using dependency injection in a .Net Core console application).

Installation

pip install rodi

De gustibus non disputandum est

This library is designed to work both by type hints in constructor signatures, and by constructor parameter names (convention over configuration), like described in below diagram. It can be useful for those who like type hinting, those who don't, and those who like having both options.

Usage option

Minimum overhead

rodi works by inspecting __init__ methods once at runtime, to generate functions that return instances of desired types. 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.

For this reason, services are first registered inside an instance of Container class, which implements a method build_provider() that returns an instance of Services. The service provider is then used to obtain desired services by type or name. Inspection and validation steps are done only when creating an instance of service provider.

Classes

In the example below, a singleton is registered by exact instance.

container = Container()
container.add_instance(Cat("Celine"))

services = container.build_provider()  # --> validation, generation of functions

cat = services.get(Cat)

assert cat is not None
assert cat.name == "Celine"

Service life style:

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

Usage in BlackSheep

rodi is used in the BlackSheep web framework to implement dependency injection for request handlers.

Documentation

For documentation and examples, please refer to the wiki in GitHub, https://github.com/Neoteroi/rodi/wiki.

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

rodi-1.1.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rodi-1.1.2-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file rodi-1.1.2.tar.gz.

File metadata

  • Download URL: rodi-1.1.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for rodi-1.1.2.tar.gz
Algorithm Hash digest
SHA256 c2bde6428ecea9dc7febfca8574f7843bbfebb95c95304cde782978b9edde9b4
MD5 6263cd39d71340f3cf126c055aa54802
BLAKE2b-256 8b2d7d1f609708f765aa284480e8a6ae69dff52d5ba5b67ac2848cff51bf6e22

See more details on using hashes here.

File details

Details for the file rodi-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: rodi-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for rodi-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a1f5fdcd534bdc0cd7df6fbeddda7aab336baf8e742ffea6ed727d976f63d161
MD5 c5ff8841b48446e9642d6081adc8cb07
BLAKE2b-256 d04f4380df5a20d73c7189ec70a83ee0284bee3ff59a51b84d85c3bca3fcc270

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page