Static Factory doesn't work on registered types

Jun 16, 2008 at 8:58 AM
Edited Jun 16, 2008 at 8:58 AM

if any type is registered at the unity container, through configuration or code, the static factory doesn't work for that registered type. Only if you specify a static factory for the mapped type, the static factory will work.

Following codesample:
IUnityContainer parent = new UnityContainer();
   .RegisterFactory<IUnityContainer>(p => p.CreateChildContainer())
   .RegisterFactory<IUnityContainer>("UnityTest", p => p.CreateChildContainer());
   .RegisterType<IUnityContainer, UnityContainer>()
   .RegisterType<IUnityContainer, UnityContainer>("UnityTest");
IUnityContainer firstChild = parent.Resolve<IUnityContainer>();              // firstChild.Parent == null IUnityContainer secondChild = parent.Resolve<IUnityContainer>("UnityTest");  // secondChild.Parent == null
   .RegisterFactory<UnityContainer>(p => p.CreateChildContainer())
   .RegisterFactory<UnityContainer>("UnityTest", p => p.CreateChildContainer());
firstChild = parent.Resolve<IUnityContainer>(); // firstChild.Parent == parent
secondChild = parent.Resolve<IUnityContainer>("UnityTest"); // secondChild.Parent == parent
Jun 16, 2008 at 5:20 PM
That is the expected behavior. Type mapping happens before the build plan (which is what the static factory hooks up). By the time the delegate runs, the type has already run through the type mapping and the IUnityContainer has been replaced with UnityContainer.