In your example, you provide the serviceProvider runtime currentDependency . Application components should not require data at run time, as described here . The solution is to reorganize your design as described in this article.
About additional arguments:
The fact that some DI containers support optional arguments prevents them from using them. In fact, the arguments of the injection constructor should never be extra.
As explained in this article:
An optional dependency implies that the reference to the dependency will be null if it is not provided. Null references complicate the code because they require specific logic for the null case. Instead of passing a null reference, the caller can insert an implementation without any behavior, that is, an implementation of a null object template.
If not, what is a clean solution to this kind of problem?
As indicated, the Null Object pattern is a solution for this even when using a DI container that actually supports optional constructor dependencies.
source share