HoangNguyen0403

Dependency Injection

Best practices for DI, inject() usage, and providers.

HoangNguyen0403 501 148 Updated 4mo ago

Resources

1
GitHub

Install

npx skillscat add hoangnguyen0403/agent-skills-standard/dependency-injection

Install via the SkillsCat registry.

SKILL.md

Dependency Injection

Priority: P0 (CRITICAL)

Principles

  • inject() over Constructor: Use the inject() function for cleaner injection updates and type inference.
  • Tree Shaking: Always use providedIn: 'root' for services unless specific scoping is required.
  • Tokens: Use InjectionToken<T> for configuration, primitives, or interface abstraction.

Guidelines

  • Providers: Prefer provide* functions (e.g., provideHttpClient()) in app.config.ts over importing modules.
  • Factories: Use useFactory strictly when dependencies need runtime configuration.

Anti-Patterns

  • Global State: Avoid providedIn: 'platform' unless absolutely necessary (share between Micro Frontends).
  • Circular Deps: Use forwardRef only as a last resort; refactor architecture instead.

References