How do you manage Multiple Implementations of the same interface in DI?
Updated Apr 28, 2026
Short answer
When multiple classes implement the same interface, you must configure the container to resolve the correct one.
Deep explanation
This is done using Keyed/Named registrations (e.g., requesting 'Email' vs 'SMS'), relying on IEnumerable<T> to inject all implementations, or using a Factory pattern that takes a parameter and resolves the specific type at runtime.
Real-world example
A validation engine that injects IEnumerable<IValidationRule>. It loops through all registered rules and executes them dynamically.
Common mistakes
- Relying on the order of registration in the container to magically resolve the 'last registered' service.
Follow-up questions
- What is a Keyed Service?