jeudi 20 janvier 2022

Each service in a IEnumerable

I have a service that will be available in my app via dependency injection, but this service itself will have a subservice injected into it, which can be from varying services that inherit from ISubService.

Start -> Will have multiple Services of type IService -> Each IService will have a different service that inherits from ISubService.

It doesn't have to inherit, I just thought this would be the first step to solve my problem.

Interfaces

interface IService
interface ISubService
interface ISubService1 : ISubService
interface SubService2 : ISubService

Service that has to have multiple instances, with a different subservice each time

public class Service: IService
{
   public Service(SubService ISubService, ... ) { ... }
}

My registration of services.

services.AddTransient<IIService, Service>(); // This service should have SubService 1
services.AddTransient<ISubService1, SubService1>();
services.AddTransient<IIService, Service>(); // This service should have SubService 2
services.AddTransient<ISubService2, SubService2>();

Class that will have multiple services, with different subservices

public class Start
        public Start(IEnumerable<IService> services, ... ) { ... }

I have tried adding a parameter to the construction when adding the transient, but this does not work as I have other parameters via dependency injection which are expected.

Thank you for your help in advance.

Aucun commentaire:

Enregistrer un commentaire