Container¶
The Container is the central component that manages dependency registration and resolution.
Registration¶
from inversipy import Container, Scopes
container = Container()
# Register with automatic resolution (type = implementation)
container.register(MyService)
# Register with explicit implementation
container.register(IService, implementation=MyServiceImpl)
# Register with factory function
container.register_factory(MyService, lambda: MyService("config"))
# Register with pre-created instance
instance = MyService()
container.register_instance(MyService, instance)
All registration methods return self for chaining:
container = Container()
container.register(Database, scope=Scopes.SINGLETON).register(UserRepository).register(UserService)
Resolution¶
# Resolve a dependency
service = container.get(MyService)
# Check if registered
if container.has(MyService):
service = container.get(MyService)
# Try to get (returns None if not found)
service = container.try_get(MyService)
Async Resolution¶
All resolution methods have async counterparts:
Factory Functions with Dependencies¶
Factory functions can have dependencies that are automatically resolved:
container.register(Config, scope=Scopes.SINGLETON)
def create_database(config: Config) -> Database:
return Database(config.db_url)
container.register_factory(Database, create_database, scope=Scopes.SINGLETON)
# Config is injected automatically
db = container.get(Database)