Transient

Create a Fresh Instance Every Time

Transient is the library default for a reason — it is conservative. Each resolution produces a new instance. Use it when reuse would be incorrect, such as an accumulator or builder that gathers state during one operation.

When Transient Makes Sense

A builder that collects sections and produces output shouldn't reuse state from a previous build. Transient guarantees a clean slate every time.

report_builder.ts
import { Component, LoadAs } from "@noego/ioc";

@Component({ scope: LoadAs.Transient })
export class ReportBuilder {
  private sections: string[] = [];

  addSection(text: string): void {
    this.sections.push(text);
  }

  build(): string {
    return this.sections.join("\n\n");
  }
}

Transient Is Rare in Practice

Most NoEgo classes are stateless services that can safely be singletons. Even classes that hold state as they process data can often be designed to accept that state as method parameters rather than constructor arguments. Reserve transient for cases where the object's identity or accumulated state genuinely cannot be shared.

Rules

  • Use Transient rarely — only when every resolution must return a fresh mutable object.
  • Transient instances are not cached; the container creates a new instance every time.
  • Prefer Singleton + method parameters over Transient for most cases.
  • Good candidates: builders, accumulators, one-shot validators.
NoEgo

© 2025 NoEgo. All rights reserved.