Can I pass constructor parameters to Unity's Resolve() method?

Posted by NotDan on Stack Overflow See other posts from Stack Overflow or by NotDan
Published on 2009-04-24T18:18:16Z Indexed on 2010/04/28 19:07 UTC
Read the original article Hit count: 687

Filed under:
|
|
|

I am using Microsoft's Unity for dependency injection and I want to do something like this:

  IDataContext context = _unityContainer.Resolve<IDataContext>();
  var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context
  var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context

  IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance
  var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(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 dont want 3 instances of IDataContext).

© Stack Overflow or respective owner

Related posts about c#

Related posts about dependency-injection