# Dependency Injection

> Best practices for DI, inject() usage, and providers.

- Skill: `fierzone/dependency-injection` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add fierzone/dependency-injection`
- Raw SKILL.md: https://api.skillmd.com/api/skills/fierzone/dependency-injection/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: fierzone (https://skillmd.com/u/fierzone)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/fierzone/dependency-injection

---


# Dependency Injection

## **Priority: P0 (CRITICAL)**

## Principles

- **`inject()` over Constructor**: Use the `inject()` function for cleaner injection updates and type inference.
- **Tree Shaking**: Always use `providedIn: 'root'` for services unless specific scoping is required.
- **Tokens**: Use `InjectionToken<T>` for configuration, primitives, or interface abstraction.

## Guidelines

- **Providers**: Prefer `provide*` functions (e.g., `provideHttpClient()`) in `app.config.ts` over importing modules.
- **Factories**: Use `useFactory` strictly when dependencies need runtime configuration.

## Anti-Patterns

- **Global State**: Avoid `providedIn: 'platform'` unless absolutely necessary (share between Micro Frontends).
- **Circular Deps**: Use `forwardRef` only as a last resort; refactor architecture instead.

## References

- [DI Patterns](references/di-patterns.md)

