Ejemplo n.º 1
0
        public void ScopingEndToEndWithCompositionService_MatchingCatalogsShouldSucceed()
        {
            var c  = new TypeCatalog(typeof(ClassRoot), typeof(ClassA));
            var sd = c.AsScope(c.AsScope());

            var container = new CompositionContainer(sd, CompositionOptions.ExportCompositionService);

            var fromRoot = container.GetExportedValue <ClassRoot>();
            var a1       = fromRoot.classA.CreateExport().Value;
            var a2       = fromRoot.classA.CreateExport().Value;

            fromRoot.localClassA.InstanceValue = 101;
            a1.InstanceValue = 202;
            a2.InstanceValue = 303;

            if (a1.InstanceValue == a2.InstanceValue)
            {
                throw new Exception("Incorrect sharing, a1 is shared with a2");
            }

            var xroot = new ImportA();
            var x1    = new ImportA();
            var x2    = new ImportA();

            fromRoot.localClassA.CompositionService.SatisfyImportsOnce(xroot);
            a1.CompositionService.SatisfyImportsOnce(x1);
            a2.CompositionService.SatisfyImportsOnce(x2);
            Assert.AreEqual(xroot.classA.InstanceValue, fromRoot.localClassA.InstanceValue);
            Assert.AreEqual(x1.classA.InstanceValue, a1.InstanceValue);
            Assert.AreEqual(x2.classA.InstanceValue, a2.InstanceValue);
        }
        public void SimpleChainWithTwoChildren()
        {
            var parentCatalog = new TypeCatalog(typeof(BarWithMany));
            var childCatalog1 = new TypeCatalog(typeof(FooImpl));
            var childCatalog2 = new TypeCatalog(typeof(Foo2Impl));

            var scope     = parentCatalog.AsScope(childCatalog1.AsScope(), childCatalog2.AsScope());
            var container = new CompositionContainer(scope);

            var bar = container.GetExportedValue <IBarContract>() as BarWithMany;

            Assert.NotNull(bar);

            Assert.Equal(2, bar.FooFactories.Length);

            IFooContract foo1 = null;

            using (var efFoo1 = bar.FooFactories[0].CreateExport())
            {
                foo1 = efFoo1.Value;
            }

            IFooContract foo2 = null;

            using (var efFoo2 = bar.FooFactories[1].CreateExport())
            {
                foo2 = efFoo2.Value;
            }

            Assert.True(((foo1 is FooImpl) && (foo2 is Foo2Impl)) || ((foo2 is FooImpl) && (foo1 is Foo2Impl)));
        }
        public void ExportFactoryCausesRejectionBasedOnCardinality()
        {
            var parentCatalog = new TypeCatalog(typeof(BarImpl));
            var childCatalog  = new TypeCatalog(typeof(FooImpl), typeof(Foo2Impl));

            var scope     = parentCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope);

            var bar = container.GetExportedValueOrDefault <IBarContract>();

            Assert.Null(bar);
        }
        public void SimpleChainWithLowerLoopRejection()
        {
            var parentCatalog = new TypeCatalog(typeof(BarImpl));
            var childCatalog  = new TypeCatalog(typeof(Foo3Impl));

            var scope     = parentCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope);

            var bar = container.GetExportedValueOrDefault <IBarContract>();

            Assert.Null(bar);
        }
Ejemplo n.º 5
0
        public void DontExportICompositionServiceFromChildImportShouldShouldThrowCompositionException()
        {
            var rootCatalog  = new TypeCatalog(typeof(FromRoot));
            var childCatalog = new TypeCatalog(typeof(ClassRequiresICompositionService), typeof(ClassOptionallyImportsICompositionService));
            var scope        = rootCatalog.AsScope(childCatalog.AsScope());
            var container    = new CompositionContainer(scope);

            ExceptionAssert.Throws <ImportCardinalityMismatchException>(() =>
            {
                var fromRoot = container.GetExportedValue <FromRoot>();
                Assert.IsNull(fromRoot);
            });
        }
        public void SelfExportFromExportFactory_ShouldSucceed()
        {
            var cat1 = new TypeCatalog(typeof(ClassRoot));
            var cat2 = new TypeCatalog(typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(ClassD));
            var sd   = cat1.AsScope(cat2.AsScope());

            var container = new CompositionContainer(sd);

            var fromRoot = container.GetExportedValue <ClassRoot>();
            var a1       = fromRoot.classA.CreateExport().Value;

            a1.InstanceValue = 8;

            Assert.Equal(8, a1.classB.classA.InstanceValue);
            Assert.Equal(8, a1.classC.classA.InstanceValue);
            Assert.Equal(8, a1.classD.classA.InstanceValue);
        }
        public void FilteredScopeFactoryOfTM_ShouldSucceed()
        {
            var c1 = new TypeCatalog(typeof(ClassRoot), typeof(ClassA));
            var c2 = new TypeCatalog(typeof(ClassA), typeof(ClassB), typeof(ClassC));
            var c3 = new TypeCatalog(typeof(ClassA), typeof(ClassB), typeof(ClassC));
            var c4 = new TypeCatalog(typeof(ClassA), typeof(ClassB), typeof(ClassC));
            var sd = c1.AsScope(c2.AsScopeWithPublicSurface <ClassA>(),
                                c3.AsScopeWithPublicSurface <ClassB>(),
                                c4.AsScopeWithPublicSurface <ClassC>());

            var container = new CompositionContainer(sd);

            var fromRoot = container.GetExportedValue <ClassRoot>();
            var a        = fromRoot.classA.CreateExport().Value;
            var b        = fromRoot.classB.CreateExport().Value;
            var c        = fromRoot.classC.CreateExport().Value;
        }
Ejemplo n.º 8
0
        public void ExportICompositionServiceFromChildImportShouldShouldSucceed()
        {
            var childCatalog = new TypeCatalog(typeof(ClassRequiresICompositionService), typeof(ClassOptionallyImportsICompositionService));
            var rootCatalog  = new TypeCatalog(typeof(FromRoot));
            CompositionScopeDefinition scope = rootCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope, CompositionOptions.ExportCompositionService);

            FromRoot fromRoot = container.GetExportedValue <FromRoot>();

            ExportLifetimeContext <ClassRequiresICompositionService> requiredService = fromRoot.Required.CreateExport();

            Assert.NotNull(requiredService.Value.CompositionService);

            ExportLifetimeContext <ClassOptionallyImportsICompositionService> optionalService = fromRoot.Optional.CreateExport();

            Assert.NotNull(optionalService.Value.CompositionService);
        }
        public void DeeplyNestedCatalogOverlappedCatalog_ShouldWork()
        {
            var cat1 = new TypeCatalog(typeof(ClassRoot), typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(ClassD));
            var cat2 = cat1;
            var cat3 = cat1;
            var cat4 = cat1;
            var cat5 = cat1;
            var sd   = cat1.AsScope(cat2.AsScope(cat3.AsScope(cat4.AsScope(cat5.AsScope()))));

            var container = new CompositionContainer(sd);

            var fromRoot = container.GetExportedValue <ClassRoot>();

            var a1 = fromRoot.classA.CreateExport().Value;
            var b1 = a1.classB.CreateExport().Value;
            var c1 = b1.classC.CreateExport().Value;
            var d1 = c1.classD.CreateExport().Value;
        }
        public void ScopeBridgingAdaptersImportExportProperty_ShouldSucceed()
        {
            var cat1      = new TypeCatalog(typeof(ClassRoot), typeof(ClassXXXX));
            var cat2      = new TypeCatalog(typeof(ClassC), typeof(ClassD));
            var sd        = cat1.AsScope(cat2.AsScope());
            var container = new CompositionContainer(sd);

            var fromRoot = container.GetExportedValue <ClassRoot>();
            var c1       = fromRoot.classC.CreateExport().Value;
            var c2       = fromRoot.classC.CreateExport().Value;
            var c3       = fromRoot.classC.CreateExport().Value;
            var c4       = fromRoot.classC.CreateExport().Value;
            var c5       = fromRoot.classC.CreateExport().Value;

            Assert.Equal(0, fromRoot.xxxx.InstanceValue);
            Assert.Equal(0, c1.xxxx.InstanceValue);
            Assert.Equal(0, c1.classD.xxxx.InstanceValue);
            Assert.Equal(0, c2.xxxx.InstanceValue);
            Assert.Equal(0, c2.classD.xxxx.InstanceValue);
            Assert.Equal(0, c3.xxxx.InstanceValue);
            Assert.Equal(0, c3.classD.xxxx.InstanceValue);
            Assert.Equal(0, c4.xxxx.InstanceValue);
            Assert.Equal(0, c4.classD.xxxx.InstanceValue);
            Assert.Equal(0, c5.xxxx.InstanceValue);
            Assert.Equal(0, c5.classD.xxxx.InstanceValue);

            c1.xxxx.InstanceValue = 1;
            c2.xxxx.InstanceValue = 2;
            c3.xxxx.InstanceValue = 3;
            c4.xxxx.InstanceValue = 4;
            c5.xxxx.InstanceValue = 5;

            Assert.Equal(0, fromRoot.xxxx.InstanceValue);
            Assert.Equal(1, c1.xxxx.InstanceValue);
            Assert.Equal(1, c1.classD.xxxx.InstanceValue);
            Assert.Equal(2, c2.xxxx.InstanceValue);
            Assert.Equal(2, c2.classD.xxxx.InstanceValue);
            Assert.Equal(3, c3.xxxx.InstanceValue);
            Assert.Equal(3, c3.classD.xxxx.InstanceValue);
            Assert.Equal(4, c4.xxxx.InstanceValue);
            Assert.Equal(4, c4.classD.xxxx.InstanceValue);
            Assert.Equal(5, c5.xxxx.InstanceValue);
            Assert.Equal(5, c5.classD.xxxx.InstanceValue);
        }
Ejemplo n.º 11
0
        public void ExportICompositionServiceFromChildImportShouldShouldSucceed()
        {
            var childCatalog = new TypeCatalog(typeof(ClassRequiresICompositionService), typeof(ClassOptionallyImportsICompositionService));
            var rootCatalog  = new TypeCatalog(typeof(FromRoot));
            var scope        = rootCatalog.AsScope(childCatalog.AsScope());
            var container    = new CompositionContainer(scope, CompositionOptions.ExportCompositionService);

            var fromRoot = container.GetExportedValue <FromRoot>();

            var requiredService = fromRoot.Required.CreateExport();

            Console.WriteLine("requiredService: {0}", requiredService.Value.CompositionService);
            Assert.IsNotNull(requiredService.Value.CompositionService);

            var optionalService = fromRoot.Optional.CreateExport();

            Console.WriteLine("optionalService: {0}", optionalService.Value.CompositionService);
            Assert.IsNotNull(optionalService.Value.CompositionService);
        }
Ejemplo n.º 12
0
        public void FilteredScopeFactoryOfT_ShouldSucceed()
        {
            var c1 = new TypeCatalog(typeof(ClassRoot), typeof(ClassA));
            var c2 = new TypeCatalog(typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(ClassD));
            var sd = c1.AsScope(c2.AsScope());

            var container = new CompositionContainer(sd);

            var fromRoot = container.GetExportedValue <ClassRoot>();

            fromRoot.classA.ValueToFilter = "IncludeInFirstInstance";
            var a1 = fromRoot.classA.CreateExport().Value;

            fromRoot.classA.ValueToFilter = "IncludeInSubsequentInstances";
            var a2 = fromRoot.classA.CreateExport().Value;

            Assert.AreEqual(fromRoot.localClassA.filteredTypes.Count(), 0);
            Assert.AreEqual(a1.filteredTypes.Count(), 2);
            Assert.AreEqual(a2.filteredTypes.Count(), 1);
        }
        public void ScopeFactoryAutoResolveFromAncestorScopeShouldSucceed()
        {
            var c1 = new TypeCatalog(typeof(ClassRoot), typeof(ClassA), typeof(Root));
            var c2 = new TypeCatalog(typeof(ClassRoot), typeof(ClassA), typeof(Child));
            var sd = c1.AsScope(c2.AsScope());

            var container = new CompositionContainer(sd, CompositionOptions.ExportCompositionService);

            var fromRoot = container.GetExportedValue <ClassRoot>();
            var a1       = fromRoot.classA.CreateExport().Value;
            var a2       = fromRoot.classA.CreateExport().Value;

            fromRoot.localClassA.InstanceValue = 101;
            a1.InstanceValue = 202;
            a2.InstanceValue = 303;

            Assert.NotEqual(a1.InstanceValue, a2.InstanceValue);
            Assert.NotNull(fromRoot.localClassA.classRoot);
            Assert.NotNull(a1.classRoot);
            Assert.NotNull(a2.classRoot);
        }
        public void LocalSharedNonLocalInSameContainer_ShouldSucceed()
        {
            var cat1 = new TypeCatalog(typeof(ClassRoot), typeof(ClassXXXX));
            var cat2 = new TypeCatalog(typeof(ClassA));
            var cat3 = new TypeCatalog(typeof(ClassB));
            var cat4 = new TypeCatalog(typeof(ClassC), typeof(ClassD), typeof(ClassXXXX));
            var sd   = cat1.AsScope(cat2.AsScope(cat3.AsScope(cat4.AsScope())));

            var container = new CompositionContainer(sd);

            var fromRoot = container.GetExportedValue <ClassRoot>();
            var a1       = fromRoot.classA.CreateExport().Value;

            fromRoot.xxxx.InstanceValue = 16;
            var b1 = a1.classB.CreateExport().Value;
            var c1 = b1.classC.CreateExport().Value;

            Assert.Equal(16, fromRoot.xxxx.InstanceValue);
            Assert.Equal(16, a1.xxxx.InstanceValue);
            Assert.Equal(16, b1.xxxx.InstanceValue);
            Assert.Equal(16, c1.xxxx.InstanceValue);
            Assert.Equal(0, c1.classD.xxxx.InstanceValue);

            c1.xxxx.InstanceValue = 8;

            Assert.Equal(8, fromRoot.xxxx.InstanceValue);
            Assert.Equal(8, a1.xxxx.InstanceValue);
            Assert.Equal(8, b1.xxxx.InstanceValue);
            Assert.Equal(8, c1.xxxx.InstanceValue);
            Assert.Equal(0, c1.classD.xxxx.InstanceValue);

            c1.classD.xxxx.InstanceValue = 2;
            Assert.Equal(8, fromRoot.xxxx.InstanceValue);
            Assert.Equal(8, a1.xxxx.InstanceValue);
            Assert.Equal(8, b1.xxxx.InstanceValue);
            Assert.Equal(8, c1.xxxx.InstanceValue);
            Assert.Equal(2, c1.classD.xxxx.InstanceValue);
        }
        public void SimpleChainWithCrossLoop()
        {
            var parentCatalog = new TypeCatalog(typeof(BarImpl));
            var childCatalog  = new TypeCatalog(typeof(Foo4Impl));

            var scope     = parentCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope);

            var bar = container.GetExportedValue <IBarContract>();

            Assert.NotNull(bar);

            var foo1 = bar.CreateFoo();

            Assert.NotNull(foo1);
            Assert.True(foo1 is Foo4Impl);

            var foo2 = bar.CreateFoo();

            Assert.NotNull(foo1);
            Assert.True(foo2 is Foo4Impl);

            Assert.NotEqual(foo1, foo2);
        }