Bird
0
0

Why do dependency injection frameworks prefer constructor injection over property or method injection?

hard📝 Conceptual Q10 of 15
LLD - Advanced LLD Concepts
Why do dependency injection frameworks prefer constructor injection over property or method injection?
AMethod injection is the only way to inject dependencies
BConstructor injection ensures dependencies are provided when object is created, improving immutability and testability
CProperty injection is faster and preferred for all cases
DConstructor injection hides dependencies making code harder to read
Step-by-Step Solution
Solution:
  1. Step 1: Understand injection types

    Constructor injection provides dependencies at creation, making objects immutable and easier to test.
  2. Step 2: Compare benefits

    Property and method injection can allow changing dependencies later, reducing safety.
  3. Final Answer:

    Constructor injection ensures dependencies are provided when object is created, improving immutability and testability -> Option B
  4. Quick Check:

    Constructor injection benefits = Immutability and testability [OK]
Quick Trick: Constructor injection improves safety and testing [OK]
Common Mistakes:
  • Believing property injection is always better
  • Thinking method injection is only way
  • Assuming constructor injection hides dependencies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes