Bird
0
0

How can you combine the Repository pattern with Dependency Injection to swap database implementations in NestJS without changing service code?

hard📝 Application Q9 of 15
NestJS - Database with TypeORM
How can you combine the Repository pattern with Dependency Injection to swap database implementations in NestJS without changing service code?
ACreate repositories inside controllers directly
BHardcode repository classes inside services
CUse global variables to hold repository instances
DDefine an interface for repository methods and inject implementations via tokens
Step-by-Step Solution
Solution:
  1. Step 1: Understand Dependency Injection benefits

    DI allows swapping implementations by injecting different classes that follow the same interface.
  2. Step 2: Apply interface and tokens

    Defining an interface for repository methods and injecting implementations via tokens lets you change database layers without modifying services.
  3. Final Answer:

    Define an interface for repository methods and inject implementations via tokens -> Option D
  4. Quick Check:

    Use interfaces and DI tokens to swap repositories [OK]
Quick Trick: Use interfaces and DI tokens to swap repository implementations [OK]
Common Mistakes:
  • Hardcoding repository classes in services
  • Using global variables for repositories
  • Creating repositories in controllers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes