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:
public class Consumer
{
public Consumer(Func<ArgumentType, IRepository> repositoryFactory)
{
_repositoryFactory = repositoryFactory;
}
public void CallRepository()
{
ArgumentType arg = (x == y) ? z : a;
var repository = _repositoryFactory(arg);
repository.GetById(...);
}
}
In the configuration for structure map, you can configure the func:
For<Func<ArgumentType, IRepository>>().Use( arg => new Repository(arg));
I think something like this should work:
var arg = (x == y) ? z : a;
ObjectFactory.With("argumentName").EqualTo(arg).GetInstance<IRepository>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With