juniorDependency Injection
What is Method Injection?
Updated Apr 28, 2026
Short answer
Method injection passes a dependency directly into a specific method rather than the class constructor.
Deep explanation
This is useful when a dependency is only needed by one specific method, or when the dependency changes on every single method call (like context parameters). It prevents bloating the constructor with dependencies that are rarely used.
Real-world example
Passing an HTTP Request context object directly into a specific controller action method rather than the controller's constructor.
Common mistakes
- Passing global services (like logging or database connections) via method injection instead of the constructor.
Follow-up questions
- When would you prefer Method Injection over Constructor Injection?