dependency-injection
Skill1 starsChanged 2 months agoMITView on GitHub
Use when writing or reviewing code with object graphs, service dependencies, or testability concerns. Covers constructor injection, DI containers, lifetimes, and anti-patterns (service locator, ambient context, over-injection).
We split this skill into the rules it gives an agent
2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 10 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 60
Use a container (FastAPI's Depends, tsyringe, Wire for Go) when:
- Distinctive: line 65
Don't use a container for:
“Common” is measured: kept by at least 6 in 10 of the 10 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Dependency Injection
One rule: depend on abstractions, receive them via constructor, never create them internally.
The Pattern
# BAD — hard to test, tightly coupled
class OrderService:
def __init__(self):
self.db = Database() # creates its own dependency
self.email = EmailClient() # can't inject fakes in tests
# GOOD — testable, decoupled
class OrderService:
def __init__(self, db: Database, email: EmailClient) -> None:
self.db = db
self.email = email// BAD
class OrderService {
private db = new Database(); // hidden dependency
}
// GOOD
class OrderService {
constructor(private db: Database, private email: EmailClient) {}
}Rules
| Do | Don't |
| Inject dependencies via constructor | Create dependencies with new inside a class |
| Depend on interfaces/protocols | Depend on concrete types when an abstraction exists |
| Make dependencies explicit | Use service locators (Container.get(...)) inside business logic |
| Keep constructors simple — no logic | Do work in __init__/constructors |
| Use fakes/stubs in tests | Patch global singletons in tests |
When to Use a DI Container
Use a container (FastAPI's Depends, tsyringe, Wire for Go) when:
- The object graph has 3+ levels of nesting
- You need lifetime management (singleton vs. transient vs. scoped)
- You need request-scoped dependencies (e.g., database sessions per HTTP request)
Don't use a container for:
- Small scripts or utilities (manual wiring is simpler and clearer)
- Value objects (DTOs, records) — they carry data, not behavior
- Stateless pure functions
Lifetimes
| Lifetime | When to use | Example |
| Singleton | Shared state across the app; expensive to create | DatabasePool, ConfigLoader |
| Scoped | One instance per request/unit of work | DbSession, UserContext |
| Transient | Stateless; cheap to create | EmailFormatter, Validator |
Anti-Patterns
Service Locator — business logic calls Container.get(Service) directly. Makes dependencies invisible.
Ambient Context / Thread-local — global mutable state accessed via static accessors. Breaks testability.
Constructor over-injection (> 4 params) — sign the class violates SRP; split responsibilities.
Circular dependency — A depends on B which depends on A. Refactor to break the cycle, often by extracting a shared interface.
See Also
patterns/dependency-injection/README.md— pattern directory entry.claude/skills/solid-principles/SKILL.md— DIP (Dependency Inversion Principle) explains why.claude/skills/testing/SKILL.md— how DI enables unit testing with fakes
Other versions11 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 11
- trezor/trezor-suite★ 1.1ksame name
- jame581/GodotPrompter★ 870same name
- GoBetterAuth/go-better-auth★ 273same name
- Resgrid/Core★ 229same name
- garrettw/php-arch-skills★ 13same name
- HamzaSheikh768/Agents-Skills-Lap★ 3same name
- ffsshhttiikk/opencode-agents-skills★ 2same name
- kimtth/agent-skill-100-lines-or-less★ 2same name
- aubynsamuel/android-dev-skills★ 1same name
- gerivdb/SKILLS★ 1same name
1 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository77 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/accessibility.mdcCursor rule
- .cursor/rules/agentharness-router.mdcCursor rule
- .cursor/rules/agentic-loops.mdcCursor rule
- .cursor/rules/api-design.mdcCursor rule
- .cursor/rules/audit-review-followup.mdcCursor rule
- .cursor/rules/big-work-plan-review.mdcCursor rule
- .cursor/rules/branching.mdcCursor rule
- .cursor/rules/clean-architecture.mdcCursor rule
- .cursor/rules/code-review-api.mdcCursor rule
- .cursor/rules/code-review-db.mdcCursor rule
- .cursor/rules/code-review.mdcCursor rule
- .cursor/rules/code-review-ui.mdcCursor rule
- .cursor/rules/committing.mdcCursor rule
- .cursor/rules/database-conventions.mdcCursor rule
- .cursor/rules/dependency-audit.mdcCursor rule
- .cursor/rules/dependency-injection.mdcCursor rule
- .cursor/rules/design-patterns.mdcCursor rule
- .cursor/rules/docker-conventions.mdcCursor rule
- .cursor/rules/error-handling.mdcCursor rule
- .cursor/rules/file-placement-policy.mdcCursor rule
- .cursor/rules/github-issue-triage.mdcCursor rule
- .cursor/rules/go-conventions.mdcCursor rule
- .cursor/rules/harness-feedback.mdcCursor rule
- .cursor/rules/logging.mdcCursor rule
- .cursor/rules/multi-agent-coordination.mdcCursor rule
- .cursor/rules/mutation-testing.mdcCursor rule
- .cursor/rules/performance-profiling.mdcCursor rule
- .cursor/rules/planning-with-files.mdcCursor rule
- .cursor/rules/port-agent-config.mdcCursor rule
- .cursor/rules/project-bootstrap.mdcCursor rule
- .cursor/rules/python-conventions.mdcCursor rule
- .cursor/rules/react-best-practices.mdcCursor rule
- .cursor/rules/refactoring.mdcCursor rule
- .cursor/rules/requirements-clarification.mdcCursor rule
- .cursor/rules/security-review.mdcCursor rule
- .cursor/rules/solid-principles.mdcCursor rule
- .cursor/rules/testing.mdcCursor rule
- .cursor/rules/typescript-conventions.mdcCursor rule
- accessibilitySkill
- agentic-loopsSkill
- api-designSkill
- audit-review-followupSkill
- big-work-plan-reviewSkill
- branchingSkill
- clean-architectureSkill
- code-review-apiSkill
- code-review-dbSkill
- code-reviewSkill
- code-review-uiSkill
- committingSkill
- database-conventionsSkill
- dependency-auditSkill
- design-patternsSkill
- docker-conventionsSkill
- error-handlingSkill
- file-placement-policySkill
- github-issue-triageSkill
- The first 60 shown.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

