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
Use type hints to declare what a class needs. The framework uses these hints to "look up" the correct dependency. injector.py
If you have multiple implementations for one interface (e.g., a "TestDB" and a "LiveDB"), use a Module to specify which one to use. If you have multiple implementations for one interface (e
For further details, consult the official Injector documentation . Make Your Django Project Less Confusing with Design Pattern A guide to implementing injector
: The library is commonly used to simplify SOLID principles in frameworks like Django and FastAPI .
A guide to implementing injector.py revolves around three main abstractions:
: Control object lifetimes. Common scopes include singleton (one instance for the whole app) and NoScope (new instance every time).