What's the difference between IUnityContainer.Resolve() and ResolvedParameter when configuring constructor arguments?

ぃ、小莉子 提交于 2019-12-11 04:13:36

问题


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



来源:https://stackoverflow.com/questions/42654950/whats-the-difference-between-iunitycontainer-resolve-and-resolvedparameter-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!