示例#1
0
        public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor definitionAccessor)
        {
            if (!(contract.ContractType.IsArray ||
                  contract.ContractType.IsConstructedGenericType && SupportedContractTypes.Contains(contract.ContractType.GetGenericTypeDefinition())))
            {
                return(NoExportDescriptors);
            }

            string keyToOrderBy;
            CompositionContract orderUnwrappedContract;

            if (!contract.TryUnwrapMetadataConstraint(OrderByMetadataImportMetadataConstraintName, out keyToOrderBy, out orderUnwrappedContract))
            {
                return(NoExportDescriptors);
            }

            var elementType = contract.ContractType.IsArray ?
                              contract.ContractType.GetElementType() :
                              contract.ContractType.GenericTypeArguments[0];

            var elementContract = orderUnwrappedContract.ChangeType(elementType);

            var gimd = GetImportManyDefinitionMethod.MakeGenericMethod(elementType);

            return(new[] { (ExportDescriptorPromise)gimd.Invoke(null, new object[] { contract, elementContract, definitionAccessor, keyToOrderBy }) });
        }
        public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor definitionAccessor)
        {
            if (!(contract.ContractType.IsArray ||
                  contract.ContractType.IsConstructedGenericType && SupportedContractTypes.Contains(contract.ContractType.GetGenericTypeDefinition())))
            {
                return(NoExportDescriptors);
            }

            bool isImportMany;
            CompositionContract unwrapped;

            if (!contract.TryUnwrapMetadataConstraint(Constants.ImportManyImportMetadataConstraintName, out isImportMany, out unwrapped))
            {
                return(NoExportDescriptors);
            }

            var elementType = contract.ContractType.IsArray ?
                              contract.ContractType.GetElementType() :
                              contract.ContractType.GenericTypeArguments[0];

            var elementContract = unwrapped.ChangeType(elementType);

            var gimd  = GetImportManyDefinitionMethod.MakeGenericMethod(elementType);
            var gimdm = gimd.CreateStaticDelegate <Func <CompositionContract, CompositionContract, DependencyAccessor, object> >();

            return(new[] { (ExportDescriptorPromise)gimdm(contract, elementContract, definitionAccessor) });
        }
        public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
        {
            if (!(contract.ContractType.IsConstructedGenericType && contract.ContractType.GetGenericTypeDefinition() == typeof(IDictionary <,>)))
            {
                return(NoExportDescriptors);
            }

            CompositionContract unwrapped;
            string keyByMetadataName;

            if (!contract.TryUnwrapMetadataConstraint(KeyByMetadataImportMetadataConstraintName, out keyByMetadataName, out unwrapped))
            {
                return(NoExportDescriptors);
            }

            var args      = contract.ContractType.GenericTypeArguments;
            var keyType   = args[0];
            var valueType = args[1];

            var valueContract = unwrapped.ChangeType(valueType);

            var gdd = GetDictionaryDefinitionsMethod.MakeGenericMethod(keyType, valueType);

            return(new[] { (ExportDescriptorPromise)gdd.Invoke(null, new object[] { contract, valueContract, descriptorAccessor, keyByMetadataName }) });
        }
        static ExportDescriptorPromise[] GetExportFactoryDescriptors <TProduct>(CompositionContract exportFactoryContract, DependencyAccessor definitionAccessor)
        {
            var productContract = exportFactoryContract.ChangeType(typeof(TProduct));
            var boundaries      = new string[0];

            IEnumerable <string> specifiedBoundaries;
            CompositionContract  unwrapped;

            if (exportFactoryContract.TryUnwrapMetadataConstraint(Constants.SharingBoundaryImportMetadataConstraintName, out specifiedBoundaries, out unwrapped))
            {
                productContract = unwrapped.ChangeType(typeof(TProduct));
                boundaries      = (specifiedBoundaries ?? new string[0]).ToArray();
            }

            return(definitionAccessor.ResolveDependencies("product", productContract, false)
                   .Select(d => new ExportDescriptorPromise(
                               exportFactoryContract,
                               Formatters.Format(typeof(ExportFactory <TProduct>)),
                               false,
                               () => new[] { d },
                               _ =>
            {
                var dsc = d.Target.GetDescriptor();
                var da = dsc.Activator;
                return ExportDescriptor.Create((c, o) =>
                {
                    return new ExportFactory <TProduct>(() => {
                        var lifetimeContext = new LifetimeContext(c, boundaries);
                        return Tuple.Create <TProduct, Action>((TProduct)CompositionOperation.Run(lifetimeContext, da), lifetimeContext.Dispose);
                    });
                },
                                               dsc.Metadata);
            }))
                   .ToArray());
        }
        public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(
            CompositionContract contract,
            DependencyAccessor dependencyAccessor)
        {
            if (contract == null) throw new ArgumentNullException("contract");
            if (dependencyAccessor == null) throw new ArgumentNullException("dependencyAccessor");

            string key;
            CompositionContract unwrapped;

            if (!contract.TryUnwrapMetadataConstraint(SettingKey, out key, out unwrapped) ||
                !unwrapped.Equals(new CompositionContract(unwrapped.ContractType)) ||
                !SupportedSettingTypes.Contains(unwrapped.ContractType) ||
                !ConfigurationManager.AppSettings.AllKeys.Contains(key))
                yield break;

            var value = ConfigurationManager.AppSettings.Get(key);
            var converted = Convert.ChangeType(value, contract.ContractType);

            yield return new ExportDescriptorPromise(
                    contract,
                    "System.Configuration.ConfigurationManager.AppSettings",
                    true,
                    NoDependencies,
                    _ => ExportDescriptor.Create((c, o) => converted, NoMetadata));
        }
        /// <summary>
        /// <see cref="ILogger"/>生成処理
        ///
        /// https://mef.codeplex.com/wikipage?title=ProgrammingModelExtensions&referringTitle=Documentation
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="descriptorAccessor"></param>
        /// <returns></returns>
        public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
        {
            if (contract.ContractType == typeof(ILogger))
            {
                string value = string.Empty;
                CompositionContract unwrap;

                contract.TryUnwrapMetadataConstraint("LogType", out value, out unwrap);

                string header = string.Format("[{0}] LogExportDescriptorProvider - ", value);

                return(new ExportDescriptorPromise[]
                {
                    new ExportDescriptorPromise(
                        contract,
                        "ConsoleMef2 ILogger",
                        true,
                        NoDependencies,
                        _ => ExportDescriptor.Create((c, o) => new Logger()
                    {
                        Header = header
                    }, NoMetadata)
                        )
                });
            }
            else
            {
                return(NoExportDescriptors);
            }
        }
        /// <summary>
        /// <see cref="ILogger"/>生成処理
        /// 
        /// https://mef.codeplex.com/wikipage?title=ProgrammingModelExtensions&referringTitle=Documentation
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="descriptorAccessor"></param>
        /// <returns></returns>
        public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
        {
            if (contract.ContractType == typeof(ILogger))
            {
                string value = string.Empty;
                CompositionContract unwrap;

                contract.TryUnwrapMetadataConstraint("LogType", out value, out unwrap);

                string header = string.Format("[{0}] LogExportDescriptorProvider - ", value);

                return new ExportDescriptorPromise[]
                {
                    new ExportDescriptorPromise(
                        contract,
                        "ConsoleMef2 ILogger",
                        true,
                        NoDependencies,
                        _ => ExportDescriptor.Create((c, o) => new Logger() { Header = header },  NoMetadata)
                    )
                };
            }
            else
                return NoExportDescriptors;
        }
        public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
        {
            if (contract == null) throw new ArgumentNullException("contract");
            if (descriptorAccessor == null) throw new ArgumentNullException("descriptorAccessor");

            string name;
            CompositionContract unwrapped;

            var connectionStringSettings = ConfigurationManager.ConnectionStrings.OfType<ConnectionStringSettings>().ToArray();

            if (!contract.TryUnwrapMetadataConstraint(NameKey, out name, out unwrapped) ||
                !unwrapped.Equals(new CompositionContract(unwrapped.ContractType)) ||
                !(unwrapped.ContractType == typeof(string) || typeof(DbConnectionStringBuilder).IsAssignableFrom(unwrapped.ContractType)) ||
                !connectionStringSettings.Any(cs => cs.Name == name))
                yield break;

            var stringValue = connectionStringSettings.Single(cs => cs.Name == name).ConnectionString;
            object value = stringValue;

            if (contract.ContractType != typeof(string))
            {
                var stringBuilder = Activator.CreateInstance(contract.ContractType) as DbConnectionStringBuilder;
                if (stringBuilder == null) yield break;
                stringBuilder.ConnectionString = stringValue;
                value = stringBuilder;
            }

            yield return new ExportDescriptorPromise(
                    contract,
                    "System.Configuration.ConfigurationManager.ConnectionStrings",
                    true,
                    NoDependencies,
                    _ => ExportDescriptor.Create((c, o) => value, NoMetadata));
        }
示例#9
0
        public void TryUnwrapMetadataConstraint_NullConstraints_ReturnsFalse()
        {
            var contract = new CompositionContract(typeof(int));

            Assert.False(contract.TryUnwrapMetadataConstraint("constraintName", out int constraintValue, out CompositionContract remainingContract));
            Assert.Equal(0, constraintValue);
            Assert.Null(remainingContract);
        }
示例#10
0
        public void TryUnwrapMetadataConstraint_IncorrectConstraintNameType_ReturnsFalse()
        {
            var contract = new CompositionContract(typeof(int), "contractName", new Dictionary <string, object> {
                { "constraintName", "value" }
            });

            Assert.False(contract.TryUnwrapMetadataConstraint("constraintName", out int constraintValue, out CompositionContract remainingContract));
            Assert.Equal(0, constraintValue);
            Assert.Null(remainingContract);
        }
示例#11
0
        public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor definitionAccessor)
        {
            string key;
            CompositionContract unwrapped;

            if (!contract.TryUnwrapMetadataConstraint(Constants.SettingKey, out key, out unwrapped))
            {
                return(NoExportDescriptors);
            }

            if (!unwrapped.Equals(new CompositionContract(unwrapped.ContractType)))
            {
                return(NoExportDescriptors);
            }

            if (!SupportedSettingTypes.Contains(unwrapped.ContractType))
            {
                return(NoExportDescriptors);
            }

            var value = ConfigurationManager.AppSettings.Get(key);

            if (value == null)
            {
                return(NoExportDescriptors);
            }

            var converted = Convert.ChangeType(value, contract.ContractType);

            return(new[] {
                new ExportDescriptorPromise(
                    contract,
                    "Application Configuration",
                    true,
                    NoDependencies,
                    _ => ExportDescriptor.Create((c, o) => converted, NoMetadata))
            });
        }
示例#12
0
        public void TryUnwrapMetadataConstraint_UnwrapAllConstraints_ReturnsTrue()
        {
            var originalContract = new CompositionContract(typeof(int), "contractName", new Dictionary <string, object> {
                { "constraintName1", 1 }, { "constraintName2", 2 }
            });

            Assert.True(originalContract.TryUnwrapMetadataConstraint("constraintName1", out int constraintValue1, out CompositionContract remainingContract1));
            Assert.Equal(1, constraintValue1);

            Assert.Equal(originalContract.ContractType, remainingContract1.ContractType);
            Assert.Equal(originalContract.ContractName, remainingContract1.ContractName);
            Assert.Equal(new Dictionary <string, object> {
                { "constraintName2", 2 }
            }, remainingContract1.MetadataConstraints);
            Assert.NotEqual(originalContract.MetadataConstraints, remainingContract1.MetadataConstraints);

            Assert.True(remainingContract1.TryUnwrapMetadataConstraint("constraintName2", out int constraintValue2, out CompositionContract remainingContract2));
            Assert.Equal(2, constraintValue2);

            Assert.Equal(originalContract.ContractType, remainingContract2.ContractType);
            Assert.Equal(originalContract.ContractName, remainingContract2.ContractName);
            Assert.Null(remainingContract2.MetadataConstraints);
            Assert.NotEqual(originalContract.MetadataConstraints, remainingContract2.MetadataConstraints);
        }
示例#13
0
        public void TryUnwrapMetadataConstraint_NullContractName_ThrowsArgumentNullException()
        {
            var contract = new CompositionContract(typeof(int));

            AssertExtensions.Throws <ArgumentNullException>("constraintName", () => contract.TryUnwrapMetadataConstraint(null, out int unusedValue, out CompositionContract unusedContract));
        }