myfy dependency injection with scopes (SINGLETON, REQUEST, TASK). Use when working with @provider decorator, DI container, scopes, injection patterns, or understanding how WebModule, DataModule, FrontendModule, TasksModule, UserModule, CliModule, AuthModule, and RateLimitModule use dependency injection.
View on GitHubSelect agents to install to:
npx add-skill https://github.com/psincraian/myfy/blob/main/plugins/claude-code/skills/dependency-injection/SKILL.md -a claude-code --skill dependency-injectionInstallation paths:
.claude/skills/dependency-injection/# Dependency Injection in myfy
myfy uses constructor-based dependency injection with three scopes.
## Scopes
| Scope | Lifetime | Use Case |
|-------|----------|----------|
| SINGLETON | Application lifetime | Config, pools, services, caches |
| REQUEST | Per HTTP request | Sessions, user context, request logger |
| TASK | Per background task | Task context, job-specific state |
## Provider Declaration
```python
from myfy.core import provider, SINGLETON, REQUEST, TASK
# SINGLETON: Created once, shared across all requests
@provider(scope=SINGLETON)
def database_pool(settings: DatabaseSettings) -> DatabasePool:
return DatabasePool(settings.database_url)
# REQUEST: Created per HTTP request, auto-cleaned up
@provider(scope=REQUEST)
def db_session(pool: DatabasePool) -> AsyncSession:
return pool.get_session()
# TASK: Created per background task execution
@provider(scope=TASK)
def task_logger(ctx: TaskContext) -> Logger:
return Logger(task_id=ctx.task_id)
```
## Provider Options
```python
@provider(
scope=SINGLETON, # Lifecycle scope
qualifier="primary", # Optional qualifier for multiple providers
name="my_database", # Optional name for resolution
reloadable=("log_level",), # Settings that can hot-reload
)
def database(settings: Settings) -> Database:
return Database(settings.db_url)
```
## Scope Dependency Rules
1. **SINGLETON** can depend on: other singletons only
2. **REQUEST** can depend on: singletons and other request-scoped
3. **TASK** can depend on: singletons and other task-scoped
**SINGLETON cannot depend on REQUEST/TASK** - this fails at compile time!
```python
# WRONG - will fail at startup
@provider(scope=SINGLETON)
def bad_service(session: AsyncSession): # AsyncSession is REQUEST scope
return MyService(session)
# CORRECT - use factory pattern
@provider(scope=SINGLETON)
def service_factory(pool: DatabasePool) -> ServiceFactory:
return ServiceFactory(pool)
@provider(sco