public void RegisterTypeAsSingleton(Type implementation)
 {
     this.defaultContainer.Register(
         EntryBuilder.For(implementation)
         .WithLifestyle(Lifestyle.Singleton)
         );
 }
 public void RegisterTypeAsTransient(Type implementation)
 {
     this.defaultContainer.Register(
         EntryBuilder.For(implementation)
         .WithLifestyle(Lifestyle.Transient)
         );
 }
 public void RegisterInstance(IEnumerable <Type> contracts, object instance)
 {
     this.defaultContainer.Register(
         EntryBuilder.For(contracts.First())
         .UsingInstance(instance)
         .WithLifestyle(Lifestyle.Singleton)
         );
 }
 public void RegisterAsSingleton(IEnumerable <Type> contracts, Type implementation)
 {
     this.defaultContainer.Register(
         EntryBuilder.For(contracts.First())
         .ImplementedBy(implementation)
         .WithLifestyle(Lifestyle.Singleton)
         );
 }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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));
        }
        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);
        }
        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);
        }
        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>());
        }
        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);
        }
        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);
        }
        public void puzzleContainer_register_entry_using_valid_entry_should_not_fail()
        {
            var container = new PuzzleContainer();

            container.Register(EntryBuilder.For <string>());
        }