Registration to child container

Feb 20, 2012 at 12:03 PM
Edited Feb 20, 2012 at 12:24 PM


Consider code:

var container = new UnityContainer();
container.RegisterType<ITest, Test>();
var childContainer = container.CreateChildContainer();
childContainer.RegisterType(typeof(ITest), new InjectionFactory((x) => new Test2()));
var result = childContainer.Resolve<ITest>();

I expected to get instance of Test2 from child container instead I got Test. Can you explain why?

The other test I created works as I expected:

var container = new UnityContainer();
container.RegisterType<ITest, Test>();
var childContainer = container.CreateChildContainer();
childContainer.RegisterType<ITest, Test2>();
var result = childContainer.Resolve<ITest>(); Console.WriteLine(result.GetType().FullName);


Feb 20, 2012 at 12:51 PM

var container = new UnityContainer();
var child = container.CreateChildContainer();
container.RegisterType<ITest, Test>();
child.RegisterType<ITest, Test2>(new InjectionFactory(c => new Test2()));
var sut = child.Resolve<ITest>();
Assert.IsInstanceOfType(sut, typeof(Test2));

If you provide source and destination type for the registration it works fine.

Feb 20, 2012 at 1:23 PM

Thanks, this solution works for me, but isn't that odd that this second type is required? What if I wanted dynamically determine implementation type to use?

Feb 20, 2012 at 1:27 PM

You could also use the non-generic overload of RegisterType

child.RegisterType(typeof(ITest), typeof(Test2), new InjectionFactory(c => new Test2()));
But if you determine the implementation Type dynamically, why would you create it yourself via a factory and not let Unity decide how best to create an instance of your implementation?

Feb 20, 2012 at 1:42 PM

I would. This is only sample code. I write integration layer with some old code that provides me with type factory that I'm supposed to use in certain situation. By understanding it I can determine the type that will be returned form it.

But what if I couldn't. Or if decision would be made in runtime base on some application state what then?

Feb 20, 2012 at 2:01 PM
Edited Feb 20, 2012 at 2:07 PM

OK, now I get your point. You don't know which Type your factory will create in the end. If you change the registration to


child.RegisterType<ITest, ITest>(new InjectionFactory(c => new Test2));

Identical source and destination types will also solve your problem. It has something to do with the way Unity handles type mappings and build keys.

Feb 20, 2012 at 3:44 PM

Thanks that is what I needed to know.

Feb 20, 2012 at 9:18 PM

Yeah, this is an unfortunate corner case due to the internals of the design.

Basically, all registrations except type mappings are keyed off concrete types, not interfaces. So when you do what you did in the first post, Unity will:

1) Look up type mapping: ITest -> Test

2) Look up factory for type Test

It doesn't matter that you've got a factory registered for ITest in the child container, because by the time you get to that stage in the creation pipeline, the container's no longer looking for ITest.

That's why doing RegisterType<ITest, ITest> works - it finds a type mapping ITest -> ITest, and then goes looking for a factory for ITest.