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

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. Refer to the framework's documentation for more information on how dependency injection is

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.0.tar.gz (11.5 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.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rodi-1.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for rodi-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d40847e6e19c109a4054a74a3b118f73aef6b5d7da4cd6fa68177469991d0f4c
MD5 72a933af4ae1e53e72427b8590a8454b
BLAKE2b-256 cceb0d874652bc6c70e8538d9cd7b54a69acaa18c04fb1889351d0cdb3487444

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rodi-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for rodi-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a63f2ec1425ad42a09fa3e78f272b31525ab88f4e9dd5939dc4ae52ce094a77
MD5 44bd64b12bf8d7ddd5a1698419f2a7a3
BLAKE2b-256 4256247bffa295bbb1c369a3a6ee356d520886dad7e0e615134036eddd52607b

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