Bird
Raised Fist0

Identify the error in the following code snippet using a dependency injection framework:

medium๐Ÿ“ Analysis Q14 of Q15
LLD - Advanced LLD Concepts
Identify the error in the following code snippet using a dependency injection framework:
class ServiceB {}

const serviceB = injector.get(ServiceB);
injector.register(ServiceB);
AServiceB is registered after trying to get it
BServiceB class is missing a constructor
Cinjector.get should be injector.fetch
Dinjector.register should be called twice
Step-by-Step Solution
Solution:
  1. Step 1: Check the order of registration and retrieval

    The code tries to get ServiceB from the injector before registering it, which causes an error because the injector doesn't know about ServiceB yet.
  2. Step 2: Confirm correct usage order

    Services must be registered before they can be retrieved from the injector.
  3. Final Answer:

    ServiceB is registered after trying to get it -> Option A
  4. Quick Check:

    Register before get = correct order [OK]
Quick Trick: Always register before getting a service [OK]
Common Mistakes:
MISTAKES
  • Trying to get service before registration
  • Confusing method names like get vs fetch
  • Thinking constructor is required for registration

Want More Practice?

15+ quiz questions ยท All difficulty levels ยท Free

Free Signup - Practice All Questions
More LLD Quizzes