Exemple #1
0
 internal static ComposablePartDefinition CreateComposablePartDefinition(SerializableComposablePartDefinition serializableComposablePartDefinition)
 {
     return(ReflectionModelServices.CreatePartDefinition(
                new Lazy <Type>(() => Type.GetType(serializableComposablePartDefinition.PartTypeAssemblyQualifiedName)),
                serializableComposablePartDefinition.IsDisposalRequired,
                new Lazy <IEnumerable <ImportDefinition> >(() => serializableComposablePartDefinition.ImportDefinitions.Select(CreateImportDefinition)),
                new Lazy <IEnumerable <ExportDefinition> >(() => serializableComposablePartDefinition.ExportDefinitions.Select(CreateExportDefinition)),
                new Lazy <IDictionary <string, object> >(() => serializableComposablePartDefinition.Metadata),
                null)); // TODO: Is it OK to have null for origin?
 }
 /// <summary>
 /// Create a <see cref="ComposablePartDefinition"/> for a specified type using the provided <see cref="IPartConvention"/>.
 /// </summary>
 /// <param name="convention">The <see cref="IPartConvention"/> instance which is used to create the <see cref="ComposablePartDefinition"/>.</param>
 /// <param name="type">The <see cref="Type"/> for which the <see cref="ComposablePartDefinition"/> should be created.</param>
 /// <returns>A <see cref="ComposablePartDefinition"/> instance.</returns>
 private ComposablePartDefinition CreatePartDefinition(IPartConvention convention, Type type)
 {
     return(ReflectionModelServices.CreatePartDefinition(
                new Lazy <Type>(() => type),
                false,
                new Lazy <IEnumerable <ImportDefinition> >(() => CreateImportDefinitions(convention.Imports, type)),
                new Lazy <IEnumerable <ExportDefinition> >(() => CreateExportDefinitions(convention.Exports, type)),
                new Lazy <IDictionary <string, object> >(() => GetPartDefinitionMetadata(convention)),
                null));
 }
 private static ComposablePartDefinition CreatePartDefinition(Type type, IList <ImportDefinition> imports, IList <ExportDefinition> exports, IDictionary <string, object> metadata)
 {
     return(ReflectionModelServices.CreatePartDefinition(
                new Lazy <Type>(() => type, LazyThreadSafetyMode.PublicationOnly),
                false,
                new Lazy <IEnumerable <ImportDefinition> >(() => imports),
                new Lazy <IEnumerable <ExportDefinition> >(() => exports),
                new Lazy <IDictionary <string, object> >(() => metadata),
                null));
 }
        public void RegisterType(Type implementation, Type contract, string key = null)
        {
            var part = ReflectionModelServices.CreatePartDefinition(
                new Lazy <Type>(() => implementation),
                false,
                new Lazy <IEnumerable <ImportDefinition> >(() => GetImportDefinitions(implementation)),
                new Lazy <IEnumerable <ExportDefinition> >(() => GetExportDefinitions(implementation, contract, key)),
                new Lazy <IDictionary <string, object> >(() => new Dictionary <string, object>()),
                null
                );

            parts.Add(part);
        }
            private ComposablePartDefinition CreateWrapped(ComposablePartDefinition partDefinition, Type type)
            {
                IEnumerable <ExportDefinition> exports = partDefinition.ExportDefinitions.Select(e => this.CreateWrapped(e, type)).ToArray();
                IEnumerable <ImportDefinition> imports = partDefinition.ImportDefinitions.Cast <ContractBasedImportDefinition>().Select(i => this.CreateWrapped(i, type)).ToArray();

                return(ReflectionModelServices.CreatePartDefinition(
                           this.CreateWrapped(ReflectionModelServices.GetPartType(partDefinition), type),
                           ReflectionModelServices.IsDisposalRequired(partDefinition),
                           imports.AsLazy(),
                           exports.AsLazy(),
                           partDefinition.Metadata.AsLazy(),
                           null));
            }
Exemple #6
0
 /// <summary>
 /// Clones the non-shared to avoid object instance reuse,
 /// which happens if you cache the part definition.
 /// </summary>
 private IEnumerable <ComposablePartDefinition> CloneNonSharedParts()
 {
     return(this.nonSharedParts
            .AsParallel()
            .Where(part => part != null)
            .Select(def => ReflectionModelServices.CreatePartDefinition(
                        ReflectionModelServices.GetPartType(def),
                        true,
                        new Lazy <IEnumerable <ImportDefinition> >(() => def.ImportDefinitions),
                        new Lazy <IEnumerable <ExportDefinition> >(() => def.ExportDefinitions),
                        new Lazy <IDictionary <string, object> >(() => def.Metadata),
                        this)));
 }
 private IEnumerable <ComposablePartDefinition> BuildParts(IQueryable <ComposablePartDefinition> parts)
 {
     return(parts.Select(def => ReflectionModelServices.CreatePartDefinition(
                             ReflectionModelServices.GetPartType(def),
                             true,
                             new Lazy <IEnumerable <ImportDefinition> >(() => def.ImportDefinitions),
                             new Lazy <IEnumerable <ExportDefinition> >(() => def.ExportDefinitions.Select(export =>
                                                                                                           ReflectionModelServices.CreateExportDefinition(
                                                                                                               ReflectionModelServices.GetExportingMember(export),
                                                                                                               export.ContractName,
                                                                                                               new Lazy <IDictionary <string, object> >(() => VisitExport(def, export)),
                                                                                                               this))),
                             new Lazy <IDictionary <string, object> >(() => VisitPart(def)),
                             this)));
 }
        /// <summary>
        /// 注册一个指定协定类型的实现类型。
        /// </summary>
        /// <param name="contractType">协定类型。</param>
        /// <param name="implType">实现类型。</param>
        /// <param name="replace">是否替换已有的部件定义。</param>
        /// <returns>当前的 <see cref="ConventionalCatalog"/> 对象。</returns>
        public ConventionalCatalog Register(Type contractType, Type implType, bool replace = true)
        {
            Guard.ArgumentNull(contractType, "contractType");
            Guard.ArgumentNull(implType, "implType");
            if (!IsRegisted(contractType) || replace)
            {
                var part = ReflectionModelServices.CreatePartDefinition(
                    new Lazy <Type>(() => implType),
                    false,
                    new Lazy <IEnumerable <ImportDefinition> >(() => GetImportDefinitions(implType)),
                    new Lazy <IEnumerable <ExportDefinition> >(() => GetExportDefinitions(implType, contractType)), null, null);
                partDictionary.AddOrReplace(contractType, part);
            }

            return(this);
        }
        private void ComposeDecoration()
        {
            if (myParts.Any())
            {
                return;
            }

            var contracts = new List <string>();

            foreach (var type in myDecoratorChain)
            {
                var originalPart = AttributedModelServices.CreatePartDefinition(type, null);

                var importDefs = originalPart.ImportDefinitions.ToList();

                if (type != myDecoratorChain.First())
                {
                    RewriteContract(type, importDefs, contracts.Last());
                }

                var exportDefs = originalPart.ExportDefinitions.ToList();

                if (type != myDecoratorChain.Last())
                {
                    contracts.Add(Guid.NewGuid().ToString());
                    RewriteContract(type, exportDefs, type, contracts.Last());
                }

                // as we pass it to lazy below we have to copy it to local variable - otherwise we create a closure with the loop iterator variable
                // and this will cause the actual part type to be changed
                var partType = type;
                var part     = ReflectionModelServices.CreatePartDefinition(
                    new Lazy <Type>(() => partType),
                    ReflectionModelServices.IsDisposalRequired(originalPart),
                    new Lazy <IEnumerable <ImportDefinition> >(() => importDefs),
                    new Lazy <IEnumerable <ExportDefinition> >(() => exportDefs),
                    new Lazy <IDictionary <string, object> >(() => new Dictionary <string, object>()),
                    null);

                myParts.Add(part);
            }

            // no add possible any longer
            myDecoratorChain = null;
        }
        public static ComposablePartDefinition ReadPartDefinition(IDictionary <string, object> cache, Func <ComposablePartDefinition, IEnumerable <ImportDefinition> > importsCreator, Func <ComposablePartDefinition, IEnumerable <ExportDefinition> > exportsCreator, Func <Assembly> assemblyLoader)
        {
            Assumes.NotNull(cache);

            Lazy <Type> partType = cache.ReadLazyTypeForPart(assemblyLoader);

            ComposablePartDefinition part = null;

            part = ReflectionModelServices.CreatePartDefinition(
                partType,
                cache.ReadValue <bool>(AttributedCacheServices.CacheKeys.IsDisposalRequired, false),
                LazyServices.MakeLazy(() => importsCreator(part)),
                LazyServices.MakeLazy(() => exportsCreator(part)),
                cache.ReadLazyMetadata(),
                null);

            return(part);
        }
Exemple #11
0
        private ComposablePartDefinition RewritePart(ComposablePartDefinition definition)
        {
            var closedGenericContractType = ContractType.MakeGenericType(ExportingType.GetGenericArguments());
            var closedGenericTypeIdentity = AttributedModelServices.GetTypeIdentity(closedGenericContractType);
            var openGenericTypeIdentity   = AttributedModelServices.GetTypeIdentity(ContractType);
            var openGenericContractName   = AttributedModelServices.GetContractName(ContractType);
            var exports             = new List <ExportDefinition>();
            var hasRewrittenExports = false;

            foreach (var exportDefinition in definition.ExportDefinitions)
            {
                // Rewrite only exports having open-generics type identity of the contract type we do care about
                if (openGenericTypeIdentity == (string)exportDefinition.Metadata[CompositionConstants.ExportTypeIdentityMetadataName])
                {
                    // If both open-generic contract name and the present contract name are equal,
                    // contract name has to be rewritten to form a closed-generic contract
                    var contractName = openGenericContractName == exportDefinition.ContractName
                                           ? AttributedModelServices.GetContractName(closedGenericContractType)
                                           : exportDefinition.ContractName;

                    // Preserve all the metadata except the type identity as it has to be rewritten
                    var metadata = new Dictionary <string, object>
                    {
                        { CompositionConstants.ExportTypeIdentityMetadataName, closedGenericTypeIdentity }
                    };
                    foreach (var key in exportDefinition.Metadata.Keys.Where(key => key != CompositionConstants.ExportTypeIdentityMetadataName))
                    {
                        metadata.Add(key, exportDefinition.Metadata[key]);
                    }

                    // Rewrite the export
                    var rewrittenExport = ReflectionModelServices.CreateExportDefinition(
                        ReflectionModelServices.GetExportingMember(exportDefinition),
                        contractName,
                        new Lazy <IDictionary <string, object> >(() => metadata),
                        exportDefinition as ICompositionElement);

                    exports.Add(rewrittenExport);
                    hasRewrittenExports = true;
                }
                else
                {
                    // This export is ok, copy the original
                    exports.Add(exportDefinition);
                }
            }

            // If the part has any rewritten exports, we have to rewrite the part itself
            if (hasRewrittenExports)
            {
                return(ReflectionModelServices.CreatePartDefinition(
                           ReflectionModelServices.GetPartType(definition),
                           ReflectionModelServices.IsDisposalRequired(definition),
                           new Lazy <IEnumerable <ImportDefinition> >(() => definition.ImportDefinitions),
                           new Lazy <IEnumerable <ExportDefinition> >(() => exports),
                           new Lazy <IDictionary <string, object> >(() => definition.Metadata),
                           definition as ICompositionElement));
            }

            return(definition);
        }