I am using Microsoft's Unity for dependency injection and I want to do something like this:
IDataContext context = _unityContainer.Resolve();
var repositoryA = _unityContainer.Resolve(context); //Same instance of context
var repositoryB = _unityContainer.Resolve(context); //Same instance of context
IDataContext context2 = _unityContainer.Resolve(); //New instance
var repositoryA2 = _unityContainer.Resolve(context2);
RepositoryA and RepositoryB both have a constructor that takes an IDataContext parameter, and I want Unity to initialize the repository with the context that I pass it. Also note that IDataContext is not registered with Unity (I don't want 3 instances of IDataContext).
解决方案
As of today they have added this functionality:
It’s in the latest drop here:
Discussion on it here:
Example:
container.Resolve(new ParameterOverrides { { "name", "bar" }, { "address", 42 } });"