I have a class with a constructor that looks like the following:
public BatchService(IRepository repository, ILogger logger, string user)
I
You can use ParameterOverride
:
BatchService batchService =
DIContainer.Resolve<BatchService>(new ParameterOverride("user", valueForUser));
Please don't abuse Unity as a ServiceLocator.
If you want to create objects that need runtime parameters use a factory. You can even drop the act of implementing that factory by either using the Unity version of Typed Factories or let Unity generate factory delegates for you.
First of all it's not good idea mix ILogger
with business logic. You can create ILogger directly in BatchService
or resolve thru [Dependency]
attribute. DI it's not panacea, business object creation shouldn't depend from ILogger
Use new InjectionParameter<string>("user")
Take a look please Registering Injected Parameter and Property Values for more details