コード例 #1
0
        public void named_registrations_should_be_isolated_on_child_containers()
        {
            var container = new Container(_ => { }); //Parent Container doesnt contain the named instance

            var child1      = container.CreateChildContainer();
            var child1Thing = Thing1.Build("child1");

            child1.Configure(_ => _.For <IThing>().Add(child1Thing).Named("A"));

            child1.GetInstance <IThing>("A").ShouldBeTheSameAs(child1Thing);

            Debug.WriteLine(container.WhatDoIHave());

            var child2      = container.CreateChildContainer();
            var child2Thing = Thing1.Build("child2");

            child2.Configure(_ => _.For <IThing>().Add(child2Thing).Named("A"));

            child2.GetInstance <IThing>("A")
            .ShouldBeTheSameAs(child2Thing);

            child2.TryGetInstance <IThing>("A")
            .ShouldBeTheSameAs(child2Thing);

            child2.TryGetInstance <IThing>("A")
            .ShouldBeTheSameAs(child2Thing);

            container.TryGetInstance <IThing>("A").ShouldBeNull();
        }
コード例 #2
0
        public void named_registrations_should_be_isolated_on_nested_containers()
        {
            var container = new Container(_ => { }); //Parent Container doesnt contain the named instance

            var nested1      = container.GetNestedContainer();
            var nested1Thing = Thing1.Build("child1");

            nested1.Configure(_ => _.For <IThing>().Add(nested1Thing).Named("A"));
            var containerNested1Thing = nested1.GetInstance <IThing>("A");

            containerNested1Thing.ShouldBe(nested1Thing);

            var nested2      = container.GetNestedContainer();
            var nested2Thing = Thing1.Build("child2");

            nested2.Configure(_ => _.For <IThing>().Add(nested2Thing).Named("A"));
            var nested2ContainerThing = nested2.TryGetInstance <IThing>("A");

            nested2ContainerThing.ShouldBe(nested2Thing);

            var parentContainerThing = container.TryGetInstance <IThing>("A");

            parentContainerThing.ShouldBeNull();
        }