public void Throws_Exception_If_Type_Not_Found()
            {
                var registry = new SimpleTypeRegistry();

                Assert.Throws <TypeNotRegisteredException>(() =>
                                                           registry.Find(
                                                               typeof(int)));
            }
            public void Returns_True_If_Type_Registered()
            {
                var registry = new SimpleTypeRegistry();

                registry.Register(typeof(IEmptyInteface), typeof(ConcreteClassImplementingEmptyInterface));

                Assert.True(registry.Contains(typeof(IEmptyInteface)));
            }
            public void Registering_Unrelated_Classes_Throws_Exception()
            {
                var registry = new SimpleTypeRegistry();

                Assert.Throws <ArgumentException>(() =>
                                                  registry.Register(
                                                      typeof(System.String),
                                                      typeof(AbstractClassImplementingEmptyInterface)));
            }
            public void Registering_An_Abstract_Class_Throws_Exception()
            {
                var registry = new SimpleTypeRegistry();

                Assert.Throws <ArgumentException>(() =>
                                                  registry.Register(
                                                      typeof(ConcreteClassImplementingEmptyInterface),
                                                      typeof(AbstractClassImplementingEmptyInterface)));
            }
            public void Replaces_Existing_Entry()
            {
                var registry = new SimpleTypeRegistry();

                registry.Register(typeof(IEmptyInteface), typeof(ConcreteClassImplementingEmptyInterface));
                Assert.Equal(typeof(ConcreteClassImplementingEmptyInterface), registry.Find(typeof(IEmptyInteface)));

                registry.Register(typeof(IEmptyInteface), typeof(AlternativeConcreteClassImplementingEmptyInterface));
                Assert.Equal(typeof(AlternativeConcreteClassImplementingEmptyInterface), registry.Find(typeof(IEmptyInteface)));
            }
            public void Returns_Specific_Type_When_Asked_For_General_Type()
            {
                var registry = new SimpleTypeRegistry();

                registry.Register(typeof(IEmptyInteface), typeof(ConcreteClassImplementingEmptyInterface));

                var concreteType = registry.Find(typeof(IEmptyInteface));

                Assert.Equal(typeof(ConcreteClassImplementingEmptyInterface), concreteType);
            }
Example #7
0
        public void TestExceptionThatIsThrowHasHierarchyInformationIfCantFullySatisfyConstructor()
        {
            var registry = new SimpleTypeRegistry();

            registry.Register <ISimpleInterface>().WithConcreteType <EmptyClassWithDefaultConstructor>();
            registry.Register <ISecondInterface>().WithConcreteType <EmptyClassWithThatOneSimpleObjectInItsConstructor>();
            registry.Register <IThirdInterface>().WithConcreteType <MoreComplicatedClassThatCantBeFullySatisfied>();
            var builder = Clean.MakeBuilder();

            builder.AddRegistry(registry);
            var container = builder.Container;

            var first = container.GetInstanceOf <ISimpleInterface>();

            Assert.That(first, Is.Not.Null);

            var second = container.GetInstanceOf <ISecondInterface>();

            Assert.That(second, Is.Not.Null);

            try {
                container.GetInstanceOf <IThirdInterface>();
            } catch (UnableToConstructException ex) {
                Assert.That(ex.AttemptedConstructors, Is.Not.Null);
                Assert.That(ex.AttemptedConstructors, Has.Count.EqualTo(1));

                Assert.That(ex.AttemptedConstructors[0].Parameters, Is.Not.Null);
                Assert.That(ex.AttemptedConstructors[0].Parameters, Has.Count.EqualTo(2));
                Assert.That(ex.AttemptedConstructors[0].Success, Is.Not.True);

                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts, Is.Not.Null);
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts, Has.Count.EqualTo(1));

                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].Outcome, Is.EqualTo(ConstructionOutcome.Success));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].Injected, Is.EqualTo(typeof(EmptyClassWithThatOneSimpleObjectInItsConstructor)));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].Declared, Is.EqualTo(typeof(ISecondInterface)));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters, Is.Not.Null);
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters, Has.Count.EqualTo(1));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Success, Is.EqualTo(ConstructionOutcome.Success));

                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].Injected, Is.EqualTo(typeof(EmptyClassWithDefaultConstructor)));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].Declared, Is.EqualTo(typeof(ISimpleInterface)));

                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].ConstructorAttempts, Is.Not.Null);
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].ConstructorAttempts, Has.Count.EqualTo(1));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].ConstructorAttempts[0].Success, Is.EqualTo(ConstructionOutcome.Success));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters, Is.Not.Null);
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters.ToList()[0].ConstructorAttempts[0].Parameters, Is.Empty);

                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[1].Injected, Is.Null);
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[1].Declared, Is.EqualTo(typeof(IFourthInterfaceActuallyDoesntHaveAnyDerivedClasses)));
                Assert.That(ex.AttemptedConstructors[0].Parameters.ToList()[1].Outcome, Is.EqualTo(ConstructionOutcome.NoMappingFound));
            }
        }
Example #8
0
        public void TestThatItWorksWithASimpleTypeWithADefaultConstructor()
        {
            var registry = new SimpleTypeRegistry();

            registry.Register <ISimpleInterface>().WithConcreteType <EmptyClassWithDefaultConstructor>();
            var builder = Clean.MakeBuilder();

            builder.AddRegistry(registry);
            var container = builder.Container;

            var instance = container.GetInstanceOf <ISimpleInterface>();

            Assert.That(instance, Is.Not.Null);
            Assert.That(instance, Is.TypeOf <EmptyClassWithDefaultConstructor>());
        }
Example #9
0
        public void TestThatWithoutSettingALifetimeItIsTransient()
        {
            var registry = new SimpleTypeRegistry();

            registry.Register <ISimpleInterface>().WithConcreteType <EmptyClassWithDefaultConstructor>();
            var builder = Clean.MakeBuilder();

            builder.AddRegistry(registry);
            var container = builder.Container;

            var first  = container.GetInstanceOf <ISimpleInterface>();
            var second = container.GetInstanceOf <ISimpleInterface>();

            Assert.That(first, Is.Not.Null);
            Assert.That(second, Is.Not.Null);
            Assert.That(first, Is.Not.SameAs(second));
        }
Example #10
0
        static void Main(string[] args)
        {
            bool doSimpleInversion = true;

            if (args.Length > 0)
            {
                doSimpleInversion = !(string.Compare(args[0], "auto", StringComparison.CurrentCultureIgnoreCase) == 0);
            }

            if (doSimpleInversion)
            {
                var types     = new SimpleTypeRegistry();
                var instances = new ResolvingInstanceCreator(types);

                var container = new TypeContainer(types, instances);
                DependencyInjection.Container = container;

                //DependencyInjection.Container.Register<ISpeak, Cat>();
                //DependencyInjection.Container.Register<ISpeak, Dog>();
                //DependencyInjection.Container.Register<ISpeak, Parrot>();
                DependencyInjection.Container.Register <ISpeak, Hydra>();
            }
            else
            {
                var types     = new SimpleTypeRegistry();
                var instances = new ResolvingInstanceCreator(types);
                var container = new AutoTypeContainer("brioche.walkingskeleton", types, instances);

                DependencyInjection.Container = container;
            }

            try
            {
                var speaker = DependencyInjection.Container.Resolve <ISpeak>();

                speaker.Speak();
            }
            catch (MissingMethodException)
            {
            }

            Console.WriteLine("Demo complete, press a key to close window");
            Console.ReadKey();
        }
Example #11
0
        public void TestThrowsAnExceptionIfCantFullySatisfyConstructor()
        {
            var registry = new SimpleTypeRegistry();

            registry.Register <ISimpleInterface>().WithConcreteType <EmptyClassWithDefaultConstructor>();
            registry.Register <ISecondInterface>().WithConcreteType <EmptyClassWithThatOneSimpleObjectInItsConstructor>();
            registry.Register <IThirdInterface>().WithConcreteType <MoreComplicatedClassThatCantBeFullySatisfied>();
            var builder = Clean.MakeBuilder();

            builder.AddRegistry(registry);
            var container = builder.Container;

            var first = container.GetInstanceOf <ISimpleInterface>();

            Assert.That(first, Is.Not.Null);

            var second = container.GetInstanceOf <ISecondInterface>();

            Assert.That(second, Is.Not.Null);

            Assert.That(() => container.GetInstanceOf <IThirdInterface>(), Throws.InstanceOf <UnableToConstructException>());
        }
Example #12
0
        public void TestThatItCanConstructTypesWithASingleRegistredType()
        {
            var registry = new SimpleTypeRegistry();

            registry.Register <ISimpleInterface>().WithConcreteType <EmptyClassWithDefaultConstructor>();
            registry.Register <ISecondInterface>().WithConcreteType <EmptyClassWithThatOneSimpleObjectInItsConstructor>();
            var builder = Clean.MakeBuilder();

            builder.AddRegistry(registry);
            var container = builder.Container;

            var first = container.GetInstanceOf <ISimpleInterface>();

            Assert.That(first, Is.Not.Null);
            Assert.That(first, Is.TypeOf <EmptyClassWithDefaultConstructor>());

            var second = container.GetInstanceOf <ISecondInterface>();

            Assert.That(second, Is.Not.Null);
            Assert.That(second, Is.TypeOf <EmptyClassWithThatOneSimpleObjectInItsConstructor>());
            Assert.That(second.FirstParam, Is.Not.Null);
            Assert.That(second.FirstParam, Is.InstanceOf <EmptyClassWithDefaultConstructor>());
        }
            public void Returns_False_When_Registry_Is_Empty()
            {
                var registry = new SimpleTypeRegistry();

                Assert.False(registry.Contains(typeof(System.String)));
            }