public void Constructor_NullCatalog_ShowThrowNullArgument()
 {
     var ex = ExceptionAssert.Throws <ArgumentNullException>(RetryMode.DoNotRetry, () =>
     {
         CompositionScopeDefinition scope = new CompositionScopeDefinition(null, Enumerable.Empty <CompositionScopeDefinition>());
     });
 }
        public CatalogExportProvider(ComposablePartCatalog catalog, CompositionOptions compositionOptions)
        {
            Requires.NotNull(catalog, nameof(catalog));
            if (compositionOptions > (CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe | CompositionOptions.ExportCompositionService))
            {
                throw new ArgumentOutOfRangeException(nameof(compositionOptions));
            }

            _catalog            = catalog;
            _compositionOptions = compositionOptions;
            var notifyCatalogChanged = _catalog as INotifyComposablePartCatalogChanged;

            if (notifyCatalogChanged != null)
            {
                notifyCatalogChanged.Changing += OnCatalogChanging;
            }

            CompositionScopeDefinition scopeDefinition = _catalog as CompositionScopeDefinition;

            if (scopeDefinition != null)
            {
                _innerExportProvider = new AggregateExportProvider(new ScopeManager(this, scopeDefinition), new InnerCatalogExportProvider(this));
            }
            else
            {
                _innerExportProvider = new InnerCatalogExportProvider(this);
            }
            _lock = new CompositionLock(compositionOptions.HasFlag(CompositionOptions.IsThreadSafe));
            _disableSilentRejection = compositionOptions.HasFlag(CompositionOptions.DisableSilentRejection);
        }
                protected override object GetExportedValueCore()
                {
                    if (this._export == null)
                    {
                        // Need to create a new scopedefinition that is filtered by the ExportProvider
                        var filteredScopeDefinition = new CompositionScopeDefinition(
                            new FilteredCatalog(this._scopeFactoryExport._catalog, this._catalogFilter), 
                            this._scopeFactoryExport._catalog.Children);
                        var childContainer = this._scopeFactoryExport._scopeManager.CreateChildContainer(filteredScopeDefinition);

                        var export = childContainer.CatalogExportProvider.CreateExport(this._scopeFactoryExport.UnderlyingPartDefinition, this._scopeFactoryExport.UnderlyingExportDefinition, false, CreationPolicy.Any);
                        lock (this._lock)
                        {
                            if (this._export == null)
                            {
                                this._childContainer = childContainer;
                                Thread.MemoryBarrier();
                                this._export = export;

                                childContainer = null;
                                export = null;
                            }
                        }
                        if (childContainer != null)
                        {
                            childContainer.Dispose();
                        }
                    }

                    return this._export.Value;
                }
        public void Dispose()
        {
            var         parts   = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2)).Parts;
            var         exports = parts.Select(p => Tuple.Create(p, p.ExportDefinitions.First()));
            TestCatalog catalog = new TestCatalog(
                () => parts,
                () => exports);
            var import = parts.SelectMany(p => p.ImportDefinitions).First();

            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null);

            scope.Dispose();
            var ex = ExceptionAssert.Throws <ObjectDisposedException>(RetryMode.DoNotRetry, () =>
            {
                var ps = scope.Parts;
            });

            ex = ExceptionAssert.Throws <ObjectDisposedException>(RetryMode.DoNotRetry, () =>
            {
                var es = scope.GetExports(import);
            });

            scope.Dispose();
            ex = ExceptionAssert.Throws <ObjectDisposedException>(RetryMode.DoNotRetry, () =>
            {
                var ps = scope.Parts;
            });

            ex = ExceptionAssert.Throws <ObjectDisposedException>(RetryMode.DoNotRetry, () =>
            {
                var es = scope.GetExports(import);
            });
        }
                protected override object GetExportedValueCore()
                {
                    if (this._export == null)
                    {
                        // Need to create a new scopedefinition that is filtered by the ExportProvider
                        var filteredScopeDefinition = new CompositionScopeDefinition(
                            new FilteredCatalog(this._scopeFactoryExport._catalog, this._catalogFilter),
                            this._scopeFactoryExport._catalog.Children);
                        var childContainer = this._scopeFactoryExport._scopeManager.CreateChildContainer(filteredScopeDefinition);

                        var export = childContainer.CatalogExportProvider.CreateExport(this._scopeFactoryExport.UnderlyingPartDefinition, this._scopeFactoryExport.UnderlyingExportDefinition, false, CreationPolicy.Any);
                        lock (this._lock)
                        {
                            if (this._export == null)
                            {
                                this._childContainer = childContainer;
                                Thread.MemoryBarrier();
                                this._export = export;

                                childContainer = null;
                                export         = null;
                            }
                        }
                        if (childContainer != null)
                        {
                            childContainer.Dispose();
                        }
                    }

                    return(this._export.Value);
                }
        public void Constructor_NullChildren()
        {
            TypeCatalog catalog = new TypeCatalog(typeof(FooImpl));
            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null);

            Assert.NotNull(scope.Children);
            Assert.Equal(0, scope.Children.Count());
        }
        public void Constructor_PublicSurface_MultipleExportsPerPart()
        {
            var catalog = new TypeCatalog(typeof(FooImpl4));
            var exports = catalog.Parts.SelectMany(p => p.ExportDefinitions);
            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null, exports);

            Assert.Equal(3, scope.PublicSurface.Count());
        }
            public ScopeManager(CatalogExportProvider catalogExportProvider, CompositionScopeDefinition scopeDefinition)
            {
                ArgumentNullException.ThrowIfNull(catalogExportProvider);
                ArgumentNullException.ThrowIfNull(scopeDefinition);

                _scopeDefinition       = scopeDefinition;
                _catalogExportProvider = catalogExportProvider;
            }
            public ScopeManager(CatalogExportProvider catalogExportProvider, CompositionScopeDefinition scopeDefinition)
            {
                Assumes.NotNull(catalogExportProvider);
                Assumes.NotNull(scopeDefinition);

                _scopeDefinition       = scopeDefinition;
                _catalogExportProvider = catalogExportProvider;
            }
            public ScopeManager(CatalogExportProvider catalogExportProvider, CompositionScopeDefinition scopeDefinition)
            {
                Assumes.NotNull(catalogExportProvider);
                Assumes.NotNull(scopeDefinition);

                this._scopeDefinition = scopeDefinition;
                this._catalogExportProvider = catalogExportProvider;
            }
        public void Constructor_PublicSurface()
        {
            var catalog = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2));
            var exports = catalog.Parts.Select(p => p.ExportDefinitions.First());
            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null, exports);

            Assert.Equal(catalog.Parts.Count(), scope.Parts.Count());
            Assert.Equal(exports.Count(), scope.PublicSurface.Count());
        }
        public void SimpleComposition()
        {
            var catalog   = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2));
            var scope     = new CompositionScopeDefinition(catalog, null);
            var container = new CompositionContainer(scope);

            var foos = container.GetExportedValues <IFooContract>();

            Assert.Equal(3, foos.Count());
        }
Example #13
0
        public void Parts_DelegateToCatalog()
        {
            var         parts   = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2)).Parts;
            var         exports = parts.Select(p => Tuple.Create(p, p.ExportDefinitions.First()));
            TestCatalog catalog = new TestCatalog(
                () => parts,
                () => exports);

            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null);

            EnumerableAssert.AreSequenceEqual(parts, scope.Parts);
        }
        public void Parts_DelegateToCatalog()
        {
            var         parts   = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2)).Parts;
            var         exports = parts.Select(p => Tuple.Create(p, p.ExportDefinitions.First()));
            TestCatalog catalog = new TestCatalog(
                () => parts,
                () => exports);

            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null);

            EqualityExtensions.CheckEquals(parts, scope.Parts);
        }
        public void GetExports_DelegateToCatalog()
        {
            var         parts   = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2)).Parts;
            var         exports = parts.Select(p => Tuple.Create(p, p.ExportDefinitions.First()));
            var         import  = parts.SelectMany(p => p.ImportDefinitions).First();
            TestCatalog catalog = new TestCatalog(
                () => parts,
                () => exports);

            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null);

            Assert.Same(exports, scope.GetExports(import));
        }
Example #16
0
            public ScopeManager(CatalogExportProvider catalogExportProvider, CompositionScopeDefinition scopeDefinition)
            {
                if (catalogExportProvider == null)
                {
                    throw new ArgumentNullException(nameof(catalogExportProvider));
                }
                if (scopeDefinition == null)
                {
                    throw new ArgumentNullException(nameof(scopeDefinition));
                }

                _scopeDefinition       = scopeDefinition;
                _catalogExportProvider = catalogExportProvider;
            }
Example #17
0
        public void Notifications()
        {
            var         parts   = new TypeCatalog(typeof(FooImpl), typeof(FooImpl2), typeof(FooImpl2)).Parts;
            var         exports = parts.Select(p => Tuple.Create(p, p.ExportDefinitions.First()));
            TestCatalog catalog = new TestCatalog(
                () => parts,
                () => exports);

            CompositionScopeDefinition scope = new CompositionScopeDefinition(catalog, null);

            ComposablePartCatalogChangeEventArgs args = new ComposablePartCatalogChangeEventArgs(Enumerable.Empty <ComposablePartDefinition>(), Enumerable.Empty <ComposablePartDefinition>(), null);

            bool changedFired = false;

            scope.Changed += new EventHandler <ComposablePartCatalogChangeEventArgs>((o, e) =>
            {
                Assert.AreSame(args, e);
                Assert.AreSame(scope, o);
                changedFired = true;
            });

            bool changingFired = false;

            scope.Changing += new EventHandler <ComposablePartCatalogChangeEventArgs>((o, e) =>
            {
                Assert.AreSame(args, e);
                Assert.AreSame(scope, o);
                changingFired = true;
            });


            catalog.OnChanged(args);
            Assert.IsTrue(changedFired);

            catalog.OnChanging(args);
            Assert.IsTrue(changingFired);


            changedFired  = false;
            changingFired = false;

            scope.Dispose();

            catalog.OnChanged(args);
            catalog.OnChanging(args);

            Assert.IsFalse(changedFired);
            Assert.IsFalse(changingFired);
        }
        public void Constructor()
        {
            TypeCatalog catalog  = new TypeCatalog(typeof(FooImpl));
            TypeCatalog catalog1 = new TypeCatalog(typeof(FooImpl2));
            TypeCatalog catalog2 = new TypeCatalog(typeof(FooImpl3));

            CompositionScopeDefinition scope1 = new CompositionScopeDefinition(catalog1, Enumerable.Empty <CompositionScopeDefinition>());
            CompositionScopeDefinition scope2 = new CompositionScopeDefinition(catalog1, Enumerable.Empty <CompositionScopeDefinition>());
            CompositionScopeDefinition scope  = new CompositionScopeDefinition(catalog, new CompositionScopeDefinition[] { scope1, scope2 });

            Assert.NotNull(scope);
            Assert.NotNull(scope.Parts);
            Assert.Equal(1, scope.Parts.Count());
            Assert.Equal(2, scope.Children.Count());
            Assert.Same(scope1, scope.Children.ToArray()[0]);
            Assert.Same(scope2, scope.Children.ToArray()[1]);
        }
 private Export CreateScopeExport(CompositionScopeDefinition childCatalog, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition)
 {
     return(new ScopeFactoryExport(this, childCatalog, partDefinition, exportDefinition));
 }
 private Export CreateScopeExport(CompositionScopeDefinition childCatalog, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition)
 {
     return new ScopeFactoryExport(this, childCatalog, partDefinition, exportDefinition);
 }
        public CompositionScopeDefinitionDebuggerProxy(CompositionScopeDefinition compositionScopeDefinition) 
        {
            Requires.NotNull(compositionScopeDefinition, "compositionScopeDefinition");

            this._compositionScopeDefinition = compositionScopeDefinition;
        }
Example #22
0
 internal ScopeFactoryExport(ScopeManager scopeManager, CompositionScopeDefinition catalog, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition) :
     base(partDefinition, exportDefinition)
 {
     _scopeManager = scopeManager;
     _catalog      = catalog;
 }
        public CompositionScopeDefinitionDebuggerProxy(CompositionScopeDefinition compositionScopeDefinition)
        {
            Requires.NotNull(compositionScopeDefinition, nameof(compositionScopeDefinition));

            _compositionScopeDefinition = compositionScopeDefinition;
        }
        public CompositionScopeDefinitionDebuggerProxy(CompositionScopeDefinition compositionScopeDefinition)
        {
            Requires.NotNull(compositionScopeDefinition, "compositionScopeDefinition");

            this._compositionScopeDefinition = compositionScopeDefinition;
        }
 internal ScopeFactoryExport(ScopeManager scopeManager, CompositionScopeDefinition catalog, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition) :
     base(partDefinition, exportDefinition)
 {
     this._scopeManager = scopeManager;
     this._catalog = catalog;
 }