Skip to main content

Implementation of dependency injection for Python 3

Project description

Build pypi versions codecov license documentation

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, Using dependency injection in a .Net Core console application). The ContainerProtocol for v2 is inspired by punq.

Documentation

Rodi is documented here: https://www.neoteroi.dev/rodi/.

Installation

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 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.

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 web framework to implement dependency injection for request handlers.

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-2.0.8.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

rodi-2.0.8-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rodi-2.0.8.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rodi-2.0.8.tar.gz
Algorithm Hash digest
SHA256 de122527dc6c0dd2d28bf57ac425b9dd22e37535fb1d7d22eadab0a079edcbe4
MD5 8fb48b51a1d80f147f745c878d10571b
BLAKE2b-256 285c2d914ad5f15bbd1cbf8589db4ec18a4b23806cbfa543f7ff91bacdf937f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rodi-2.0.8-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rodi-2.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 78a4ee6a29b4a4ce5d7474e3585149b3e78d613dc0cd2a10719b7a7f0f30ca05
MD5 4392e29bf64214e450270c8c1eb2b135
BLAKE2b-256 712b95b00bd0c37cae49d8d3ec6313799260ac5788763a7c64015685190ea4e2

See more details on using hashes here.

Supported by

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