How Autowiring works in Kink
masterAutowiring allows the container to automatically resolve and inject dependencies into classes or functions. Kink uses two primary mechanisms to match dependencies:
- Argument Name Matching: The container looks for a service in
dithat matches the name of the function/method argument. - Type Annotation Matching: The container looks for a service in
dithat matches the type hint of the argument. This is the preferred method for static analysis. To use this, you should register services using their type as the key.
Precedence Order:
- Manually passed arguments (highest priority).
- Argument names.
- Type annotations (lowest priority).
from kink import di, inject
from sqlite3 import connect, Connection
# Registering by type for autowiring
di[Connection] = lambda di: connect(di["db_name"])
@inject
class UserRepository:
# 'db' will be resolved via the Connection type annotation
def __init__(self, db: Connection):
self.db = db