I am using .net core, I got a simple Interface something like :
public interface ITask
{
string TaskName { get; }
void Execute(TaskDetails item);
}
I got many classes which implement it, a sample class :
public class Task1Adapter : ITaskAdapter
{
public string TaskName => "MyTask...";
public void Execute(TaskDetails item)
{
var x = 1 + 1;
}
}
Then i got the DI section in which i add a line for each class -
services.AddSingleton<ITaskAdapter, Task1Adapter>();
My problem is that I got quite a lot of tasks and If possible I would rather avoid having 100+ AddSingleton lines.
Is there a way to somehow add all of the classes which implement that interface in one go without having to add AddSingleton logic for each new class I am adding?
Edit: Since I need to call different tasks / classes that implement the interface dynamically I used the following logic in run time -
private readonly IEnumerable<IFilesWorkerAdapter> _taskAdapter;
_taskAdapter.FirstOrDefault(e => e.TaskName== taskNameFromExternalSource).Execute();