Пример #1
0
        protected override IEnumerable<Export> GetExportsCore(
            ImportDefinition definition,
            AtomicComposition atomicComposition
        ) {

            return from kv in _exports
                   where definition.IsConstraintSatisfiedBy(kv.Key)
                   select kv.Value;
        }
 private static bool IsAffectedImport(ImportDefinition import, IEnumerable<ExportDefinition> changedExports)
 {
     // This could be more efficient still if the export definitions were indexed by contract name,
     // only worth revisiting if we need to squeeze more performance out of recomposition
     foreach (var export in changedExports)
     {
         if (import.IsConstraintSatisfiedBy(export))
         {
             return true;
         }
     }
    
     return false;
 }
Пример #3
0
        internal static bool IsProductConstraintSatisfiedBy(ImportDefinition productImportDefinition, ExportDefinition exportDefinition)
        {
            object productValue = null;
            if (exportDefinition.Metadata.TryGetValue(CompositionConstants.ProductDefinitionMetadataName, out productValue))
            {
                ExportDefinition productDefinition = productValue as ExportDefinition;

                if (productDefinition != null)
                {
                    return productImportDefinition.IsConstraintSatisfiedBy(productDefinition);
                }
            }

            return false;
        }
Пример #4
0
        internal virtual bool TryGetExports(ImportDefinition definition, out Tuple <ComposablePartDefinition, ExportDefinition>?singleMatch, out IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> >?multipleMatches)
        {
            singleMatch     = null;
            multipleMatches = null;

            List <Tuple <ComposablePartDefinition, ExportDefinition> >?multipleExports = null;
            Tuple <ComposablePartDefinition, ExportDefinition>?        singleExport    = null;
            bool matchesFound = false;

            foreach (var export in ExportDefinitions)
            {
                if (definition.IsConstraintSatisfiedBy(export))
                {
                    matchesFound = true;
                    if (singleExport == null)
                    {
                        singleExport = new Tuple <ComposablePartDefinition, ExportDefinition>(this, export);
                    }
                    else
                    {
                        if (multipleExports == null)
                        {
                            multipleExports = new List <Tuple <ComposablePartDefinition, ExportDefinition> >();
                            multipleExports.Add(singleExport);
                        }
                        multipleExports.Add(new Tuple <ComposablePartDefinition, ExportDefinition>(this, export));
                    }
                }
            }

            if (!matchesFound)
            {
                return(false);
            }

            if (multipleExports != null)
            {
                multipleMatches = multipleExports;
            }
            else
            {
                singleMatch = singleExport;
            }
            return(true);
        }
Пример #5
0
        internal virtual IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> > GetExports(ImportDefinition definition)
        {
            List <Tuple <ComposablePartDefinition, ExportDefinition> > exports = null;

            foreach (var export in this.ExportDefinitions)
            {
                if (definition.IsConstraintSatisfiedBy(export))
                {
                    if (exports == null)
                    {
                        exports = new List <Tuple <ComposablePartDefinition, ExportDefinition> >();
                    }
                    exports.Add(new Tuple <ComposablePartDefinition, ExportDefinition>(this, export));
                }
            }

            return(exports ?? _EmptyExports);
        }
            public override IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(
                ImportDefinition import)
            {
                var originalExports = this._originalCatalog.GetExports(import);
                var trimmedExports = originalExports.Where(partAndExport =>
                    !this._removedParts.ContainsKey(partAndExport.Item1));

                var addedExports = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();
                foreach (var part in this._addedParts)
                {
                    foreach (var export in part.ExportDefinitions)
                    {
                        if (import.IsConstraintSatisfiedBy(export))
                        {
                            addedExports.Add(new Tuple<ComposablePartDefinition, ExportDefinition>(part, export));
                        }
                    }
                }
                return trimmedExports.Concat(addedExports);
            }
Пример #7
0
        /// <summary>
        ///     Returns the export definitions that match the constraint defined by the specified definition.
        /// </summary>
        /// <param name="definition">
        ///     The <see cref="ImportDefinition"/> that defines the conditions of the
        ///     <see cref="ExportDefinition"/> objects to return.
        /// </param>
        /// <returns>
        ///     An <see cref="IEnumerable{T}"/> of <see cref="Tuple{T1, T2}"/> containing the
        ///     <see cref="ExportDefinition"/> objects and their associated
        ///     <see cref="ComposablePartDefinition"/> for objects that match the constraint defined
        ///     by <paramref name="definition"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="definition"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        ///     The <see cref="ComposablePartCatalog"/> has been disposed of.
        /// </exception>
        /// <remarks>
        ///     <note type="inheritinfo">
        ///         Overriders of this property should never return <see langword="null"/>, if no
        ///         <see cref="ExportDefinition"/> match the conditions defined by
        ///         <paramref name="definition"/>, return an empty <see cref="IEnumerable{T}"/>.
        ///     </note>
        /// </remarks>
        public virtual IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> > GetExports(ImportDefinition definition)
        {
            this.ThrowIfDisposed();

            Requires.NotNull(definition, "definition");

            var exports = new List <Tuple <ComposablePartDefinition, ExportDefinition> >();

            foreach (var part in this.Parts)
            {
                foreach (var export in part.ExportDefinitions)
                {
                    if (definition.IsConstraintSatisfiedBy(export))
                    {
                        exports.Add(new Tuple <ComposablePartDefinition, ExportDefinition>(part, export));
                    }
                }
            }
            return(exports);
        }
Пример #8
0
        /// <summary>
        ///     Returns the export definitions that match the constraint defined by the specified definition.
        /// </summary>
        /// <param name="definition">
        ///     The <see cref="ImportDefinition"/> that defines the conditions of the
        ///     <see cref="ExportDefinition"/> objects to return.
        /// </param>
        /// <returns>
        ///     An <see cref="IEnumerable{T}"/> of <see cref="Tuple{T1, T2}"/> containing the
        ///     <see cref="ExportDefinition"/> objects and their associated
        ///     <see cref="ComposablePartDefinition"/> for objects that match the constraint defined
        ///     by <paramref name="definition"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="definition"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        ///     The <see cref="ComposablePartCatalog"/> has been disposed of.
        /// </exception>
        /// <remarks>
        ///     <note type="inheritinfo">
        ///         Overriders of this property should never return <see langword="null"/>, if no
        ///         <see cref="ExportDefinition"/> match the conditions defined by
        ///         <paramref name="definition"/>, return an empty <see cref="IEnumerable{T}"/>.
        ///     </note>
        /// </remarks>
        public override IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
        {
            this.ThrowIfDisposed();

            IEnumerable<ComposablePartDefinition> candidateParts = this.GetCandidateParts(definition);
            if (candidateParts == null)
            {
                return Enumerable.Empty<Tuple<ComposablePartDefinition, ExportDefinition>>();
            }

            var exports = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();
            foreach (var part in candidateParts)
            {
                foreach (var export in part.ExportDefinitions)
                {
                    if (definition.IsConstraintSatisfiedBy(export))
                    {
                        exports.Add(new Tuple<ComposablePartDefinition, ExportDefinition>(part, export));
                    }
                }
            }

            return exports;
        }
Пример #9
0
        public virtual IEnumerable<Tuple2<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
        {
            this.ThrowIfDisposed();

            Requires.NotNull(definition, "definition");

            var exports = new List<Tuple2<ComposablePartDefinition, ExportDefinition>>();
            foreach (var part in this.Parts)
            {
                foreach (var export in part.ExportDefinitions)
                {
                    if (definition.IsConstraintSatisfiedBy(export))
                    {
                        exports.Add(new Tuple2<ComposablePartDefinition, ExportDefinition>(part, export));
                    }
                }
            }
            return exports;

        }
Пример #10
0
        internal IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExportsFromPublicSurface(ImportDefinition definition)
        {
            Assumes.NotNull(definition, "definition");

            var exports = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();

            foreach(var exportDefinition in this.PublicSurface)
            {
                if (definition.IsConstraintSatisfiedBy(exportDefinition))
                {
                    foreach (var export in this.GetExports(definition))
                    {
                        if(export.Item2 == exportDefinition)
                        {
                            exports.Add(export);
                            break;
                        }
                    }
                }
            }
            return exports;
        }
        /// <summary>
        /// Returns the exports in the catalog that match a given definition of an import.
        /// This method is called every time MEF tries to satisfy an import.
        ///</summary>
        public override IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition importDef)
        {
            // If ImportMany is defined and we are at design-time the use the standard bahavior and return
            // all matching exports.
            if (importDef.Cardinality == ImportCardinality.ZeroOrMore && DesignTime)
            {
                return base.GetExports(importDef);
            }

            //otherwise we have to do our own logic
            IList<Tuple<ComposablePartDefinition, ExportDefinition>> result
                = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();

            // Walk through all parts in that catalog...
            foreach (ComposablePartDefinition partDef in Parts)
            {
                // ... and for each part, examine if any export definition matches the
                // requested import definition.
                foreach (ExportDefinition exportDef in partDef.ExportDefinitions)
                {
                    if (importDef.IsConstraintSatisfiedBy(exportDef))
                    {
                        //ok the import definition is satisfied
                        var matchingExport = new Tuple<ComposablePartDefinition, ExportDefinition>(partDef, exportDef);
                        object designTimeMetadata;
                        exportDef.Metadata.TryGetValue("DesignTime", out designTimeMetadata);
                        //if DesignTimeAttribute is set then ToBool returns the assigend value
                        //ohterwise it returns false 
                        bool hasDesignTimeAttribute = ToBool(designTimeMetadata);

                        //If ImportMany is defined and we are at run-time then filter out
                        //design-time exports
                        if (importDef.Cardinality == ImportCardinality.ZeroOrMore)
                        {
                            if (DesignTime || !hasDesignTimeAttribute)
                                result.Add(matchingExport);
                        }
                        //If Import or Import(AllowDefault=true) then prioritize design-time exports
                        //at design-time
                        else
                        {
                            if (DesignTime)
                            {
                                if (result.Count == 0) //also allow run-time exports at design-time
                                    result.Add(matchingExport);
                                else if (hasDesignTimeAttribute) //but prioritize design time data at design time
                                {
                                    result.Clear();
                                    result.Add(matchingExport);
                                }
                            }
                            else
                            {
                                if (!hasDesignTimeAttribute) //only allow run-time exports at run-time
                                    result.Add(matchingExport);
                            }
                        }
                    }
                }
            }
            return result;
        }
Пример #12
0
 protected override IEnumerable<Export> GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)
 {
     return Cache.Where(export => definition.IsConstraintSatisfiedBy(export.Definition));
 }
Пример #13
0
        internal virtual IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
        {
            List<Tuple<ComposablePartDefinition, ExportDefinition>> exports = null;
            foreach (var export in this.ExportDefinitions)
            {
                if (definition.IsConstraintSatisfiedBy(export))
                {
                    if (exports == null)
                    {
                        exports = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();
                    }
                    exports.Add(new Tuple<ComposablePartDefinition, ExportDefinition>(this, export));
                }
            }

            return exports ?? _EmptyExports;
        }
Пример #14
0
        public void EmptyContractName_ShouldMatchAllContractNames()
        {
            var importDefinition = new ImportDefinition(ed => true, string.Empty, ImportCardinality.ZeroOrMore, false, false);

            var shouldMatch1 = new ExportDefinition("contract1", null);
            var shouldMatch2 = new ExportDefinition("contract2", null);

            Assert.IsTrue(importDefinition.IsConstraintSatisfiedBy(shouldMatch1));
            Assert.IsTrue(importDefinition.IsConstraintSatisfiedBy(shouldMatch2));
        }
Пример #15
0
        public void ContractName_ShouldBeIncludedInConstraintAutomatically()
        {
            string testContractName = "TestContractName";
            var contractImportDefinition = new ImportDefinition(ed => true, testContractName, ImportCardinality.ZeroOrMore, false, false);

            var shouldMatch = new ExportDefinition(testContractName, null);
            var shouldNotMatch = new ExportDefinition(testContractName + testContractName, null);

            Assert.IsTrue(contractImportDefinition.IsConstraintSatisfiedBy(shouldMatch));
            Assert.IsFalse(contractImportDefinition.IsConstraintSatisfiedBy(shouldNotMatch));
        }