Inject an IEnumerable of interface in controller using Dependency Injection

I want to resolve the dependency for the IEnumerable collections of the multiple class inheriting the Interface on the controller.

I want to resolve the following dependency during the application startup:

var notificationStrategy = new NotificationStrategy(
new INotificationService[]
{
    new TextNotificationService(), // <-- inject any dependencies here
    new EmailNotificationService()      // <-- inject any dependencies here
});

NotificationStragey

public class NotificationStrategy : INotificatonStrategy
{
    private readonly IEnumerable<INotificationService> notificationServices;

    public NotificationStrategy(IEnumerable<INotificationService> notificationServices)
    {
        this.notificationServices = notificationServices ?? throw new ArgumentNullException(nameof(notificationServices));
    }
}

What is the best way of dependency injection of IEnumerable types of objects in the asp.net core without using any external dependency or library?



Read more here: https://stackoverflow.com/questions/67940785/inject-an-ienumerable-of-interface-in-controller-using-dependency-injection

Content Attribution

This content was originally published by Aakash at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: