Container Hierarchy

Apr 4, 2008 at 2:52 PM

public interface IFoo {}
public class ParentFoo : IFoo {}
public class ChildFoo : IFoo {}
public interface IBar {}
public class Bar : IBar
  [Dependency] IFoo Foo { get; set; }

What if I define a container hiearchy as follows:
IUnityContainer childContainer = new UnityContainer()
  .RegisterType<IFoo, ParentFoo>()
  .RegisterType<IBar, Bar>()
    .RegisterType<IFoo, ChildFoo>();

and now resolve IBar
IBar bar = childContainer.Resolve<IBar>();

Which IFoo should the IBar be build with? ParentFoo or ChildFoo? Right now it is ChildFoo.

Does this make sense or should the Bar be built with ParentFoo as ChildFoo is "out-of-scope" to the Bar declaration?
Apr 4, 2008 at 5:30 PM
ChildFoo. There's no such thing as "scope", what matters is whatever set of registrations that are active in the container you're building with.

If you wanted ParentFoo you should build IBar via the parent container.