AGENT SKILL
nestjs-dependency-injection
Use NestJS providers and injection tokens for testable, decoupled services
- License 许可
- Apache-2.0
- 所属仓库
- linktogo/maggie
- 最近更新
- 2026-11-14
- Stars
- ★ 2
- Forks
- 2
01 前言 Front Matter
- name
- nestjs-dependency-injection
- globs
- [ "**/*.service.ts", "**/*.module.ts" ]
- description
- Use NestJS providers and injection tokens for testable, decoupled services
03 SKILL.md 正文
NestJS dependency injection
Let Nest wire dependencies. Never new a service that has its own
dependencies — inject it so it can be swapped in tests.
Rules
- Constructor injection with
private readonly:constructor(private readonly users: UsersService) {} - Depend on an interface via an injection token when you need multiple
implementations or want to fake I/O in tests:
export const MAILER = Symbol('MAILER'); // provider: { provide: MAILER, useClass: SmtpMailer } constructor(@Inject(MAILER) private readonly mailer: Mailer) {} - Default to the singleton scope. Reach for
Scope.REQUESTonly when a provider genuinely needs per-request state — it is slower and taints everything that injects it. - Configuration comes from
ConfigService, notprocess.envread inline.
Testing
- In unit tests, build a module with
Test.createTestingModuleand override providers (.overrideProvider(MAILER).useValue(fakeMailer)). - If a class is hard to test because of its dependencies, that is a design signal — inject a narrower abstraction rather than mocking the world.
04 同仓库更多 Skills
vuejs-state-management
Manage Vue application state with Pinia and the right state locality
★ 2
vuejs-composition-api
Write Vue 3 components with the Composition API, script setup, and composables
★ 2
reactjs-hooks
Use React hooks correctly — dependencies, effects, and custom hooks
★ 2
reactjs-component-design
Write small, composable React components with clean props and no needless state
★ 2
postgres-query-performance
Diagnose and fix slow PostgreSQL queries with indexes and EXPLAIN
★ 2
postgres-migrations
Write safe, reversible, zero-downtime PostgreSQL schema migrations
★ 2