dependency-injection
Designing loosely coupled code through dependency injection, reducing testability barriers and hidden dependencies.
от sethdford★ 51 звезда на GitHub
Установить dependency-injection в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
Dependency Injection
Fundamental pattern for loose coupling and testability.
Context
You are helping the engineer apply dependency injection. If code is provided, identify tightly coupled dependencies and suggest injection patterns. DI is not optional for testable, maintainable code.
Domain Context
- Loose Coupling: Dependencies are explicit parameters, not hidden inside the class
- Testability: You can inject mocks, stubs, or fakes for testing without modification
- Inversion of Control: Class doesn't create its dependencies; they're provided
- Three Patterns: Constructor Injection (preferred), Setter Injection, Interface Injection
- Container: Optional; frameworks like Spring, Guice manage dependency graphs
Instructions
- Identify Hard Dependencies: What does the class create or reference? httpClient, database, logger?
- Declare Dependencies: Add constructor parameters for each dependency (prefer constructor over setter)
- Make Testable: Can you pass in a mock? If not, you're still too tightly coupled
- Use Interfaces: Depend on abstractions (Logger), not concrete implementations (ConsoleLogger)
- Avoid Service Locator: Don't pass in a container or factory; too implicit
- Document Contracts: Make it clear which dependencies are required vs. optional
- Keep Constructors Simple: Only assign fields; avoid logic or other dependencies
- Validate Dependencies: Check for null; fail fast if required dependency is missing
Anti-Patterns
- Instantiating dependencies directly in the constructor or methods; you've coupled to the concrete type
- Creating a "service locator" factory and passing it everywhere; defeats the purpose of DI
- Injecting heavy frameworks or containers; inject thin abstractions instead
- Setter injection when constructor injection would suffice; constructor makes requirements explicit
- Circular dependencies; indicates design problem; refactor to break the cycle
- Ignoring dependency lifetime (singleton vs. transient); wrong lifetimes cause bugs
Further Reading
- Martin Fowler, "Inversion of Control Containers and the Dependency Injection pattern"
- Michael Feathers, Working Effectively with Legacy Code, Chapter 20 (Dependency Injection)
- Gang of Four, Design Patterns, Strategy and Factory patterns
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл dependency-injection?
Designing loosely coupled code through dependency injection, reducing testability barriers and hidden dependencies.
Как установить скилл dependency-injection?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл dependency-injection запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
AnthropicGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
claude-api
AnthropicReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
algorithmic-art
AnthropicCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
mcp-integration
AnthropicThis skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…