Injector.py
: The library is commonly used to simplify SOLID principles in frameworks like Django and FastAPI .
: Marks constructors or methods that need dependencies automatically provided.
from injector import Injector injector = Injector([MyModule()]) # Pass your modules here service = injector.get(Service) # Automatically creates Database and Service print(service.db.status) # "Connected" Use code with caution. Copied to clipboard injector.py
: Control object lifetimes. Common scopes include singleton (one instance for the whole app) and NoScope (new instance every time).
from injector import Module, singleton class MyModule(Module): def configure(self, binder): # Always provide the same instance (Singleton) binder.bind(Database, to=Database(), scope=singleton) Use code with caution. Copied to clipboard : The library is commonly used to simplify
: The central orchestrator that builds the dependency graph and provides objects to your application. 3. Basic Implementation Guide Step A: Define Your Classes
Use type hints to declare what a class needs. The framework uses these hints to "look up" the correct dependency. Copied to clipboard : Control object lifetimes
A guide to implementing injector.py revolves around three main abstractions: