Design class with different dependency requirements based on configuration

You’ve not abstracted this correctly.

Inject a dependency (IDependency) which has a method which returns a boolean. Your service does not need to know how the dependency works, it just knows that it will receive a boolean.

Based on your configuration, inject a different concrete class as the dependency.

  • If you need to call a http client, inject HttpClientDependency : IDependency which actually calls an external service and returns a meaningful boolean value.
  • If you don’t need to call a http client, inject a ReturnsTrueDependency : IDependency which does nothing more than return true;

Regardless of which dependency is injected, your service class works the same way, which simplifies its logic and keeps it readable.

Note also that how you choose the right concrete class to inject is up to you. You could use a factory. Or you could wire your DI container differently at startup. This depends on what makes the most sense for your particular scenario.

These names are used as oversimplified examples. Better names should be used in your code.