Dependency Injection · API Governance Rules

Dependency Injection API Rules

Spectral linting rules defining API design standards and conventions for Dependency Injection.

0 Rules
View Rules File View on GitHub

Spectral Ruleset

dependency-injection-rules.yml Raw ↑
x-name: Dependency Injection Rules
description: Rules and guidance for applying the Dependency Injection design pattern.
x-version: 1.0.0
x-created: 2026-04-28
x-modified: 2026-04-28
rules:
  - id: DI-001
    name: Prefer Constructor Injection
    severity: warning
    description: Inject required collaborators through the constructor by default.
    rationale: Required dependencies become explicit and the resulting object is always in a valid state.

  - id: DI-002
    name: Use Setter Injection for Optional Dependencies
    severity: info
    description: Reserve setter injection for genuinely optional collaborators or post-construction reconfiguration.
    rationale: Optional setters keep the constructor focused while signaling that the dependency may be absent.

  - id: DI-003
    name: Depend on Abstractions
    severity: error
    description: Type constructor parameters and fields against interfaces, not concrete classes.
    rationale: Programming to interfaces preserves substitutability and unlocks test doubles.

  - id: DI-004
    name: No new Inside Business Logic
    severity: warning
    description: Avoid instantiating collaborators with new inside methods that hold business logic.
    rationale: Hidden construction couples logic to specific implementations and frustrates testing.

  - id: DI-005
    name: Avoid Service Locator in Domain Code
    severity: warning
    description: Domain and application services should not call a service locator to fetch collaborators.
    rationale: Locators hide dependencies and reintroduce the coupling DI removes.

  - id: DI-006
    name: Composition Root at the Edge
    severity: error
    description: Wire the object graph in a single composition root at application startup.
    rationale: Centralizing wiring keeps the rest of the codebase free of container concerns.

  - id: DI-007
    name: Limit Constructor Parameter Count
    severity: warning
    description: A constructor that requires more than five collaborators signals a violation of the single responsibility principle.
    rationale: A bloated constructor reveals an over-scoped class that should be decomposed.

  - id: DI-008
    name: Forbid Field Injection in Production Code
    severity: warning
    description: Avoid annotation-driven field injection on private fields outside frameworks that require it.
    rationale: Field injection hides dependencies, prevents immutability, and breaks plain-object construction in tests.

  - id: DI-009
    name: Scope Lifetimes Explicitly
    severity: info
    description: Declare singleton, scoped, and transient lifetimes intentionally; do not rely on container defaults.
    rationale: Implicit lifetimes produce subtle threading and state-sharing bugs.

  - id: DI-010
    name: Avoid Circular Dependencies
    severity: error
    description: Detect and break circular dependencies between injected components.
    rationale: Cycles indicate a layering mistake and often manifest as initialization order bugs.

  - id: DI-011
    name: Configuration Separate from Usage
    severity: warning
    description: Externalize wiring configuration from the classes that consume the dependencies.
    rationale: Decoupling configuration from logic is the foundational principle that DI exists to express.

  - id: DI-012
    name: Test Without the Container
    severity: info
    description: Unit tests should construct objects directly with test doubles rather than booting the DI container.
    rationale: Container-free tests run faster and prove that injected classes can be instantiated explicitly.

Work with this as data

Every ruleset here is available over the APIs.io API and to AI agents over MCP.

MCP server

One button, every client — Claude, Cursor, VS Code and the rest.

https://apis.io/mcp

Tools for spectral rules

4 MCP tools reach this
  • find_rulesBrowse and filter every ruleset in the catalog.
  • apis_io_searchSTART HERE — APIs, providers and tags for one query, each with its total.
  • resolveTurn a domain, URL or GitHub org into the provider it belongs to.
  • find_cohortsEvery scored population of providers in the catalog.
All 92 tools →

Call it yourself

curl for this page
This ruleset
curl "https://apis.io/api/v1/rules/dependency-injection-rules"
All spectral rules
curl "https://apis.io/api/v1/rules?limit=25"

Discovery needs no key. Ratings and market analysis are Pro.

Get an API key

Free tier, no form to fill in. Signing in shares your email address with us — we store it to create your key and to recognise you if you sign in with another provider. See our Privacy Policy and Terms.

A second provider on the same verified email joins the account you already have.