Request dependent concrete factory in .NET 6.0 Api

87 Views Asked by At

Lets say I have two factories that are producing very similar sets of products.
This is a typical case where I use an abstract factory in my app:

enter image description here

This is a very simplified version of my scenario.

Now the thing is, that my factories have many dependencies. I would like to add those dependencies via dependency injection to the factory.

So I am creating my factories also via DI like this:

services.AddScoped<IAbstractFactory, ConcreteFactory1>();    
services.AddScoped<IAbstractFactory, ConcreteFactory2>();   

Of course this must fail. The DI engine does not know which concrete factory to inject.

This depends on a parameter of my API request and can change for each request.

So for some requests my DI should deliver ConcreteFactory1, and for other requests ConcreteFactory2.

How can I archive this?

0

There are 0 best solutions below