You need to realize that configuration you're using deep down is the same kind of dependency as IMyRepository
. You inject repository via abstract contract (interface). Why the same isn't done for configuration? Quick and naive solution would be to create IConfiguration
interface and implement it by simply delegating calls to ConfigurationManager
. Your constructor would look like this:
public class MyController(IMyRepository repository, IConfiguration configuration)
What does that tell us? Well, not much unfortunately. Fact that controller requires configuration is very vague. Real question is, what's the exact parameter from configuration it needs? You need to identify that very parameter and that's the real dependency you want to inject. Consider:
MyController(IMyRepository repository, IConfiguration configuration)
MyController(IMyRepository repository, int serviceCallTimeoutSeconds)
MyController(IMyRepository repository, string serviceAccessKey)
Which one communicates its purpose better? The more single-feature oriented your controller is, the less parameters it should use. Your problem might not be where you think it is.