コード例 #1
0
        public void when_registering_POCO_message_handler_facility_should_correctly_subscribe_messages()
        {
            var container = new PuzzleContainer();

            container.AddFacility <SubscribeToMessageFacility>();
            container.Register(EntryBuilder.For <IMessageBroker>().UsingInstance(new MessageBroker(new NullDispatcher())));

            container.Register(EntryBuilder.For <IHandleMessage <AMessage> >().ImplementedBy <AMessageHandler>());

            var broker  = container.Resolve <IMessageBroker>();
            var handler = (AMessageHandler)container.Resolve <IHandleMessage <AMessage> >();

            broker.Dispatch(this, new AMessage());

            Assert.IsTrue(handler.Invoked);
        }
コード例 #2
0
        public void puzzleContainer_register_entry_should_report_entry_as_registered()
        {
            var container = new PuzzleContainer();

            container.Register(EntryBuilder.For <Object>());
            var actual = container.IsRegistered <Object>();

            Assert.IsTrue(actual);
        }
コード例 #3
0
        public void puzzleContainer_register_entry_using_instance_should_resolve_the_given_instance_using_getService_as_singleton()
        {
            var container = new PuzzleContainer();

            container.Register(EntryBuilder.For <Object>().UsingInstance(new Object()));
            var expected = container.GetService(typeof(Object));
            var actual   = container.GetService(typeof(Object));

            Assert.AreEqual(expected, actual);
        }
コード例 #4
0
        public void puzzleContainer_register_entry_as_transient_should_resolve_instances_as_transient()
        {
            var container = new PuzzleContainer();

            container.Register(EntryBuilder.For <Object>().WithLifestyle(Lifestyle.Transient));
            var i1 = container.Resolve <Object>();
            var i2 = container.Resolve <Object>();

            Assert.AreNotEqual(i1, i2);
        }
コード例 #5
0
        public void puzzleContainer_resolveAll_non_generic_should_behave_as_expected()
        {
            var obj1 = new Object();
            var obj2 = new Object();

            var container = new PuzzleContainer();

            container.Register(
                EntryBuilder.For <Object>().UsingInstance(obj1)
                );

            container.Register(
                EntryBuilder.For <Object>().UsingInstance(obj2)
                );

            IEnumerable <Object> actual = container.ResolveAll(typeof(Object));

            Assert.IsTrue(actual.Contains(obj1));
            Assert.IsTrue(actual.Contains(obj2));
        }
コード例 #6
0
        public void puzzleContainer_register_entry_using_instance_should_resolve_the_given_instance()
        {
            var expected = new Object();

            var container = new PuzzleContainer();

            container.Register(EntryBuilder.For <Object>().UsingInstance(expected));
            var actual = container.Resolve <Object>();

            Assert.AreEqual(expected, actual);
        }
コード例 #7
0
        public void PuzzleContainer_resolve_using_type_with_static_type_initializer_should_not_fail()
        {
            var container = new PuzzleContainer();

            container.Register(
                EntryBuilder.For <TypeWithStaticCtor>()
                );

            var actual = container.Resolve <TypeWithStaticCtor>();

            Assert.IsNotNull(actual);
        }
コード例 #8
0
        public void PuzzleContainer_Resolve_using_overridable_types_should_resolve_overrider_and_not_overridden()
        {
            var obj1 = new Object();
            var obj2 = new Object();

            var sut = new PuzzleContainer();

            sut.Register(
                EntryBuilder.For <Object>()
                .UsingInstance(obj1)
                .Overridable()
                );

            sut.Register(
                EntryBuilder.For <Object>().UsingInstance(obj2)
                );

            var actual = sut.Resolve <Object>();

            Assert.AreEqual(obj2, actual);
        }
コード例 #9
0
        public void puzzleContainer_register_should_raise_componentRegistered_event_with_a_reference_to_the_registered_entry()
        {
            IContainerEntry expected = EntryBuilder.For <Object>();
            IContainerEntry actual   = null;

            var container = new PuzzleContainer();

            container.ComponentRegistered += (s, e) => actual = e.Entry;

            container.Register(expected);

            Assert.AreEqual(expected, actual);
        }
コード例 #10
0
        public void puzzleContainer_register_should_raise_componentRegistered_event()
        {
            var expected = 1;
            var actual   = 0;

            var container = new PuzzleContainer();

            container.ComponentRegistered += (s, e) => actual++;

            container.Register(EntryBuilder.For <Object>());

            Assert.AreEqual(expected, actual);
        }
コード例 #11
0
        public void puzzleContainer_should_resolve_type_with_dependency_on_array_of_registered_types()
        {
            var obj1 = new Object();
            var obj2 = new Object();

            var sut = new PuzzleContainer();

            sut.Register(
                EntryBuilder.For <Object>().UsingInstance(obj1)
                );

            sut.Register(
                EntryBuilder.For <Object>().UsingInstance(obj2)
                );

            sut.Register(
                EntryBuilder.For <TypeWithArrayDependency>()
                );

            var actual = sut.Resolve <TypeWithArrayDependency>();

            Assert.IsTrue(actual.IsResolved);
        }
コード例 #12
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding()
        {
            var entry = EntryBuilder.For <IFoo>()
                        .Forward <IBar>()
                        .ImplementedBy <TypeWithMultipleContracts>();

            var sut = new PuzzleContainer();

            sut.Register(entry);

            var actual = sut.IsRegistered <IBar>();

            Assert.IsTrue(actual);
        }
コード例 #13
0
        public void puzzleContainer_register_entries_using_valid_entry_should_resolve_the_given_entries()
        {
            var container = new PuzzleContainer();

            IContainerEntry e1 = EntryBuilder.For <ArgumentException>();
            IContainerEntry e2 = EntryBuilder.For <ArgumentNullException>();

            container.Register(new[] { e1, e2 });
            var obj1 = container.Resolve <ArgumentException>();
            var obj2 = container.Resolve <ArgumentNullException>();

            Assert.IsNotNull(obj1);
            Assert.IsNotNull(obj2);
        }
コード例 #14
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_in_singleton_types()
        {
            var entry = EntryBuilder.For <IFoo>()
                        .Forward <IBar>()
                        .ImplementedBy <TypeWithMultipleContracts>();

            var sut = new PuzzleContainer();

            sut.Register(entry);

            var iBar = sut.Resolve <IBar>();
            var iFoo = sut.Resolve <IFoo>();

            Assert.AreEqual(iBar, iFoo);
        }
コード例 #15
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_with_instances()
        {
            var entry = EntryBuilder.For <IFoo>()
                        .Forward <IBar>()
                        .UsingInstance(new TypeWithMultipleContracts());

            var sut = new PuzzleContainer();

            sut.Register(entry);

            var iBar = sut.Resolve <IBar>();
            var iFoo = sut.Resolve <IFoo>();

            Assert.AreEqual(iBar, iFoo);
        }
コード例 #16
0
        public void PuzzleContainer_at_dispose_should_dispose_singleton_references()
        {
            var entry = EntryBuilder.For <IFoo>()
                        .ImplementedBy <DisposableFoo>()
                        .WithLifestyle(Lifestyle.Singleton);

            var sut = new PuzzleContainer();

            sut.Register(entry);

            var iFoo = (DisposableFoo)sut.Resolve <IFoo>();

            sut.Dispose();

            Assert.IsTrue(iFoo.Disposed);
        }
コード例 #17
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_with_factory_method()
        {
            var entry = EntryBuilder.For <IFoo>()
                        .Forward <IBar>()
                        .UsingFactory(() => new TypeWithMultipleContracts())
                        .WithLifestyle(Lifestyle.Singleton);

            var sut = new PuzzleContainer();

            sut.Register(entry);

            var iBar = sut.Resolve <IBar>();
            var iFoo = sut.Resolve <IFoo>();

            Assert.AreEqual(iBar, iFoo);
        }
コード例 #18
0
        public void puzzleContainer_register_entry_with_factory_as_singleton_should_call_factory_once()
        {
            var expected  = 1;
            var actual    = 0;
            var container = new PuzzleContainer();

            container.Register
            (
                EntryBuilder.For <Object>()
                .UsingFactory(() =>
            {
                actual++;
                return(new Object());
            })
            );

            container.Resolve <Object>();
            container.Resolve <Object>();

            Assert.AreEqual(expected, actual);
        }
コード例 #19
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_in_transient_types()
        {
            var entry = EntryBuilder.For <IFoo>()
                        .Forward <IBar>()
                        .ImplementedBy <TypeWithMultipleContracts>()
                        .WithLifestyle(Lifestyle.Transient);

            var sut = new PuzzleContainer();

            sut.Register(entry);

            var iBar = sut.Resolve <IBar>();
            var iFoo = sut.Resolve <IFoo>();

            Assert.IsNotNull(iBar);
            Assert.IsNotNull(iFoo);

            Assert.AreNotEqual(iBar, iFoo);

            Assert.IsTrue(iBar.GetType().Is <TypeWithMultipleContracts>());
            Assert.IsTrue(iFoo.GetType().Is <TypeWithMultipleContracts>());
        }
コード例 #20
0
        public void puzzleContainer_register_entry_with_factory_as_transient_should_resolve_instances_as_transient()
        {
            var expected  = 2;
            var actual    = 0;
            var container = new PuzzleContainer();

            container.Register
            (
                EntryBuilder.For <Object>()
                .UsingFactory(() =>
            {
                actual++;
                return(new Object());
            })
                .WithLifestyle(Lifestyle.Transient)
            );

            container.Resolve <Object>();
            container.Resolve <Object>();

            Assert.AreEqual(expected, actual);
        }
コード例 #21
0
        public void puzzleContainer_register_should_raise_componentRegistered_event()
        {
            var expected = 1;
            var actual = 0;

            var container = new PuzzleContainer();
            container.ComponentRegistered += ( s, e ) => actual++;

            container.Register( EntryBuilder.For<Object>() );

            Assert.AreEqual( expected, actual );
        }
コード例 #22
0
        public void puzzleContainer_register_entry_using_instance_should_resolve_the_given_instance_using_getService_as_singleton()
        {
            var container = new PuzzleContainer();

            container.Register( EntryBuilder.For<Object>().UsingInstance( new Object() ) );
            var expected = container.GetService( typeof( Object ) );
            var actual = container.GetService( typeof( Object ) );

            Assert.AreEqual( expected, actual );
        }
コード例 #23
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_with_instances()
        {
            var entry = EntryBuilder.For<IFoo>()
                .Forward<IBar>()
                .UsingInstance( new TypeWithMultipleContracts() );

            var sut = new PuzzleContainer();
            sut.Register( entry );

            var iBar = sut.Resolve<IBar>();
            var iFoo = sut.Resolve<IFoo>();

            Assert.AreEqual( iBar, iFoo );
        }
コード例 #24
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_in_transient_types()
        {
            var entry = EntryBuilder.For<IFoo>()
                .Forward<IBar>()
                .ImplementedBy<TypeWithMultipleContracts>()
                .WithLifestyle( Lifestyle.Transient );

            var sut = new PuzzleContainer();
            sut.Register( entry );

            var iBar = sut.Resolve<IBar>();
            var iFoo = sut.Resolve<IFoo>();

            Assert.IsNotNull( iBar );
            Assert.IsNotNull( iFoo );

            Assert.AreNotEqual(iBar, iFoo);

            Assert.IsTrue( iBar.GetType().Is<TypeWithMultipleContracts>() );
            Assert.IsTrue( iFoo.GetType().Is<TypeWithMultipleContracts>() );
        }
コード例 #25
0
        public void PuzzleContainer_Resolve_using_overridable_types_should_resolve_even_all_are_marked_as_overridable()
        {
            var obj1 = new Object();
            var obj2 = new Object();

            var sut = new PuzzleContainer();

            sut.Register(
                EntryBuilder.For<Object>()
                    .UsingInstance( obj1 )
                    .Overridable()
            );

            sut.Register(
                EntryBuilder.For<Object>()
                    .UsingInstance( obj2 )
                    .Overridable()
            );

            var actual = sut.Resolve<Object>();

            Assert.AreEqual( obj1, actual );
        }
コード例 #26
0
        public void PuzzleContainer_resolve_using_type_with_static_type_initializer_should_not_fail()
        {
            var container = new PuzzleContainer();
            container.Register(
                EntryBuilder.For<TypeWithStaticCtor>()
            );

            var actual = container.Resolve<TypeWithStaticCtor>();

            Assert.IsNotNull( actual );
        }
コード例 #27
0
        public void puzzleContainer_register_entries_using_valid_entry_should_resolve_the_given_entries()
        {
            var container = new PuzzleContainer();

            IContainerEntry e1 = EntryBuilder.For<ArgumentException>();
            IContainerEntry e2 = EntryBuilder.For<ArgumentNullException>();

            container.Register( new[] { e1, e2 } );
            var obj1 = container.Resolve<ArgumentException>();
            var obj2 = container.Resolve<ArgumentNullException>();

            Assert.IsNotNull( obj1 );
            Assert.IsNotNull( obj2 );
        }
コード例 #28
0
 public void puzzleContainer_register_entries_using_null_reference_should_raise_ArgumentNullException()
 {
     var container = new PuzzleContainer();
     container.Register( ( IEnumerable<IContainerEntry> )null );
 }
コード例 #29
0
        public void puzzleContainer_register_entry_using_valid_entry_should_not_fail()
        {
            var container = new PuzzleContainer();

            container.Register(EntryBuilder.For <string>());
        }
コード例 #30
0
        public void PuzzleContainer_at_dispose_should_dispose_singleton_references()
        {
            var entry = EntryBuilder.For<IFoo>()
                .ImplementedBy<DisposableFoo>()
                .WithLifestyle(Lifestyle.Singleton);

            var sut = new PuzzleContainer();
            sut.Register(entry);

            var iFoo = (DisposableFoo)sut.Resolve<IFoo>();

            sut.Dispose();

            Assert.IsTrue(iFoo.Disposed);
        }
コード例 #31
0
        public void puzzleContainer_register_entries_using_null_reference_should_raise_ArgumentNullException()
        {
            var container = new PuzzleContainer();

            container.Register((IEnumerable <IContainerEntry>)null);
        }
コード例 #32
0
        public void puzzleContainer_register_should_raise_componentRegistered_event_with_a_reference_to_the_registered_entry()
        {
            IContainerEntry expected = EntryBuilder.For<Object>();
            IContainerEntry actual = null;

            var container = new PuzzleContainer();
            container.ComponentRegistered += ( s, e ) => actual = e.Entry;

            container.Register( expected );

            Assert.AreEqual( expected, actual );
        }
コード例 #33
0
        public void puzzleContainer_resolveAll_non_generic_should_behave_as_expected()
        {
            var obj1 = new Object();
            var obj2 = new Object();

            var container = new PuzzleContainer();
            container.Register(
                EntryBuilder.For<Object>().UsingInstance( obj1 )
            );

            container.Register(
                EntryBuilder.For<Object>().UsingInstance( obj2 )
            );

            IEnumerable<Object> actual = container.ResolveAll( typeof( Object ) );

            Assert.IsTrue( actual.Contains( obj1 ) );
            Assert.IsTrue( actual.Contains( obj2 ) );
        }
コード例 #34
0
        public void puzzleContainer_register_entry_using_instance_should_resolve_the_given_instance()
        {
            var expected = new Object();

            var container = new PuzzleContainer();

            container.Register( EntryBuilder.For<Object>().UsingInstance( expected ) );
            var actual = container.Resolve<Object>();

            Assert.AreEqual( expected, actual );
        }
コード例 #35
0
        public void puzzleContainer_should_resolve_type_with_dependency_on_array_of_registered_types()
        {
            var obj1 = new Object();
            var obj2 = new Object();

            var sut = new PuzzleContainer();

            sut.Register(
                EntryBuilder.For<Object>().UsingInstance( obj1 )
            );

            sut.Register(
                EntryBuilder.For<Object>().UsingInstance( obj2 )
            );

            sut.Register(
                EntryBuilder.For<TypeWithArrayDependency>()
            );

            var actual = sut.Resolve<TypeWithArrayDependency>();

            Assert.IsTrue( actual.IsResolved );
        }
コード例 #36
0
        public void puzzleContainer_register_entry_should_report_entry_as_registered()
        {
            var container = new PuzzleContainer();

            container.Register( EntryBuilder.For<Object>() );
            var actual = container.IsRegistered<Object>();

            Assert.IsTrue( actual );
        }
コード例 #37
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding()
        {
            var entry = EntryBuilder.For<IFoo>()
                .Forward<IBar>()
                .ImplementedBy<TypeWithMultipleContracts>();

            var sut = new PuzzleContainer();
            sut.Register( entry );

            var actual = sut.IsRegistered<IBar>();

            Assert.IsTrue( actual );
        }
コード例 #38
0
        public void puzzleContainer_register_entry_as_transient_should_resolve_instances_as_transient()
        {
            var container = new PuzzleContainer();

            container.Register( EntryBuilder.For<Object>().WithLifestyle( Lifestyle.Transient ) );
            var i1 = container.Resolve<Object>();
            var i2 = container.Resolve<Object>();

            Assert.AreNotEqual( i1, i2 );
        }
コード例 #39
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_in_singleton_types()
        {
            var entry = EntryBuilder.For<IFoo>()
                .Forward<IBar>()
                .ImplementedBy<TypeWithMultipleContracts>();

            var sut = new PuzzleContainer();
            sut.Register( entry );

            var iBar = sut.Resolve<IBar>();
            var iFoo = sut.Resolve<IFoo>();

            Assert.AreEqual( iBar, iFoo );
        }
コード例 #40
0
        public void puzzleContainer_register_entry_with_factory_as_transient_should_resolve_instances_as_transient()
        {
            var expected = 2;
            var actual = 0;
            var container = new PuzzleContainer();

            container.Register
            (
                EntryBuilder.For<Object>()
                    .UsingFactory( () =>
                    {
                        actual++;
                        return new Object();
                    } )
                    .WithLifestyle( Lifestyle.Transient )
            );

            container.Resolve<Object>();
            container.Resolve<Object>();

            Assert.AreEqual( expected, actual );
        }
コード例 #41
0
        public void PuzzleContainer_entryBuilder_should_support_contract_forwarding_with_factory_method()
        {
            var entry = EntryBuilder.For<IFoo>()
                .Forward<IBar>()
                .UsingFactory( () => new TypeWithMultipleContracts() )
                .WithLifestyle( Lifestyle.Singleton );

            var sut = new PuzzleContainer();
            sut.Register( entry );

            var iBar = sut.Resolve<IBar>();
            var iFoo = sut.Resolve<IFoo>();

            Assert.AreEqual( iBar, iFoo );
        }
コード例 #42
0
        public void puzzleContainer_register_entry_with_factory_as_singleton_should_call_factory_once()
        {
            var expected = 1;
            var actual = 0;
            var container = new PuzzleContainer();

            container.Register
            (
                EntryBuilder.For<Object>()
                    .UsingFactory( () =>
                    {
                        actual++;
                        return new Object();
                    } )
            );

            container.Resolve<Object>();
            container.Resolve<Object>();

            Assert.AreEqual( expected, actual );
        }
コード例 #43
0
 public void puzzleContainer_register_entry_using_valid_entry_should_not_fail()
 {
     var container = new PuzzleContainer();
     container.Register( EntryBuilder.For<String>() );
 }
コード例 #44
0
        public void PuzzleContainer_entryBuilder_should_fail_if_implementation_does_not_support_one_of_the_contracts()
		{
            //TODO: Not yet implemented
			var entry = EntryBuilder.For<IFoo>()
				.Forward<IAmNotUsed>()
				.UsingInstance( new TypeWithMultipleContracts() );

			var sut = new PuzzleContainer();
			sut.Register( entry );
		}