Hoc wire dependencies

From openreachtech/hora-skills-ort-core · View interactive version

Hoc wire dependencies is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. 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()

How to use this skill

Skill file

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

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.

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.

jest.spyOn(ThisClass, 'createExternalApiClient') swaps a factory's return value — with the

same call form as production.

internal dependencies. The seam hides the dependency relationship inside the class, lowering

coupling.

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 })
  }
}
// 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`

name should be "target class name + Ctor".

.get:ClientCtor.

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 write either new Dependency(...) (a new expression) or

Dependency.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.

basically be "create + class name" (e.g. ExternalApiClient → createExternalApiClient).

// NG: directly instantiating a dependency class
static create ({
  externalApiClient = ExternalApiClient.create({ env }),
} = {}) {
  // ...
}

// OK: go through a factory method
static create ({
  externalApiClient = this.createExternalApiClient(),
} = {}) {
  // ...
}

A dependency built on the fly goes through the factory method too

when an instance method creates one temporarily and discards it. Do not new it there either.

decides it is that the instance is used by delegating to it, not where the class comes from.

// NG: a delegate built on the fly with a direct new
sendRequest () {
  const client = new ExternalApiClient({ env })

  return client.send()
}

// OK: built through the dedicated factory method
sendRequest () {
  const client = this.createExternalApiClient()

  return client.send()
}

A module used as it is: a static getter that returns it

there is no instantiation via new / .create(...) — neither the Ctor suffix nor a dedicated

factory method is needed. Define a single static getter that returns the dependency module

as-is.

nothing in it belongs to an instance. What decides the kind of a getter is its body, not the

kind of value it holds — a getter that reaches for no instance state is a static getter,

whatever it returns.

convention's rule against calling a method from a getter. The getter must return nothing but the

module reference itself.

// OK: a static getter that returns a native module as-is, reached through #get:Ctor
import fs from 'node:fs'

export default class DeepLoader {
  static get fs () {
    return fs
  }

  get Ctor () {
    return /** @type {typeof DeepLoader} */ (this.constructor)
  }

  collectFileNames ({
    poolPath = this.poolPath,
  } = {}) {
    return this.Ctor.fs.readdirSync(poolPath)
      .filter(it => !it.startsWith('.'))
  }
}

A function a module holds as a member: a getter that returns a call-time wrapper

express.json out of express, for example — the getter returns an arrow function that calls

the member on the module. It never returns the bare member.

that no longer has express behind it, and whoever calls it as this.Ctor.factory(...) runs it

with this set to something else. Whether the member reads this today does not decide it: the

version a consumer installs may differ, and the failure then surfaces in their environment, far

from its cause.

consumer, so a correction to the getter reaches them only after they update.

seam's function is used, so this does not break the rule that a getter never calls a method.

receiver to lose, and is returned as it is.

// NG: the member is cut off from its module
static get factory () {
  return express.json
}

// OK: the call is made on the module, each time the function is used
static get factory () {
  return (...args) => express.json(...args)
}

// OK: a default export has no receiver
static get factory () {
  return cors
}

Consolidating the creation of a frequently used class

the BaseFactory example), and the intent behind the selection (.get:TargetCtor) /

instantiation (.createTarget()) two-stage separation, are collected in

references/factory-class.md.

Where this stops

convention. #get:Ctor, and the reservation of its name, belong to /hoc-classes-ctor.

defaults it applies, and when a direct new is allowed — belongs to the methods convention.

— belongs to the manifest pattern convention.

and the failure turned into a result around it — belongs to /hoc-classes-delegation.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.