コード例 #1
0
        public void ImplicitlyInstantiatedConcreteClassHasDefaultLifestyle()
        {
            var container = new PandoraContainer(new ComponentStore());

            var c1 = container.Resolve <ClassWithNoDependencies>();
            var c2 = container.Resolve <ClassWithNoDependencies>();

            Assert.Same(c1, c2);
        }
コード例 #2
0
        public void ImplicitlyInstantiatedConcreteClassHasDefaultLifestyle()
        {
            var container = new PandoraContainer(new ComponentStore());

            var c1 = container.Resolve<ClassWithNoDependencies>();
            var c2 = container.Resolve<ClassWithNoDependencies>();

            Assert.Same(c1, c2);
        }
コード例 #3
0
        public void ActivationHappensEveryTime()
        {
            var store = new ComponentStore();
            var registration = store.Add<IService, ClassWithNoDependencies>();
            registration.Lifestyle = ComponentLifestyles.Transient;

            var container = new PandoraContainer(store);
            var service = container.Resolve<IService>();
            var service2 = container.Resolve<IService>();

            Assert.NotSame(service, service2);
        }
コード例 #4
0
        public void ActivationHappensEveryTime()
        {
            var store = new ComponentStore();
            var registration = store.Add<IService, ClassWithNoDependencies>();
            registration.Lifestyle = ComponentLifestyles.Transient;

            var container = new PandoraContainer(store);
            var service = container.Resolve<IService>();
            var service2 = container.Resolve<IService>();

            Assert.NotSame(service, service2);
        }
コード例 #5
0
        public void ImplicitlyInstantiatedConcreteClassLifestyleCanBeConfigured()
        {
            var configuration = new BehaviorConfiguration
            {
                ImplicitTypeLifestyle = BehaviorConfiguration.Lifestyle.Transient
            };
            var container = new PandoraContainer(configuration);

            var c1 = container.Resolve <ClassWithNoDependencies>();
            var c2 = container.Resolve <ClassWithNoDependencies>();

            Assert.NotSame(c1, c2);
        }
コード例 #6
0
        public void ImplicitlyInstantiatedConcreteClassLifestyleCanBeConfigured()
        {
            var configuration = new BehaviorConfiguration
                                    {
                                        ImplicitTypeLifestyle = BehaviorConfiguration.Lifestyle.Transient
                                    };
            var container = new PandoraContainer(configuration);

            var c1 = container.Resolve<ClassWithNoDependencies>();
            var c2 = container.Resolve<ClassWithNoDependencies>();

            Assert.NotSame(c1, c2);
        }
コード例 #7
0
        public void ActivationHappensOnlyOnceForSingletonComponents()
        {
            var store        = new ComponentStore();
            var registration = store.Add <IService, ClassWithNoDependencies>("test");

            registration.Lifestyle = ComponentLifestyles.Singleton;

            var container = new PandoraContainer(store);

            var service  = container.Resolve <IService>("test");
            var service2 = container.Resolve <IService>("test");

            Assert.Same(service, service2);
        }
コード例 #8
0
        public void CanConfigureLifestyle()
        {
            store.Register((p) =>
            {
                p.Service <IService>()
                .Implementor <ClassWithDependencyOnItsOwnService>()
                .Lifestyle.Singleton();
                p.Service <IService>()
                .Implementor <ClassWithNoDependencies>()
                .Lifestyle.Singleton();
            });

            Assert.DoesNotThrow(() => container.Resolve <IService>());
        }
コード例 #9
0
        public void NoRegistrationIsRequiredToInstantiateConcreteClassWithoutDependencies()
        {
            var container    = new PandoraContainer(new ComponentStore());
            var dependencies = container.Resolve <ClassWithNoDependencies>();

            Assert.IsType <ClassWithNoDependencies>(dependencies);
        }
コード例 #10
0
        public void CanSplitGraph()
        {
            /*
             * Dotty Graph:
             *
             * digraph G {
             * a [label="IService :: A"] ;
             * a -> b [label="service1"] ;
             * a -> c [label="service2"] ;
             * b [label="IService :: B"] ;
             * b -> d ;
             * d [label="IService :: C"] ;
             * c [label="IService :: B"] ;
             * c -> f ;
             * f [label="IService :: C"] ;
             * }
             */

            var store = new ComponentStore();

            store.Add <IService, ClassWithTwoDependenciesOnItsOwnService>();
            store.Add <IService, ClassWithDependencyOnItsOwnService>();
            store.Add <IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve <IService>();

            Assert.IsType(typeof(ClassWithTwoDependenciesOnItsOwnService), service);
        }
コード例 #11
0
        public void ClassWithDependencyOnConcreteClassResolves()
        {
            var container  = new PandoraContainer(new ComponentStore());
            var dependency =
                container.Resolve <GenericWithDependency <ClassWithNoDependencies> >();

            Assert.IsType <ClassWithNoDependencies>(dependency.Dependency);
        }
コード例 #12
0
        public void CanResolveConcreteType()
        {
            var store = new ComponentStore();
            store.Add<ClassWithNoDependencies, ClassWithNoDependencies>();
            var container = new PandoraContainer(store);

            Assert.DoesNotThrow(() => container.Resolve<ClassWithNoDependencies>());
        }
コード例 #13
0
 public void ConcreteClassInstantiationCanBeTurnedOff()
 {
     var behaviorConfiguration = new BehaviorConfiguration
                                     {
                                         EnableImplicitTypeInstantiation = false
                                     };
     var container = new PandoraContainer(behaviorConfiguration);
     Assert.Throws<ServiceNotFoundException>(() => container.Resolve<ClassWithNoDependencies>());
 }
コード例 #14
0
        public void CanRegisterOnContainerWithoutStore()
        {
            container.Register(p => p.Service <IService>()
                               .Implementor <ClassWithNoDependencies>());

            var service = container.Resolve <IService>();

            Assert.IsType <ClassWithNoDependencies>(service);
        }
コード例 #15
0
        public void ThrowsExceptionIfDependencyCouldNotBeSatisfied()
        {
            var store = new ComponentStore();

            store.Add <ClassWithOneDependency, ClassWithOneDependency>();
            var container = new PandoraContainer(store);

            Assert.Throws <DependencyMissingException>(() => container.Resolve <ClassWithOneDependency>());
        }
コード例 #16
0
        public void CanResolveConcreteType()
        {
            var store = new ComponentStore();

            store.Add <ClassWithNoDependencies, ClassWithNoDependencies>();
            var container = new PandoraContainer(store);

            container.Resolve <ClassWithNoDependencies>();
        }
コード例 #17
0
        public void CanResolveWhenGivenTwoConstructorsWhereOneCantBeSatisfied()
        {
            var store = new ComponentStore();

            store.Add <IService4, ClassWithMultipleConstructors>();

            var container = new PandoraContainer(store);

            container.Resolve <IService4>();
        }
コード例 #18
0
        public void ThrowsExceptionIfTypeNotRegistered()
        {
            var store = new ComponentStore();

            var container = new PandoraContainer(store);

            Assert.Throws <ServiceNotFoundException>(() => {
                var result = container.Resolve <IService>();
            });
        }
コード例 #19
0
        public void NoRegistrationIsRequiredToInstantiateConcreteClassWithoutDependencies()
        {
            var container = new PandoraContainer(new ComponentStore());

            Assert.DoesNotThrow(() =>
                                    {
                                        var dependencies = container.Resolve<ClassWithNoDependencies>();
                                        Assert.IsType<ClassWithNoDependencies>(dependencies);
                                    });
        }
コード例 #20
0
        public void ConcreteClassInstantiationCanBeTurnedOff()
        {
            var behaviorConfiguration = new BehaviorConfiguration
            {
                EnableImplicitTypeInstantiation = false
            };
            var container = new PandoraContainer(behaviorConfiguration);

            Assert.Throws <ServiceNotFoundException>(() => container.Resolve <ClassWithNoDependencies>());
        }
コード例 #21
0
        public void CanResolveClassWithoutDependencies()
        {
            var componentStore = new ComponentStore();
            componentStore.Add<IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(componentStore);
            var result = container.Resolve<IService>();

            Assert.IsType<ClassWithNoDependencies>(result);
        }
コード例 #22
0
        public void ThrowsDependencyMissingExceptionIfDependencyChainCannotBeSatisfied()
        {
            var store = new ComponentStore();

            store.Add <IService, ClassWithDependencyOnItsOwnService>();

            var container = new PandoraContainer(store);

            Assert.Throws <DependencyMissingException>(() => container.Resolve <IService>());
        }
コード例 #23
0
        public void RetrievalByNameThrowsExceptionWhenNameNotRegistered()
        {
            var store = new ComponentStore();
            var container = new PandoraContainer(store);
            //To make it lookup something
            container.Register(p =>
                p.Service<ClassWithNoDependencies>()
                    .Implementor<ClassWithNoDependencies>());

            Assert.Throws<ServiceNotFoundException>(() => container.Resolve<ClassWithNoDependencies>("test"));
        }
コード例 #24
0
        public void CanInsertInstanceWithoutName()
        {
            var store = new ComponentStore();
            var instance = new ClassWithNoDependencies();
            store.AddInstance<IService>(instance);
            var container = new PandoraContainer(store);

            var service = container.Resolve<IService>();

            Assert.Same(instance, service);
        }
コード例 #25
0
        public void CanResolveClassWithOneDependency()
        {
            IComponentStore componentStore = new ComponentStore();
            componentStore.Add<IService, ClassWithNoDependencies>();
            componentStore.Add<IService2, ClassWithOneDependency>();

            PandoraContainer locator = new PandoraContainer(componentStore);
            var result = locator.Resolve<IService2>();

            Assert.IsType<ClassWithOneDependency>(result);
        }
コード例 #26
0
        public void CanRetrieveServiceByName()
        {
            var store = new ComponentStore();
            //This registration is to give the container something to choose from, in case named lookup won't work
            store.Add<IService, ClassWithDependencyOnItsOwnService>("memory.repository");
            store.Add<IService, ClassWithNoDependencies>("db.repository");
            var container = new PandoraContainer(store);

            var service = container.Resolve<IService>("db.repository");
            Assert.IsType<ClassWithNoDependencies>(service);
        }
コード例 #27
0
        public void CanResolveClassWithTwoDependenciesOnItsOwnServiceWithOnlyOneSubdependencyRegistration()
        {
            var store = new ComponentStore();
            store.Add<IService, ClassWithTwoDependenciesOnItsOwnService>();
            store.Add<IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve<IService>();
            Assert.IsType(typeof (ClassWithTwoDependenciesOnItsOwnService), service);
        }
コード例 #28
0
        public void DependencyMissingExceptionPropagatesThroughMultipleLevels()
        {
            var store = new ComponentStore();

            store.Add <IService3, ClassDependingOnClassWithOneDependency>();
            store.Add <IService2, ClassWithOneDependency>();

            var container = new PandoraContainer(store);

            Assert.Throws <DependencyMissingException>(() => container.Resolve <IService3>());
        }
コード例 #29
0
        public void ClassWithDependencyOnConcreteClassResolves()
        {
            var container = new PandoraContainer(new ComponentStore());

            Assert.DoesNotThrow(() =>
                                    {
                                        var dependency =
                                            container.Resolve<GenericWithDependency<ClassWithNoDependencies>>();
                                        Assert.IsType<ClassWithNoDependencies>(dependency.Dependency);
                                    });
        }
コード例 #30
0
        public void CanResolveClassWithoutDependencies()
        {
            var componentStore = new ComponentStore();

            componentStore.Add <IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(componentStore);
            var result    = container.Resolve <IService>();

            Assert.IsType <ClassWithNoDependencies>(result);
        }
コード例 #31
0
        public void RetrievalByNameThrowsExceptionWhenNameNotRegistered()
        {
            var store     = new ComponentStore();
            var container = new PandoraContainer(store);

            //To make it lookup something
            container.Register(p =>
                               p.Service <ClassWithNoDependencies>()
                               .Implementor <ClassWithNoDependencies>());

            Assert.Throws <ServiceNotFoundException>(() => container.Resolve <ClassWithNoDependencies>("test"));
        }
コード例 #32
0
        public void CanResolveClassWithMultipleDependencies()
        {
            var store = new ComponentStore();
            store.Add<IService, ClassWithNoDependencies>();
            store.Add<IService2, ClassWithOneDependency>();
            store.Add<IService3, ClassDependingOnClassWithOneDependency>();

            var container = new PandoraContainer(store);
            var result = container.Resolve<IService3>();

            Assert.IsType<ClassDependingOnClassWithOneDependency>(result);
        }
コード例 #33
0
        public void CanInsertInstanceWithoutName()
        {
            var store    = new ComponentStore();
            var instance = new ClassWithNoDependencies();

            store.AddInstance <IService>(instance);
            var container = new PandoraContainer(store);

            var service = container.Resolve <IService>();

            Assert.Same(instance, service);
        }
コード例 #34
0
        public void CanResolveClassWithOneDependency()
        {
            IComponentStore componentStore = new ComponentStore();

            componentStore.Add <IService, ClassWithNoDependencies>();
            componentStore.Add <IService2, ClassWithOneDependency>();

            PandoraContainer locator = new PandoraContainer(componentStore);
            var result = locator.Resolve <IService2>();

            Assert.IsType <ClassWithOneDependency>(result);
        }
コード例 #35
0
        public void CanSpecifyDependencyByName()
        {
            var store = new ComponentStore();
            store.Add<IService, ClassWithNoDependencies>("service1");
            store.Add<IService, ClassWithNoDependencies2>("service2");
            store.Add<ClassWithOneDependency, ClassWithOneDependency>()
                .Parameters["dependency"] = "service2";
            var container = new PandoraContainer(store);

            var service2 = container.Resolve<ClassWithOneDependency>();

            Assert.IsType<ClassWithNoDependencies2>(service2.Dependency);
        }
コード例 #36
0
        public void CanResolveClassWithMultipleDependencies()
        {
            var store = new ComponentStore();

            store.Add <IService, ClassWithNoDependencies>();
            store.Add <IService2, ClassWithOneDependency>();
            store.Add <IService3, ClassDependingOnClassWithOneDependency>();

            var container = new PandoraContainer(store);
            var result    = container.Resolve <IService3>();

            Assert.IsType <ClassDependingOnClassWithOneDependency>(result);
        }
コード例 #37
0
        public void CanResolveClassWithTwoDependenciesOnItsOwnServiceWithOnlyOneSubdependencyRegistration()
        {
            var store = new ComponentStore();

            store.Add <IService, ClassWithTwoDependenciesOnItsOwnService>();
            store.Add <IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve <IService>();

            Assert.IsType(typeof(ClassWithTwoDependenciesOnItsOwnService), service);
        }
コード例 #38
0
        public void CanRetrieveServiceByName()
        {
            var store = new ComponentStore();

            //This registration is to give the container something to choose from, in case named lookup won't work
            store.Add <IService, ClassWithDependencyOnItsOwnService>("memory.repository");
            store.Add <IService, ClassWithNoDependencies>("db.repository");
            var container = new PandoraContainer(store);

            var service = container.Resolve <IService>("db.repository");

            Assert.IsType <ClassWithNoDependencies>(service);
        }
コード例 #39
0
        public void CanResolveSpecificGenericClass()
        {
            store.Register(p =>
                           p.Service <GenericClass <string> >()
                           .Implementor <GenericClass <string> >());

            container.Resolve <GenericClass <string> >();
        }
コード例 #40
0
        public void CanResolveSpecificGenericClass()
        {
            store.Register(p =>
                           p.Service <GenericClass <string> >()
                           .Implementor <GenericClass <string> >());

            Assert.DoesNotThrow(() => container.Resolve <GenericClass <string> >());
        }
コード例 #41
0
        public void CanSpecifyDependencyByName()
        {
            var store = new ComponentStore();

            store.Add <IService, ClassWithNoDependencies>("service1");
            store.Add <IService, ClassWithNoDependencies2>("service2");
            store.Add <ClassWithOneDependency, ClassWithOneDependency>()
            .Parameters["dependency"] = "service2";
            var container = new PandoraContainer(store);

            var service2 = container.Resolve <ClassWithOneDependency>();

            Assert.IsType <ClassWithNoDependencies2>(service2.Dependency);
        }
コード例 #42
0
        public void CanResolveDependencyChainOfSameService()
        {
            var store = new ComponentStore();

            store.Add <IService, ClassWithDependencyOnItsOwnService>();
            store.Add <IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve <IService>();

            Assert.IsType(typeof(ClassWithDependencyOnItsOwnService), service);
            var ownService = (ClassWithDependencyOnItsOwnService)service;

            Assert.IsType(typeof(ClassWithNoDependencies), ownService.SubService);
        }
コード例 #43
0
        public void CanInstantiateClassThatDependsOnAString()
        {
            var store = new ComponentStore();
            store.Register(
                p =>
                {
                    p.Service<ClassDependingOnAString>()
                        .Implementor<ClassDependingOnAString>()
                        .Parameters("string1");
                    p.Service<string>("string1")
                        .Instance("Hello World");
                });
            var container = new PandoraContainer(store);

            var resolve = container.Resolve<ClassDependingOnAString>();
            Assert.Equal("Hello World", resolve.Dependency);
        }
コード例 #44
0
        public void CanInstantiateClassThatDependsOnAString()
        {
            var store = new ComponentStore();

            store.Register(
                p =>
            {
                p.Service <ClassDependingOnAString>()
                .Implementor <ClassDependingOnAString>()
                .Parameters("string1");
                p.Service <string>("string1")
                .Instance("Hello World");
            });
            var container = new PandoraContainer(store);

            var resolve = container.Resolve <ClassDependingOnAString>();

            Assert.Equal("Hello World", resolve.Dependency);
        }
コード例 #45
0
        public void CanResolveDependencyChainOfSameServiceWithMultipleLevels()
        {
            var store = new ComponentStore();

            store.Add <IService, ClassWithDependencyOnItsOwnService>();
            store.Add <IService, ClassWithDependencyOnItsOwnService>();
            store.Add <IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve <IService>();

            var level1 = (ClassWithDependencyOnItsOwnService)service;
            var level2 = (ClassWithDependencyOnItsOwnService)level1.SubService;
            var level3 = (ClassWithNoDependencies)level2.SubService;

            Assert.IsType(typeof(ClassWithDependencyOnItsOwnService), level1);
            Assert.IsType(typeof(ClassWithDependencyOnItsOwnService), level2);
            Assert.IsType(typeof(ClassWithNoDependencies), level3);
        }
コード例 #46
0
        public void CanResolveDependencyChainOfSameServiceWithMultipleLevels()
        {
            var store = new ComponentStore();
            store.Add<IService, ClassWithDependencyOnItsOwnService>();
            store.Add<IService, ClassWithDependencyOnItsOwnService>();
            store.Add<IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve<IService>();

            var level1 = (ClassWithDependencyOnItsOwnService) service;
            var level2 = (ClassWithDependencyOnItsOwnService) level1.SubService;
            var level3 = (ClassWithNoDependencies)level2.SubService;

            Assert.IsType(typeof (ClassWithDependencyOnItsOwnService), level1);
            Assert.IsType(typeof(ClassWithDependencyOnItsOwnService), level2);
            Assert.IsType(typeof(ClassWithNoDependencies), level3);
        }
コード例 #47
0
        public void CanResolveWhenGivenTwoConstructorsWhereOneCantBeSatisfied()
        {
            var store = new ComponentStore();
            store.Add<IService4, ClassWithMultipleConstructors>();

            var container = new PandoraContainer(store);

            Assert.DoesNotThrow(() => container.Resolve<IService4>());
        }
コード例 #48
0
        public void CanSplitGraph()
        {
            /*
             * Dotty Graph:
             *
            digraph G {
              a [label="IService :: A"] ;
              a -> b [label="service1"] ;
              a -> c [label="service2"] ;
              b [label="IService :: B"] ;
              b -> d ;
              d [label="IService :: C"] ;
              c [label="IService :: B"] ;
              c -> f ;
              f [label="IService :: C"] ;
            }
             */

            var store = new ComponentStore();
            store.Add<IService, ClassWithTwoDependenciesOnItsOwnService>();
            store.Add<IService, ClassWithDependencyOnItsOwnService>();
            store.Add<IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve<IService>();
            Assert.IsType(typeof (ClassWithTwoDependenciesOnItsOwnService), service);
        }
コード例 #49
0
        public void DependencyMissingExceptionPropagatesThroughMultipleLevels()
        {
            var store = new ComponentStore();
            store.Add<IService3, ClassDependingOnClassWithOneDependency>();
            store.Add<IService2, ClassWithOneDependency>();

            var container = new PandoraContainer(store);

            Assert.Throws<DependencyMissingException>(() => container.Resolve<IService3>());
        }
コード例 #50
0
        public void ThrowsDependencyMissingExceptionIfDependencyChainCannotBeSatisfied()
        {
            var store = new ComponentStore();
            store.Add<IService, ClassWithDependencyOnItsOwnService>();

            var container = new PandoraContainer(store);

            Assert.Throws<DependencyMissingException>(() => container.Resolve<IService>());
        }
コード例 #51
0
        public void ThrowsExceptionIfDependencyCouldNotBeSatisfied()
        {
            var store = new ComponentStore();
            store.Add<ClassWithOneDependency, ClassWithOneDependency>();
            var container = new PandoraContainer(store);

            Assert.Throws<DependencyMissingException>(() => container.Resolve<ClassWithOneDependency>());
        }
コード例 #52
0
        public void ThrowsExceptionIfTypeNotRegistered()
        {
            var store = new ComponentStore();

            var container = new PandoraContainer(store);

            Assert.Throws<ServiceNotFoundException>(() => {
                                                              var result = container.Resolve<IService>();
            });
        }
コード例 #53
0
        public void CanResolveDependencyChainOfSameService()
        {
            var store = new ComponentStore();
            store.Add<IService, ClassWithDependencyOnItsOwnService>();
            store.Add<IService, ClassWithNoDependencies>();

            var container = new PandoraContainer(store);

            var service = container.Resolve<IService>();

            Assert.IsType(typeof (ClassWithDependencyOnItsOwnService), service);
            var ownService = (ClassWithDependencyOnItsOwnService)service;
            Assert.IsType(typeof (ClassWithNoDependencies), ownService.SubService);
        }