
fn-args-deps
Enforce the fn(args, deps) pattern: functions over classes with explicit dependency injection
作者 jagreehal|オープンソース
Functions Over Classes: fn(args, deps)
Critical rules
- Business logic is
fn(args, deps)— never service classes.argsare per-call;depsare long-lived collaborators. - Declare a per-function deps type. No god
AllServiceDepsobjects. - 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 typefor infra interfaces; instances arrive viadeps. - Before writing examples or grouping choices, read references/examples.md and references/patterns.md.
Workflow
- Identify business logic that should be a function (or extract it from a class).
- Split per-call data into
argsand collaborators into a namedXDepstype. - Before choosing individual vs grouped injection, read references/patterns.md.
- Implement
fn(args, deps). Keep classes only as thin framework wrappers or stateful resources. - Add a factory / composition-root wire-up so call sites never construct deps.
- 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
depsfields; no god deps bag - Deps wired once at composition root
- Infra via
import typeonly; instances viadeps - 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.