public void WhenProvidingDynamicValue_ThenBindingCanResolveIt()
            {
                using (var context = this.binding.CreateDynamicContext())
                {
                    // Without the dynamic context.
                    var result = this.binding.Evaluate();

                    Assert.True(result);
                    Assert.Null(this.binding.Value.Bar);

                    // With the dynamic context.
                    var bar = new Bar();
                    context.AddExport<IBar>(bar);
                    result = this.binding.Evaluate(context);

                    Assert.True(result);
                    Assert.NotNull(this.binding.Value.Bar);
                }
            }
            public void WhenDisposingDynamicContext_ThenResolvedValueRemainsValid()
            {
                var context = this.binding.CreateDynamicContext();

                // With the dynamic context.
                var bar = new Bar();
                context.AddExport<IBar>(bar);
                var result = this.binding.Evaluate(context);

                Assert.True(result);
                Assert.NotNull(this.binding.Value.Bar);

                context.Dispose();

                Assert.NotNull(this.binding.Value.Bar);
            }
            public void WhenCompositionServiceProvidesExportProvider_ThenChainsDynamicContextWithIt()
            {
                var catalog = new TypeCatalog(typeof(Foo));
                var container = new CompositionContainer(catalog);
                var compositionService = new Mock<INuPatternCompositionService>();
                compositionService.Setup(x => x.GetExportedValue<ExportProvider>()).Returns(container);
                compositionService.Setup(x => x.GetExports<IFoo, IComponentMetadata>())
                    .Returns(new[]
                    {
                        new Lazy<IFoo, IComponentMetadata>(
                        () => new Foo(),
                        Mocks.Of<IComponentMetadata>().First(m =>
                            m.CatalogName == Catalog.DefaultCatalogName &&
                            m.Id == "Foo"))
                    });

                var binding = new DynamicBinding<IFoo>(new DelegatingCompositionService(compositionService.Object), "Foo");

                // Make sure the mocking so far is going good.
                Assert.True(binding.Evaluate());

                using (var context = binding.CreateDynamicContext())
                {
                    var bar = new Bar();
                    context.AddExport<IBar>(bar);
                    var result = binding.Evaluate(context);

                    Assert.True(result);
                    Assert.NotNull(binding.Value.Bar);
                }
            }
            public void WhenDisposingDynamicContextAndReevaluating_ThenResolvedValueBecomesInvalid()
            {
                using (var context = this.binding.CreateDynamicContext())
                {
                    var bar = new Bar();
                    context.AddExport<IBar>(bar);
                    var result = this.binding.Evaluate(context);

                    Assert.True(result);
                    Assert.NotNull(this.binding.Value.Bar);
                }

                this.binding.Evaluate();
                Assert.Null(this.binding.Value.Bar);
            }
            public void WhenAddedExportExistsOnBase_ThenOverridesIt()
            {
                var catalog = new TypeCatalog(typeof(Foo), typeof(Bar));
                var container = new CompositionContainer(catalog);
                var compositionService = new NuPatternCompositionService(container);

                var binding = new DynamicBinding<IFoo>(new DelegatingCompositionService(compositionService), "Foo");
                Assert.True(binding.Evaluate());
                Assert.NotNull(binding.Value.Bar);

                using (var context = binding.CreateDynamicContext())
                {
                    var bar = new Bar();
                    context.AddExport<IBar>(bar);
                    var result = binding.Evaluate(context);

                    Assert.True(result);
                    Assert.NotNull(binding.Value.Bar);
                    Assert.Same(bar, binding.Value.Bar);
                }
            }