dependency-injection_skill

This skill explains and demonstrates myfy dependency injection with scopes, providers, and route integration to improve modularity and testability.
  • Python

83

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill psincraian/myfy --skill dependency-injection

  • SKILL.md5.2 KB

Overview

This skill documents myfy's constructor-based dependency injection system with three lifecycle scopes: SINGLETON, REQUEST, and TASK. It explains how to declare providers with the @provider decorator, use qualifiers and names, and how modules like WebModule, DataModule, TasksModule, and others commonly rely on DI. The goal is to make dependency patterns explicit, avoid lifecycle mistakes, and speed up correct wiring of services.

How this skill works

Providers are ordinary functions annotated with @provider and a scope; the DI container creates and injects their return values where needed. SINGLETON instances live for the application lifetime, REQUEST instances are created per HTTP request, and TASK instances are created per background task. The container enforces scope rules at startup and supports qualifiers, named providers, reloadable settings, and test-time overrides.

When to use it

  • Wiring shared resources like DB pools, HTTP clients, and caches (SINGLETON).
  • Creating per-request resources such as DB sessions, request logger, or user context (REQUEST).
  • Providing task-scoped items like job context or task-specific loggers (TASK).
  • When using @provider, qualifiers, or name-based resolution for multiple providers.
  • When writing routes that mix path/query/body params and DI-resolved parameters.

Best practices

  • Keep provider functions pure and free of side effects; construct objects only.
  • Prefer SINGLETON for connection pools and expensive shared resources. Use REQUEST for request-local state and TASK for background-job state.
  • Avoid SINGLETON depending on REQUEST or TASK scoped types; use factory providers to bridge scopes.
  • Annotate return types on all providers so the container can resolve types reliably.
  • Use qualifiers for multiple providers of the same type and override() in tests for safe mocks.

Example use cases

  • Declare a SINGLETON database_pool used by DataModule and Services across the app.
  • Create a REQUEST-scoped AsyncSession injected into route handlers for transactional work.
  • Provide a TASK-scoped task_logger for background job tracing in TasksModule.
  • Expose two Database providers (primary, replica) and inject by Qualifier in different routes.
  • Use a singleton factory to produce request-scoped services when a singleton must not capture request state.

FAQ

Scope violations are detected at startup and will fail; use a factory SINGLETON that creates REQUEST-scoped instances instead.

How do I choose between QUALIFIER and name?

Use qualifier annotations when you need type-based selection in code; use names for explicit lookup or tooling scenarios.

Can I hot-reload provider settings?

Yes—declare reloadable setting names on the provider to allow hot-reload of configured fields.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational