codewithmukesh/dotnet-claude-kit/dependency-injection
Claude Code and agent skill by codewithmukesh/dotnet-claude-kit
- community
Checking that the SKILL.md file is still in the repository.
Dependency injection patterns for .NET 10. Covers service lifetimes, keyed services, the decorator pattern, factory pattern, and common DI pitfalls. Load this skill when registering services, resolving lifetime issues, designing service composition, or when the user mentions "DI", "dependency injection", "service registration", "AddScoped", "AddTransient", "AddSingleton", "keyed services", "decorator", "Scrutor", "IServiceCollection", or "captive dependency".
Install
npx vskill@latest install codewithmukesh/dotnet-claude-kit/dependency-injectionSKILL.md
The same SKILL.md was published earlier as codewithmukesh/dotnet-claude-kit/dependency-injection.
Dependency Injection
Core Principles
- Constructor injection is the default — Inject dependencies through the constructor (primary constructors make this clean). No service locator, no property injection.
- Match lifetimes carefully — A singleton must never depend on a scoped or transient service. This is the most common DI bug.
- Register interfaces, resolve interfaces — Register
services.AddScoped<IOrderService, OrderService>(), not the concrete type. - Keyed services for strategy pattern — .NET 8+ keyed services replace manual factory patterns for selecting between implementations.
Patterns
Keyed Services (.NET 8+)
Use keyed services to register and resolve multiple implementations of the same interface.
Decorator Pattern
Registration by Convention (Scrutor)
Factory Pattern
When you need runtime logic to select an implementation.
Options Registration
Anti-patterns
Don't Capture Scoped Services in Singletons
Don't Register Everything as Singleton
Decision Guide
| Scenario | Recommendation | |
|---|---|---|
| Stateless service | Scoped (default) or Transient | |
| Configuration / cache | Singleton | |
| DbContext | Scoped (registered by AddDbContext) | |
| Multiple implementations | Keyed services (strategy pattern) | |
| Cross-cutting behavior | Decorator pattern | |
| Convention-based registration | Scrutor | |
| Runtime implementation selection | Factory delegate | |
| Audit existing registrations | get_di_registrations MCP tool — lifetimes, duplicates, captive-dependency risks in one call | |
| Strongly-typed config | AddOptions<T>().BindConfiguration() |
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/dependency-injection/SKILL.md
Popularity
- Repository stars
- 655
- Repository forks
- 150
- 7-day trend score
- 32
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 21 Jun 2026, 5 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/codewithmukesh/dotnet-claude-kit/dependency-injection)