My requirement is very similar to the question asked here
- With.Parameters.ConstructorArgument with ninject 2.0
This may be a dumb question -- But, isn't the approach of sending parameters to the constructor during the call to Get<IMyType>
goes against the philosophy of the de-coupling (which, I believe is the major motivation for DI) itself?
This assumes that the user of IMyType
has knowledge of the constructor of the specific implemenation of IMyType
.
Since there is no way to declare the constructor arguments within the interface, it is possible that another implementation can be added in the future, which takes different set of parameters in the constructor & you bind to that implementation. Again, going by the philosophy of de-coupling, the code that does Bind<IMyType>.To<ADifferentImpl>()
should be able to work with any module that was written earlier, which gets the implementation object from Get<IMyType>
.
I have very similar requirement - where I don't know the values of the constructor parameters at the time of binding, but only at the time of accessing. I thought that the best approach to follow in the given situation is to go with Adrian Grigore's answer. Please let me know if there is any other approach.