There are cases in my app where my repositories require passing different arguments to constructors of the concrete types. I want to be able to do something like this:
var arg = (x == y) ? z : a;
ObjectFactory.GetInstance<IRepository>(arg);
Where the argument can only be constructed at the time of creating the Repository instance depending on some condition.
How can this be done?
As @Steven also says, I think that if possible you should let the class that needs the dependency (that has a dynamic argument) take a factory as a parameter so that you can control the creation from the consumer.
With structure map there is built in support for this so that you don’t have build a factory class.
Let the consumer take a Func as a ctor argument and create the repository (dependency) by invoking the Func with the argument:
In the configuration for structure map, you can configure the func: