hoc-wire-dependencies
How a class reaches the modules and classes it depends on: never by the name it imported, always through a static getter, so that a subclass can patch one and…
от openreachtech★ 17 звёзд на GitHub
Установить hoc-wire-dependencies в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
Wiring Dependencies
A class reaches what it depends on through seams it declares itself: a static getter that
names the dependency, and — where the dependency is a class to instantiate — a static factory
method that builds it. Both are resolved through this, so a subclass overrides either one and
every call site follows without being touched.
Core principle: a dependency is reached through a seam, never through its imported name
- When a module (the class itself) depends on another module — whether a native module, a third-party module, or an in-house module — do not use the reference to that dependency directly as the imported identifier. Extract it into a getter.
- Reason:
- Easy patching: if the dependency has a bug and needs an emergency patch, it suffices to override the seam in a subclass; call sites do not need to change. If the dependency were used directly, every call site would need to explicitly pass the patched one.
- Easy testing: in tests, swapping the getter replaces the dependency entirely, and
jest.spyOn(ThisClass, 'createExternalApiClient')swaps a factory's return value — with the same call form as production. - Encapsulation of the dependency: the caller's concern is the class in question, not its internal dependencies. The seam hides the dependency relationship inside the class, lowering coupling.
- The seams are lightweight DI (Dependency Injection), achieving, without a DI container: production = creation of the real dependency / testing = swap / hotfix = swap to a patched dependency.
Which seams a dependency takes
| The dependency | Seams |
|---|---|
A class to instantiate via new / .create(...) |
A static getter named [TargetClassName]Ctor, and a dedicated factory method that instantiates through it |
A module used as it is, such as fs |
A single static getter that returns the module as-is |
A class to instantiate: the Ctor getter and the dedicated factory method
When the dependency is a class to be instantiated via new / .create(...), extract it into
a static getter named [TargetClassName]Ctor, and instantiate it via a dedicated factory method.
// NG: using a dependency class directly
import Aggregator from './Aggregator.js'
export default class BaseRewardCalculator {
constructor ({
config,
aggregator,
}) {
this.config = config
this.aggregator = aggregator
}
static create ({
config,
} = {}) {
const aggregator = Aggregator.create({ config }) // NG: instantiated directly
return new this({
config,
aggregator,
})
}
}
// OK: extract into a static getter named [TargetClassName]Ctor, and instantiate via a dedicated factory method
import Aggregator from './Aggregator.js'
export default class BaseRewardCalculator {
constructor ({
config,
aggregator,
}) {
this.config = config
this.aggregator = aggregator
}
static create ({
config,
} = {}) {
const aggregator = this.createAggregator({ config }) // OK: via a dedicated factory method
return new this({
config,
aggregator,
})
}
static get AggregatorCtor () {
return Aggregator
}
static createAggregator ({
config,
}) {
return this.AggregatorCtor.create({ config })
}
}
- When applying an emergency patch in a subclass, overriding
AggregatorCtoralone suffices.
// OK: swapping the corrected class in only requires overriding AggregatorCtor
import RoundHalfUpAggregator from './RoundHalfUpAggregator.js'
export default class UserRewardCalculator extends BaseRewardCalculator {
/** @override */
static get AggregatorCtor () {
return RoundHalfUpAggregator
}
}
Name the getter [TargetClassName]Ctor
- When holding a target class's constructor in a static getter for delegation purposes, the getter
name should be "target class name +
Ctor". - Example: for the
Constraintclass's constructor →.get:ConstraintCtor; forClient→.get:ClientCtor. - When the target class is undetermined, such as in an abstract base class, a generic name
expressing the role (e.g.
TargetCtor) is fine.
// OK: static getter holding the constructor of the delegate target class ([TargetClassName]Ctor)
static get ConstraintCtor () {
return SampleConstraint
}
Build the dependency in the dedicated factory method, never in a default argument
- Do not directly create a dependency class in a default argument of
.create(...), etc. That is, do not directly write eithernew Dependency(...)(anewexpression) orDependency.create(...)(calling the dependency class's factory method). Extract the creation of the dependency class into a dedicated factory method (e.g.this.createExternalApiClient()) and go through it. - Unless there is a specific reason otherwise, the name of the dedicated factory method should basic
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- references/factory-class.md
FAQ
Что делает скилл hoc-wire-dependencies?
How a class reaches the modules and classes it depends on: never by the name it imported, always through a static getter, so that a subclass can patch one and a test can swap it. Use when a class imports anything it uses, a native module such as `fs` included, builds a dependency in `static create()` or anywhere else, or a dependency has to be patched or mocked. How a getter's body is written belongs to the accessors convention; `static create()` in general, to the methods convention.
Как установить скилл hoc-wire-dependencies?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл hoc-wire-dependencies запускает скрипты?
Нет, скилл состоит только из инструкций (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…