I’m trying to find a method of passing a constructor argument to the constructors of child classes.
These objects are immutable so I’d prefer to use constructor arguments.
The issue I have encountered is that ConstructorArgument does not inherit to child instantiations and the following statements are not interchangeable:
_parsingProcessor = _kernel.Get<IParsingProcessor>(new ConstructorArgument("dataFilePath", dataFilePath);
and
_parsingProcessor = _kernel.Get<IParsingProcessor>(new Parameter("dataFilePath", dataFilePath, true);
So, how can get an inheritable ConstructorArgument and when does it makes sense, if ever, to new the Parameter class?
Yes, you can do this, but it’s probably not what you really want. If the container is not actually responsible for instantiating its own dependencies, then its dependencies probably shouldn’t be sharing its constructor arguments – it just doesn’t make sense.
I’m pretty sure I know what you’re trying to do, and the recommended approach is to create a unique binding specifically for your one container, and use the
WhenInjectedIntoconditional binding syntax, as in the example below:This is the class that takes a constructor argument which we want to modify, depending on who is asking for an
IHello. Let’s say it’s this boring container class:Now, here’s how you do up the bindings:
Basically all this binding is doing is saying the
nameshould be “Jim” unless it’s being requested byHello, which in this case it is, so instead it will get the name “Bob”.If you are absolutely certain that you truly want cascading behaviour and understand that this is very dangerous and brittle, you can cheat using a method binding. Assuming that we’ve now added a
nameargument to theMyAppclass for some unspecified purpose, the binding would be:Please, please, make sure you are positive that this is what you want before doing it. It looks easy but it is also very likely to break during a simple refactoring, and 95% of the “customized dependency” scenarios I’ve seen can be addressed using the
WhenInjectedIntobinding instead.