Photo by Diana Polekhina on Unsplash
.NET Dependency Injection
Utilizing .NET Dependency Injection for multiple implementations of a single interface type.
In .NET, the Dependency Injection container offers remarkable flexibility. Ever wondered why it allows multiple implementations for a single interface when it only resolves it with the last one registered at runtime?
Here's a practical scenario: Imagine building an e-commerce notification system with email, SMS, and push providers. You want notifications sent through all channels while working with the abstract type, INotificationProvider.
To make this happen, inject the interface as an IEnumerable<InterfaceType> in your class constructor. The service provider resolves all registered implementations for the service type.
Remember, using any other collection type will lead to an invalid operation exception.