jagreehal avatar

fn-args-deps

Enforce the fn(args, deps) pattern: functions over classes with explicit dependency injection

by jagreehal|Open Source

Functions Over Classes: fn(args, deps)

Critical rules

  • Business logic is fn(args, deps) — never service classes. args are per-call; deps are long-lived collaborators.
  • Declare a per-function deps type. No god AllServiceDeps objects.
  • Wire deps once at a composition root. Handlers call factories, not constructors mid-request.
  • Inject only network/disk/clock collaborators. Import pure utilities directly.
  • Use import type for infra interfaces; instances arrive via deps.
  • Before writing examples or grouping choices, read references/examples.md and references/patterns.md.

Workflow

  1. Identify business logic that should be a function (or extract it from a class).
  2. Split per-call data into args and collaborators into a named XDeps type.
  3. Before choosing individual vs grouped injection, read references/patterns.md.
  4. Implement fn(args, deps). Keep classes only as thin framework wrappers or stateful resources.
  5. Add a factory / composition-root wire-up so call sites never construct deps.
  6. Test with mock<XDeps>() — no class instantiation. See references/examples.md.

Resources

  • references/examples.md — WRONG vs CORRECT signatures, composition root, NestJS wrapper, tests. Read when implementing or reviewing.
  • references/patterns.md — grouping, migration, when classes are OK, enforcement, rationalizations. Read before structuring modules.

Validation

  • Every business function is fn(args, deps) with a per-function deps type
  • No unused deps fields; no god deps bag
  • Deps wired once at composition root
  • Infra via import type only; instances via deps
  • Pure utilities imported, not injected
  • Remaining classes are wrappers, stateful resources, or builders
  • Tests use mock<XDeps>() without instantiating a class

Constraints

  • Framework-mandated classes, connection pools, caches, and fluent builders may stay as thin wrappers over pure functions.
  • Related: result-types, validation-boundary, testing-strategy, writing-tests, observability, strict-typescript, pattern-enforcement.
fn-args-deps - AI Agent Skill for Claude Code & Cursor | Agent Skills