# Dependency Injection

> Use when: decouple components by injecting their dependencies instead of constructing them inline.

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

---


Goal: components that receive collaborators, making them testable and swappable.

Use for:
- removing hard-coded dependencies and global state
- making code testable with substitutable collaborators
- wiring an application's object graph

Workflow:
1. Identify what a component needs from outside.
2. Depend on an interface or abstraction, not a concrete type.
3. Accept dependencies via the constructor (preferred).
4. Construct and wire the graph at the composition root.
5. Inject test doubles in tests instead of real services.
6. Verify behavior with collaborators substituted.

Patterns:
- constructor injection as the default
- composition root that wires everything once
- interfaces at the seams you need to swap
- a container only when wiring becomes unwieldy

Rules:
- depend on abstractions, not concretions
- do not new up dependencies inside business logic
- keep wiring at the edge, not scattered through the code
- avoid the service-locator anti-pattern; inject explicitly

