Static Factory doesn't work on registered types

Jun 16, 2008 at 9:58 AM
Edited Jun 16, 2008 at 9:58 AM
Hi,

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();
parent.AddNewExtension<StaticFactoryExtension>();
parent.Configure<IStaticFactoryConfiguration>()
   .RegisterFactory<IUnityContainer>(p => p.CreateChildContainer())
   .RegisterFactory<IUnityContainer>("UnityTest", p => p.CreateChildContainer());
parent
   .RegisterType<IUnityContainer, UnityContainer>()
   .RegisterType<IUnityContainer, UnityContainer>("UnityTest");
IUnityContainer firstChild = parent.Resolve<IUnityContainer>();              // firstChild.Parent == null IUnityContainer secondChild = parent.Resolve<IUnityContainer>("UnityTest");  // secondChild.Parent == null
parent.Configure<IStaticFactoryConfiguration>()
   .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
Greetings
Jun 16, 2008 at 6: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.