Last updated:
0 purchases
injectable 4.0.1
Usage Examples π© | Developer Reference π©βπ» | Authors π«
license
docs
tests
package
Injectable is an elegant and simple Dependency Injection framework built with Heart
and designed for Humans.
from typing import Annotated, List
from injectable import Autowired, autowired
from models import Database
from messaging import Broker
class Service:
@autowired
def __init__(
self,
database: Annotated[Database, Autowired],
message_brokers: Annotated[List[Broker], Autowired],
):
pending = database.get_pending_messages()
for broker in message_brokers:
broker.send_pending(pending)
from abc import ABC
class Broker(ABC):
def send_pending(messages):
...
from injectable import injectable
@injectable
class Database:
...
from messaging import Broker
from injectable import injectable
@injectable
class KafkaProducer(Broker):
...
from messaging import Broker
from injectable import injectable
@injectable
class SQSProducer(Broker):
...
Features youβll love β€οΈ
Autowiring: injection is transparent to the function. Just decorate the function
with @autowired and annotate parameters with Autowired, thatβs it.
Automatic dependency discovery: just call load_injection_container() at
the root of your project or pass the root path as an argument. All classes decorated
with @injectable will be automatically discovered and ready for injection.
Qualifier overloading: declare as many injectables as you like for a single
qualifier or extending the same base class. You can inject all of them just by
specifying a typing.List to Autowired:
deps: Annotated[List["qualifier"], Autowired].
Transparent lazy initialization: passing the argument lazy=True for
Autowired will make your dependency to be initialized only when actually used, all
in a transparent fashion.
Singletons: decorate your class with @injectable(singleton=True) and only a
single instance will be initialized and shared for injection.
Namespaces: specify different namespaces for injectables as in
@injectable(namespace="foo") and then just use them when annotating your
parameters as in dep: Annotated[..., Autowired(namespace="foo")].
Linters friendly: Autowired is carefully designed to comply with static linter
analysis such as PyCharmβs to preserve the parameter original type hint.
These are just a few cool and carefully built features for you. Check out our docs!
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.