Skip to content

Refactor: Convert Backend Services to Dependency Injection Pattern #485

Description

@Smartdevs17

Context

Backend services are instantiated with manual imports and tight coupling, making testing difficult and refactoring risky.

Current Limitation/Problem

Services import dependencies directly (e.g., import prisma from "../prisma"). Unit testing requires mocking entire modules. Service wiring changes require modifying every file.

Expected Outcome

A lightweight dependency injection container where services declare their dependencies via constructor injection. Services become testable with mock injection.

Acceptance Criteria

  • Implement DI container (tsyringe or awilix)
  • Register all services in container with lifecycle management (singleton, scoped, transient)
  • Services receive dependencies via constructor injection
  • Controllers and middleware resolve services from container
  • Unit tests inject mock dependencies
  • Feature flag: opt-in old vs new injection pattern during migration
  • DI container configuration validation on startup
  • Performance: injection overhead <1ms per resolution
  • Migration guide and codemod for service files

Technical Scope

  • backend/src/di/ - DI container configuration
  • backend/src/di/container.ts - container setup
  • Registration of existing services (PaymentService, WalletService, etc.)
  • Refactor service constructors to accept dependencies
  • backend/src/di/modules/ - per-domain module registrations
  • Edge cases: circular dependencies, async initialization, request-scoped services

Metadata

Metadata

Labels

200-points200 point issueStellar WaveIssues in the Stellar wave programdrips-waveIssues in the Drips Wave programhighHigh complexity issue

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions