Exemplo n.º 1
0
        public void WhenExportProvidedUpFront_ThenEvaluatesTrue()
        {
            var catalog            = new TypeCatalog(typeof(Foo), typeof(Bar));
            var container          = new CompositionContainer(catalog);
            var compositionService = new CompositionContainerService(container);

            var binding = new Binding <IFoo>(compositionService, "Foo");
            var result  = binding.Evaluate();

            Assert.IsTrue(result);
        }
Exemplo n.º 2
0
        public void WhenExportProvidedLater_ThenEvaluatesTrue()
        {
            // Note catalog now doesn't contain the IBar dependency.
            var catalog            = new TypeCatalog(typeof(Foo));
            var container          = new CompositionContainer(catalog);
            var compositionService = new CompositionContainerService(container);

            //// CompositionInfoTextFormatter.Write(new CompositionInfo(catalog, container), Console.Out);

            var binding = new Binding <IFoo>(compositionService, "Foo");
            var result  = binding.Evaluate();

            // As appropriate, this now evaluates to false.
            Assert.IsFalse(result);

            // To re-assure this is the proper behavior, the unresolved import causes the
            // component to be unavailable via a plain query:
            var lazyValue = container.GetExports <IFoo, IComponentMetadata>()
                            .FirstOrDefault(component => component.Metadata.Id == "Foo");

            Assert.IsNull(lazyValue);

            // Here we provide the missing export.
            container.ComposeExportedValue <IBar>(new Bar());

            // CompositionInfoTextFormatter.Write(new CompositionInfo(catalog, container), Console.Out);

            // Note how now the component becomes available via query, as
            // the dependency is now satisfied.
            lazyValue = container.GetExports <IFoo, IComponentMetadata>()
                        .FirstOrDefault(component => component.Metadata.Id == "Foo");
            Assert.IsNotNull(lazyValue);

            // And re-evaluate, which should now locate the component as
            // its imports are satisfied.
            result = binding.Evaluate();

            // But this fails :(
            Assert.IsTrue(result);
        }