What's the difference between IUnityContainer.

2019-08-02 19:38发布

问题:

Some code I'm updating uses Unity which is a bit new to me, although I get the general principles.

One interface is registered like this:

          _container.RegisterType<ISomething, Something>(
            new ContainerControlledLifetimeManager(),
            new InjectionConstructor(
                new ResolvedParameter<ITypeA>(),
                new ResolvedParameter<ITypeB>(),
                _container.Resolve<ITypeC>()
            )
          );

I'm confused the distinction between new ResolvedParameter<ITypeB>() and _container.Resolve<ITypeC>() - can someone make it clearer what the difference is and when each might be used/preferred?

回答1:

Everytime you resolve ISomething, new ITypeA and new ITypeB implementations are instantiated (assuming they are not registered as singletons) and passed to constructor. but for ITypeC you have the exact instance created when you called _container.Resolve<ITypeC>().

see related question: Injecting new constructor parameters every time a type is resolved using unity