lundi 13 mars 2023

Dependency injection of One interface multiple implementations

I have following situation:

IMachineService with method CreateMachine() and I have service for every kind of machine like MachineServiceTypeA createMachine() and so on...

and the same from other side I have interface for all types of machines like IMachine with method Start() and Stop()

I am thinking this way - I have to add Factory to class with Services and Factory to class Machines to be able to inject them properly with dependency injection.

I will have something like this -

services.AddTransient<IMachine, MachineA>();
services.AddTransient<IMachine, Machineb>();
services.AddTransient<IMachineFactory, MachineFactory>();
services.AddScoped<IMachineService, MachineAService>();
services.AddScoped<IMachineService, MachineBService>();
services.AddTransient<IMachineServiceFactory, MachineServiceFactory>();

This is my initial idea, but I don't thing it is good. Could you suggest me some options please, I want to be able to create for example 5 machines of type A and 3 machines of type B all of them in same collection and to keep code simple.

Aucun commentaire:

Enregistrer un commentaire