Dependency Injection library for Python using decorators.
Project description
rr-inject
Dependency Injection library for Python using decorators.
Description
This library attempts to simply injection of dependencies into functions, constructors and methods with minimal overhead during coding. This is done using the decorator @inject. When the function, constructor or method with this decorator is invoked the arguments are inspected to find any named argument that the callable allows, but is not in the argument list. The missing values are then replaced with values registered as an injectable.
Basic Usage
from rr.inject import inject, injectable
# Mark the class type as injectable using the decorator.
@injectable(name="my_service")
class MyService:
pass
# Register an instance of the decorated class. No need to keep a reference
# since it will be held in the cache.
MyService()
# To receive the registered object as an argument simply use the name
# used to register the object as the parameter name and default it to `None`
@inject
def demo(my_service: MyService = None):
print(my_service)
demo()
Output
<__main__.MyService object at 0x000001780FBA0E20>
Alternatives to using @injectable
Using @injectable requires that only one instance of the object in the application.
Attempting to create a second instance will result in an error. You may also have
types that you do not want to define and link to the decorator to not create a dependency
on the rr-inject library in lower level packages. You can therefore use the register_injectable
function instead.
register_injectable("my_service", MyService)
or
register_injectable("my_service", lambda: MyService())
or
inst = MyService()
register_injectable("my_service", lambda: inst)
Downside of using the @inject decorator
The only downside is that callables marked with @inject cannot be invoked
with positional arguments, if you do you will get an error. All arguments must be
named.
@inject
def add_util(a: int, b: int, calc: Calculator = None) -> int:
return calc.add(a, b)
add_util(a=1, b=5)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rr_inject-0.1.0.tar.gz.
File metadata
- Download URL: rr_inject-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
351c9003429f6527f3b5c40475eaf0aacae7eef891dd783fd8192fdc2f4dc2b5
|
|
| MD5 |
90cb860c7936fd3baecea5bda415a7eb
|
|
| BLAKE2b-256 |
293137ee66825f5e0c17297d953a3e9372da3830480220179dec75f258afc336
|
File details
Details for the file rr_inject-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rr_inject-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
972d750a8ec06f1a28b025e2089c8d3903854d2a35c9c38d8075477c7149f698
|
|
| MD5 |
cf9754a187986b0d35627b836b0a872e
|
|
| BLAKE2b-256 |
62755af3af908998879ab919b276fe240956c2c89b63fe693ef82226270d8286
|