public void CollectsAllBindingsAndLoadsThemDistinctOnKernel()
        {
            var loadedModules = new List<INinjectModule>();

            A.CallTo(() => this.kernel.Load(A<IEnumerable<INinjectModule>>._))
                .Invokes((IEnumerable<INinjectModule> modules) => loadedModules.AddRange(modules));

            var dependencyA = new DependencyA();
            var dependencyB = new DependencyB();

            var dependencyDefinitionA = new Dependency<IDependencyA>(bind => bind.ToConstant(dependencyA).InTransientScope());
            var dependencyDefinitionB = new Dependency<IDependencyB>(bind => bind.ToConstant(dependencyB).InSingletonScope());

            this.testee.Load(
                new FeatureA(
                    dependencyDefinitionA,
                    dependencyDefinitionB),
                new FeatureB(
                    dependencyDefinitionA,
                    dependencyDefinitionB),
                new FeatureC());

            loadedModules.Select(_ => _.GetType().Name)
                .Should().Contain(
                    new[] 
                    {
                        typeof(ModuleA),
                        typeof(ModuleB),
                        typeof(ModuleC),
                        typeof(ModuleD)
                    }.Select(_ => _.Name))
                    .And.BeEquivalentTo(loadedModules.Select(_ => _.GetType().Name).Distinct());
        }
Exemplo n.º 2
0
        public void The_Use_by_default_should_replace_previous_typed_registration()
        {
            var container = new Container();

            var aa = new DependencyA();

            container.Use(aa);
            Assert.AreSame(aa, container.Resolve <DependencyA>());

            container.Use(new DependencyA());
            Assert.AreNotSame(aa, container.Resolve <DependencyA>());
        }
Exemplo n.º 3
0
        public void Can_keep_previous_default()
        {
            var container = new Container();

            var aa = new DependencyA();

            container.RegisterInstance(aa);
            container.RegisterInstance(new DependencyA(), IfAlreadyRegistered.Keep);

            var x = container.Resolve <DependencyA>();

            Assert.AreSame(aa, x);
        }
Exemplo n.º 4
0
        public void Can_keep_previous_default_with_presence_of_keyed()
        {
            var container = new Container();

            var aa = new DependencyA();

            container.UseInstance(aa);
            container.UseInstance(aa, serviceKey: "aa");
            container.UseInstance(new DependencyA(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            var x = container.Resolve <DependencyA>();

            Assert.AreSame(aa, x);
        }
Exemplo n.º 5
0
        public void Can_append_to_previous_typed_registration()
        {
            var container = new Container();

            container.Register <DependencyA>();

            var aa = new DependencyA();

            container.UseInstance(aa, ifAlreadyRegistered: IfAlreadyRegistered.AppendNotKeyed);

            var aas = container.ResolveMany <DependencyA>();

            Assert.AreEqual(2, aas.Count());
        }
Exemplo n.º 6
0
        public void UseInstance_by_default_should_replace_previous_typed_registration()
        {
            var container = new Container();

            container.Register <DependencyA>();

            var aa = new DependencyA();

            container.UseInstance(aa);

            var x = container.Resolve <DependencyA>();

            Assert.AreSame(aa, x);
        }
Exemplo n.º 7
0
        public void TestChildDependencies()
        {
            IDependencyA  dependency = new DependencyA();
            IXrmContainer child      = XrmContainer.Instance.CreateChild();

            child.Register <IDependencyA>(dependency);
            IModuleA module = child.Resolve <IModuleA>();

            Assert.AreSame(dependency, module.Dependency);

            IModuleA moduleParent = XrmContainer.Instance.Resolve <IModuleA>();

            Assert.AreNotSame(dependency, moduleParent.Dependency);
        }
Exemplo n.º 8
0
        public void Can_append_to_previous_typed_registration()
        {
            var container = new Container();

            container.Register <DependencyA>();

            var aa = new DependencyA();

            container.RegisterInstance(aa);

            var aas = container.ResolveMany <DependencyA>();

            Assert.AreEqual(2, aas.Count());
        }
Exemplo n.º 9
0
        public void Can_throw_on_previous_default()
        {
            var container = new Container();

            var aa = new DependencyA();

            container.RegisterInstance(aa);

            var ex = Assert.Throws <ContainerException>(() =>
                                                        container.RegisterInstance(new DependencyA(), IfAlreadyRegistered.Throw));

            Assert.AreEqual(
                Error.NameOf(Error.UnableToRegisterDuplicateDefault),
                ex.ErrorName);
        }
Exemplo n.º 10
0
        public void Can_throw_on_previous_default_with_presence_of_keyed()
        {
            var container = new Container();

            var aa = new DependencyA();

            container.UseInstance(aa);
            container.UseInstance(aa, serviceKey: "aa");

            var ex = Assert.Throws <ContainerException>(() =>
                                                        container.UseInstance(new DependencyA(), IfAlreadyRegistered.Throw));

            Assert.AreEqual(
                Error.NameOf(Error.UnableToRegisterDuplicateDefault),
                Error.NameOf(ex.Error));
        }
Exemplo n.º 11
0
        public void Can_append_new_default_implementation_And_ignore_the_duplicate_implementation()
        {
            var container = new Container();

            var aa = new DependencyA();

            container.RegisterInstance(aa);
            container.RegisterInstance <DependencyA>(new AAA(), IfAlreadyRegistered.AppendNewImplementation);

            Assert.AreEqual(2, container.ResolveMany <DependencyA>().Count());

            var newaaa = new AAA();

            container.RegisterInstance <DependencyA>(newaaa, IfAlreadyRegistered.AppendNewImplementation);

            container.RegisterInstance <DependencyA>(new AAAA(), IfAlreadyRegistered.AppendNewImplementation);
            Assert.AreEqual(3, container.ResolveMany <DependencyA>().Count());
        }
Exemplo n.º 12
0
        public void Can_append_multiple_and_then_replace_all()
        {
            var container = new Container();

            container.Register <DependencyA>();

            var aa = new DependencyA();

            container.RegisterInstance(aa, IfAlreadyRegistered.AppendNotKeyed);
            container.RegisterInstance <DependencyA>(new AAA(), IfAlreadyRegistered.AppendNotKeyed);
            var aas = container.ResolveMany <DependencyA>();

            Assert.AreEqual(3, aas.Count());

            container.RegisterInstance <DependencyA>(new AAAA(), IfAlreadyRegistered.Replace);
            var aaaa = container.Resolve <DependencyA>();

            Assert.IsInstanceOf <AAAA>(aaaa);
        }
Exemplo n.º 13
0
        public void IoCRegInstancesBeforeBuildGraph()
        {
            var instanceA = new DependencyA();

            IoCManager.RegisterInstance <DependencyA>(instanceA, deferInject: true);

            var instanceB = new DependencyB();

            IoCManager.RegisterInstance <DependencyB>(instanceB, deferInject: true);

            IoCManager.BuildGraph();

            var resolveA = IoCManager.Resolve <DependencyA>();
            var resolveB = IoCManager.Resolve <DependencyB>();

            Assert.That(instanceA, Is.EqualTo(resolveA));
            Assert.That(instanceB, Is.EqualTo(resolveB));
            Assert.That(resolveA._depB, Is.EqualTo(resolveB));
            Assert.That(resolveB._depA, Is.EqualTo(resolveA));
        }
Exemplo n.º 14
0
        public void UseInstance_wont_replace_existing_typed_registration_instead_will_append_to_it_In_presence_or_keyed_registration()
        {
            var container = new Container();

            container.Register <ServiceB>();

            container.Register <DependencyA>(serviceKey: "other");
            container.Register <DependencyA>();

            var      dependencyA = new DependencyA();
            ServiceB service;

            using (var scope = container.OpenScope())
            {
                scope.Use(dependencyA);
                service = scope.Resolve <ServiceB>();
                Assert.AreSame(dependencyA, service.Dep);
            }

            service = container.Resolve <ServiceB>();
            Assert.IsNotNull(service.Dep);
            Assert.AreNotSame(dependencyA, service.Dep);
        }
Exemplo n.º 15
0
        public void Singleton_service_should_consume_singleton_instance_despite_presence_of_scoped_instance()
        {
            var container = new Container();

            container.Register <ServiceB>(Reuse.Singleton);

            var singletonDep = new DependencyA();

            container.Use(singletonDep);

            using (var scope = container.OpenScope())
            {
                var scopedDep = new DependencyA();
                scope.Use(scopedDep);

                var service = scope.Resolve <ServiceB>();
                Assert.AreSame(singletonDep, service.Dep);
            }

            var anotherBB = container.Resolve <ServiceB>();

            Assert.AreSame(singletonDep, anotherBB.Dep);
        }
Exemplo n.º 16
0
        public void The_used_instance_dependency_should_be_independent_of_scope()
        {
            var container = new Container();

            container.Register <ServiceB>();

            var singletonDep = new DependencyA();

            container.Use(singletonDep);

            using (var scope = container.OpenScope())
            {
                var scopedDep = new DependencyA();
                scope.Use(scopedDep);

                var service = scope.Resolve <ServiceB>();
                Assert.AreSame(scopedDep, service.Dep);
            }

            var anotherBB = container.Resolve <ServiceB>();

            Assert.AreSame(singletonDep, anotherBB.Dep);
        }
Exemplo n.º 17
0
        public void UseInstance_wont_replace_existing_typed_registration_instead_will_append_to_it()
        {
            var container = new Container();

            container.Register <ServiceB>();

            container.Register <DependencyA>();

            var      dependencyA = new DependencyA();
            ServiceB service;

            using (var scope = container.OpenScope())
            {
                scope.Use(dependencyA); // what does it mean for the typed DependencyA

                service = scope.Resolve <ServiceB>();
                Assert.AreSame(dependencyA, service.Dep);
            }

            service = container.Resolve <ServiceB>();
            Assert.IsNotNull(service.Dep);
            Assert.AreNotSame(dependencyA, service.Dep);
        }
Exemplo n.º 18
0
 public Container(DependencyA depA, DependencyB depB)
 {
     this.depA = depA;
     this.depB = depB;
 }
Exemplo n.º 19
0
 public HomeController(DependencyA dependencyA)
 {
     this.dependencyA = dependencyA;
 }
        public void ExecutesAllDistinctDependencies()
        {
            var dependencyA = new DependencyA();
            var dependencyB = new DependencyB();

            var dependencyDefinitionA = new Dependency<IDependencyA>(bind => bind.ToConstant(dependencyA).InTransientScope());

            this.testee.Load(
                new FeatureA(
                    dependencyDefinitionA,
                    new Dependency<IDependencyB>(bind => bind.ToConstant(dependencyB).InSingletonScope())),
                new FeatureB(
                    dependencyDefinitionA,
                    new Dependency<IDependencyB>(bind => bind.ToConstant(dependencyB).InSingletonScope())),
                new FeatureC());

            A.CallTo(() => this.kernel.Bind<IDependencyA>()).MustHaveHappened();
            A.CallTo(() => this.kernel.Bind<IDependencyB>()).MustHaveHappened();
        }
        public void CollectsAllExtensionsAndLoadsThemDistinctBeforeLoadingModules()
        {
            var loadedModules = new List<INinjectModule>();

            A.CallTo(() => this.kernel.Load(A<IEnumerable<INinjectModule>>._))
                .Invokes((IEnumerable<INinjectModule> modules) => loadedModules.AddRange(modules));

            var dependencyA = new DependencyA();
            var dependencyB = new DependencyB();

            var dependencyDefinitionA = new Dependency<IDependencyA>(bind => bind.ToConstant(dependencyA).InTransientScope());
            var dependencyDefinitionB = new Dependency<IDependencyB>(bind => bind.ToConstant(dependencyB).InSingletonScope());

            this.testee.Load(
                new FeatureA(
                    dependencyDefinitionA,
                    dependencyDefinitionB),
                new FeatureB(
                    dependencyDefinitionA,
                    dependencyDefinitionB));

            loadedModules.Select(m => m.GetType().Name)
                .Should().ContainInOrder(
                    new[] 
                    {
                        typeof(ExtensionModuleA),
                        typeof(ExtensionModuleB),
                        typeof(ExtensionModuleC),

                        typeof(ModuleA),
                        typeof(ModuleB),
                        typeof(ModuleC),
                    }.Select(_ => _.Name))
                .And.HaveCount(6);
        }
Exemplo n.º 22
0
 public DependencyBImpl(DependencyA depA)
 {
     this.depA = depA;
 }
 public InjectedTestClassCommandTests(DependencyA dependency)
 {
     _dependency = dependency;
 }
Exemplo n.º 24
0
 public Container ProvidesContainer(DependencyA depA, DependencyB depB)
 {
     return Container = new Container(depA, depB);
 }
Exemplo n.º 25
0
 public Container(DependencyA depA, DependencyB depB)
 {
     this.depA = depA;
     this.depB = depB;
 }
 public TypeToBeActivated(DependencyA a, DependencyB b, DependencyC c)
 {
 }
Exemplo n.º 27
0
 public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB, DependencyC dependencyC)
 {
     _dependencyA = dependencyA;
     _dependencyB = dependencyB;
     _dependencyC = dependencyC;
 }
Exemplo n.º 28
0
 public Container ProvidesContainer(DependencyA depA, DependencyB depB)
 {
     return(Container = new Container(depA, depB));
 }
Exemplo n.º 29
0
 public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
 {
     _dependencyA = dependencyA;
     _dependencyB = dependencyB;
 }
Exemplo n.º 30
0
 public void HandleTestCommand(Ns06_TestCommand command)
 {
     command.Param = DependencyA.GetTestValue() == "A" &&
                     DependencyB == null &&
                     DependencyC != null ? 1 : 0;
 }
Exemplo n.º 31
0
 public ServiceB(DependencyA dep)
 {
     Dep = dep;
 }
Exemplo n.º 32
0
 public DependencyBImpl(DependencyA depA)
 {
     this.depA = depA;
 }