All Projects → Neoteroi → rodi

Neoteroi / rodi

Licence: MIT license
Implementation of dependency injection for Python 3

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to rodi

Spork
Annotation processing and dependency injection for Java/Android
Stars: ✭ 77 (+83.33%)
Mutual labels:  dependency-injection
react-obsidian
Dependency injection framework for React and React Native applications
Stars: ✭ 17 (-59.52%)
Mutual labels:  dependency-injection
linker
Dependency Injection and Inversion of Control package
Stars: ✭ 33 (-21.43%)
Mutual labels:  dependency-injection
KataSuperHeroesIOS
Super heroes kata for iOS Developers. The main goal is to practice UI Testing.
Stars: ✭ 69 (+64.29%)
Mutual labels:  dependency-injection
inversify-koa-utils
inversify-koa-utils is a module based on inversify-express-utils. This module has utilities for koa 2 applications development using decorators and IoC Dependency Injection (with inversify)
Stars: ✭ 27 (-35.71%)
Mutual labels:  dependency-injection
BESTV
Android TV App powered by TMDb. It is a easy way to find the best TV content, the top movies, series... all of that in your TV.
Stars: ✭ 49 (+16.67%)
Mutual labels:  dependency-injection
Mimick.Fody
An integrated framework for dependency injection and aspect-oriented processing.
Stars: ✭ 15 (-64.29%)
Mutual labels:  dependency-injection
sirius-kernel
Provides common core classes and the dependency injection microkernel powering all SIRIUS applications
Stars: ✭ 30 (-28.57%)
Mutual labels:  dependency-injection
varie
A Typescript Framework For VueJS
Stars: ✭ 23 (-45.24%)
Mutual labels:  dependency-injection
NetteAdapterForSymfonyBundles
[DEPRECATED due to only 20 downloads per 2 years] Read an article about this idea
Stars: ✭ 15 (-64.29%)
Mutual labels:  dependency-injection
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (-40.48%)
Mutual labels:  dependency-injection
di
Simple and yet powerful Dependency Injection for Go
Stars: ✭ 188 (+347.62%)
Mutual labels:  dependency-injection
di speed
Speed comparison of Dependency Injection Container
Stars: ✭ 18 (-57.14%)
Mutual labels:  dependency-injection
solid-services
Solid.js library adding a services layer for global shared state.
Stars: ✭ 34 (-19.05%)
Mutual labels:  dependency-injection
async-injector
Reactive dependency injection for Rust.
Stars: ✭ 28 (-33.33%)
Mutual labels:  dependency-injection
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-28.57%)
Mutual labels:  dependency-injection
wedi
[Deprecated] A lightweight dependency injection (DI) library for TypeScript, along with a binding for React.
Stars: ✭ 22 (-47.62%)
Mutual labels:  dependency-injection
redi
💉 A dependency injection library for TypeScript & JavaScript, along with a binding for React.
Stars: ✭ 29 (-30.95%)
Mutual labels:  dependency-injection
AzureFunctions.Extensions
This provides some useful extensions for Azure Functions.
Stars: ✭ 81 (+92.86%)
Mutual labels:  dependency-injection
inject
A simple Kotlin multi-platform abstraction around the javax.inject annotations.
Stars: ✭ 42 (+0%)
Mutual labels:  dependency-injection

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].