private IEnumerable <CompositionDependency> GetPartActivatorDependencies(DependencyAccessor definitionAccessor) { var partTypeAsType = _partType.AsType(); if (_constructor == null) { foreach (var c in _partType.DeclaredConstructors.Where(ci => ci.IsPublic && !(ci.IsStatic))) { if (_attributeContext.GetDeclaredAttribute <ImportingConstructorAttribute>(partTypeAsType, c) != null) { if (_constructor != null) { string message = SR.Format(SR.DiscoveredPart_MultipleImportingConstructorsFound, _partType); throw new CompositionFailedException(message); } _constructor = c; } } if (_constructor == null && _partType.IsGenericType) { _constructor = GetConstructorInfoFromGenericType(_partType); } if (_constructor == null) { _constructor = _partType.DeclaredConstructors .FirstOrDefault(ci => ci.IsPublic && !(ci.IsStatic || ci.GetParameters().Any())); } if (_constructor == null) { string message = SR.Format(SR.DiscoveredPart_NoImportingConstructorsFound, _partType); throw new CompositionFailedException(message); } } var cps = _constructor.GetParameters(); for (var i = 0; i < cps.Length; ++i) { var pi = cps[i]; var site = new ParameterImportSite(pi); var importInfo = ContractHelpers.GetImportInfo(pi.ParameterType, _attributeContext.GetDeclaredAttributes(partTypeAsType, pi), site); if (!importInfo.AllowDefault) { yield return(definitionAccessor.ResolveRequiredDependency(site, importInfo.Contract, true)); } else { CompositionDependency optional; if (definitionAccessor.TryResolveOptionalDependency(site, importInfo.Contract, true, out optional)) { yield return(optional); } } } }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"Getting Export Descriptors:" + $" contractName=[{contract.ContractName}]" + $" contractType=[{contract.ContractType.FullName}]"); } var svc = _serviceProvider.GetService(contract.ContractType); if (svc == null) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"No DI service found for" + $" contractType=[{contract.ContractType.FullName}]"); } yield break; } if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"Resolved DI service for" + $" contractType=[{contract.ContractType.FullName}]" + $" service=[{svc}]"); } CompositeActivator ca = (ctx, op) => svc; yield return(new ExportDescriptorPromise(contract, ORIGIN_NAME, true, NoDependencies, deps => ExportDescriptor.Create(ca, NoMetadata))); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { var unconstrained = new CompositionContract(contract.ContractType, contract.ContractName); ICollection <PrimitiveExport> candidates; if (!_parts.TryGetValue(unconstrained, out candidates)) { return(NoExportDescriptors); } return(candidates .Where(px => { if (contract.MetadataConstraints != null) { var subsetOfConstraints = contract.MetadataConstraints.Where(c => px.Metadata.ContainsKey(c.Key)).ToDictionary(c => c.Key, c => px.Metadata[c.Key]); var constrainedSubset = new CompositionContract(contract.ContractType, contract.ContractName, subsetOfConstraints.Count == 0 ? null : subsetOfConstraints); if (!contract.Equals(constrainedSubset)) { return false; } } return true; }) .Select(px => px.GetPromise(contract, descriptorAccessor))); }
public CompositionDependency[] GetDependencies(DependencyAccessor definitionAccessor) { return(GetPartActivatorDependencies(definitionAccessor) .Concat(_activationFeatures .SelectMany(feature => feature.GetDependencies(_partType, definitionAccessor))) .Where(a => a != null) .ToArray()); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { if (contract.ContractType == ContractType) { yield return(new ExportDescriptorPromise( contract, contract.ContractType.FullName, false, ExportDescriptorProvider.NoDependencies, dependencies => ExportDescriptor.Create(activator, ExportDescriptorProvider.NoMetadata))); } }
public CompositeActivator GetActivator(DependencyAccessor definitionAccessor, IEnumerable <CompositionDependency> dependencies) { if (_partActivator != null) { return(_partActivator); } var contextParam = Expression.Parameter(typeof(LifetimeContext), "cc"); var operationParm = Expression.Parameter(typeof(CompositionOperation), "op"); var cps = _constructor.GetParameters(); Expression[] paramActivatorCalls = new Expression[cps.Length]; var partActivatorDependencies = dependencies .Where(dep => dep.Site is ParameterImportSite) .ToDictionary(d => ((ParameterImportSite)d.Site).Parameter); for (var i = 0; i < cps.Length; ++i) { var pi = cps[i]; CompositionDependency dep; if (partActivatorDependencies.TryGetValue(pi, out dep)) { var a = dep.Target.GetDescriptor().Activator; paramActivatorCalls[i] = Expression.Convert(Expression.Call(Expression.Constant(a), s_activatorInvoke, contextParam, operationParm), pi.ParameterType); } else { paramActivatorCalls[i] = Expression.Default(pi.ParameterType); } } Expression body = Expression.Convert(Expression.New(_constructor, paramActivatorCalls), typeof(object)); var activator = Expression .Lambda <CompositeActivator>(body, contextParam, operationParm) .Compile(); foreach (var activationFeature in _activationFeatures) { activator = activationFeature.RewriteActivator(_partType, activator, _partMetadata.Value, dependencies); } _partActivator = activator; return(_partActivator); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { object svc = _serviceProvider.GetService(contract.ContractType); if (svc == null) { yield break; } object Ca(LifetimeContext ctx, CompositionOperation op) => svc; yield return(new ExportDescriptorPromise(contract, OriginName, true, NoDependencies, deps => ExportDescriptor.Create(Ca, NoMetadata))); }
public ExportDescriptorPromise GetExportDescriptorPromise( CompositionContract contract, DependencyAccessor definitionAccessor) { return(new ExportDescriptorPromise( contract, Part.PartType.Name, Part.IsShared, () => Part.GetDependencies(definitionAccessor), deps => { var activator = Part.GetActivator(definitionAccessor, deps); return GetExportDescriptor(activator); })); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { if (contract.ContractType == ContractType) { yield return(new ExportDescriptorPromise( contract, contract.ContractType.FullName, true, ExportDescriptorProvider.NoDependencies, dependencies => ExportDescriptor.Create( (context, operation) => SharedInstance, ExportDescriptorProvider.NoMetadata))); } }
public ExportDescriptorPromise GetPromise(CompositionContract contract, DependencyAccessor descriptorAccessor) { var origin = _export.ToString(); if (_export is ICompositionElement) { origin = ((ICompositionElement)_export).DisplayName; } return(new ExportDescriptorPromise(contract, origin, _part.IsShared, () => _part.GetDependencies(descriptorAccessor), d => { var partActivator = _part.GetActivator(d); CompositeActivator exportActivator = (c, o) => ((ComposablePart)partActivator(c, o)).GetExportedValue(_export); return ExportDescriptor.Create(exportActivator, _export.Metadata); })); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { if (contract.ContractType == typeof(T)) { return new[] { new ExportDescriptorPromise( contract, "Manual export", false, NoDependencies, d => new InstanceExportDescriptor <T>((T)mInstance)) } } ; //Console.WriteLine("Manual export: "+contract); return(NoExportDescriptors); } }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { var result = new List <ExportDescriptorPromise>(); foreach (var service in ServicesByType[contract.ContractType]) { Log.Debug($"Providing {service.ImplementationType.FullName} as {contract.ContractType.FullName}."); var promise = new ExportDescriptorPromise(contract, nameof(CustomExportProvider), true, () => GetDependencies(service, contract, descriptorAccessor), dependencies => ExportDescriptor.Create( activator: (context, o) => ServiceProvider.GetRequiredService(contract.ContractType), metadata: NoMetadata)); result.Add(promise); } return(result); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { var result = new List <ExportDescriptorPromise>(); foreach (var service in services.Where(s => s.ServiceType == contract.ContractType)) { var servicePromise = new ExportDescriptorPromise( contract, "Services", true, NoDependencies, _ => ExportDescriptor.Create((c, o) => services.QuickResolve(contract.ContractType), NoMetadata)); result.Add(servicePromise); } return(result); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor) { if (!contract.Equals(new CompositionContract(contract.ContractType))) { return(NoExportDescriptors); } if (!_exportedValues.ContainsKey(contract.ContractType)) { return(NoExportDescriptors); } return(new[] { new ExportDescriptorPromise( contract, "CompositionProvider", true, NoDependencies, _ => ExportDescriptor.Create( (c, o) => _exportedValues[contract.ContractType], NoMetadata)) }); }
IEnumerable <ExportDescriptorPromise> ExportPersistencePart(CompositionContract contract, DependencyAccessor descriptorAccessor) { const bool shared = true; var entityName = contract.ContractName; var persistenceResolver = new PersistenceDependencyResolver(descriptorAccessor, entityName); yield return(new ExportDescriptorPromise( contract, nameof(SqlConfigurationExportDescriptorProvider), shared, persistenceResolver.Resolve, d => Create((c, o) => PersistenceActivator(c, o, d), NoMetadata))); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor) { if (IsSupportedContract(contract)) { yield return(new ExportDescriptorPromise(contract, _exportedInstance.ToString(), true, NoDependencies, _ => ExportDescriptor.Create((c, o) => _exportedInstance, Metadata))); } }
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 }) }); }
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 exportKey, DependencyAccessor definitionAccessor) { if (!exportKey.ContractType.IsConstructedGenericType || exportKey.ContractType.GetGenericTypeDefinition() != typeof(ExportFactory <>)) { return(NoExportDescriptors); } var gld = GetExportFactoryDefinitionsMethod.MakeGenericMethod(exportKey.ContractType.GenericTypeArguments[0]); var gldm = gld.CreateStaticDelegate <Func <CompositionContract, DependencyAccessor, object> >(); return((ExportDescriptorPromise[])gldm(exportKey, definitionAccessor)); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor) { Contracts.Add(contract); return(new ExportDescriptorPromise[0]); }
static ExportDescriptorPromise GetImportManyDescriptor <TElement>(CompositionContract importManyContract, CompositionContract elementContract, DependencyAccessor definitionAccessor) { return(new ExportDescriptorPromise( importManyContract, typeof(TElement[]).Name, false, () => definitionAccessor.ResolveDependencies("item", elementContract, true), d => { var dependentDescriptors = d .Select(el => el.Target.GetDescriptor()) .ToArray(); return ExportDescriptor.Create((c, o) => dependentDescriptors.Select(e => (TElement)e.Activator(c, o)).ToArray(), NoMetadata); })); }
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)) }); }
IEnumerable <ExportDescriptorPromise> ExportMessageQueueConfigurationPart(CompositionContract contract, DependencyAccessor descriptorAccessor) { const bool shared = true; var connectionString = new StringDependencyResolver(descriptorAccessor, connectionStringKey); yield return(new ExportDescriptorPromise( contract, nameof(SqlConfigurationExportDescriptorProvider), shared, connectionString.Resolve, d => Create((c, o) => MessageQueueConfigurationActivator(c, o, d), NoMetadata))); }
static ExportDescriptorPromise GetDictionaryDefinition <TKey, TValue>(CompositionContract dictionaryContract, CompositionContract valueContract, DependencyAccessor definitionAccessor, string keyByMetadataName) { return(new ExportDescriptorPromise( dictionaryContract, typeof(IDictionary <TKey, TValue>).Name, false, () => definitionAccessor.ResolveDependencies("value", valueContract, true), deps => { var items = deps.Select(d => Tuple.Create(d.Target.Origin, d.Target.GetDescriptor())).ToArray(); var isValidated = false; return ExportDescriptor.Create((c, o) => { if (!isValidated) { Validate <TKey>(items, keyByMetadataName); isValidated = true; } return items.ToDictionary( item => (TKey)item.Item2.Metadata[keyByMetadataName], item => (TValue)item.Item2.Activator(c, o)); }, NoMetadata); })); }
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 }) }); }
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor definitionAccessor);
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor) { var implementations = descriptorAccessor.ResolveDependencies("test", contract, false); if (!implementations.Any()) { var promise = new ExportDescriptorPromise( contract, contract.ContractType.Name, false, NoDependencies, _ => ExportDescriptor.Create((c, o) => { object export; if ((c.ToString() != "Root Lifetime Context" && c.TryGetExport(typeof(MefServiceScopeFallback), out export) || c.TryGetExport(typeof(MefServiceRootFallback), out export)) && export is IMefServiceFallback serviceProvider) { return(serviceProvider.FallbackServiceProvider?.GetService(contract.ContractType)); } return(null); }, NoMetadata)); return(new[] { promise }); } return(NoExportDescriptors); }
public abstract IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor);
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) { CalledGetExportDescriptors++; return(new[] { new ExportDescriptorPromise(contract, "origin", false, () => new CompositionDependency[0], dependencies => ExportDescriptor.Create(CompositeActivator, new Dictionary <string, object>())) }); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor definitionAccessor) { DiscoverGenericParts(contract); DiscoverConstrainedParts(contract); ICollection <DiscoveredExport> forKey; if (!_discoveredParts.TryGetValue(contract, out forKey)) { return(NoExportDescriptors); } // Exports with metadata may be matched via metadata constraints. // It should be possible to do this more aggressively by changing the way // exports are stored. if (!forKey.Any(x => x.Metadata.Any())) { // Allow some garbage to be collected _discoveredParts.Remove(contract); } return(forKey.Select(de => de.GetExportDescriptorPromise(contract, definitionAccessor)).ToArray()); }
public override IEnumerable <ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor) { var target = new ExportDescriptorPromise(new CompositionContract(typeof(int)), "Origin1", true, () => { var dependencyTarget = new ExportDescriptorPromise(new CompositionContract(typeof(int)), "Origin2", true, () => Enumerable.Empty <CompositionDependency>(), dependencies => { return(ExportDescriptor.Create(Activator, new Dictionary <string, object>())); }); return(new CompositionDependency[] { CompositionDependency.Satisfied(contract, dependencyTarget, true, "Site") }); }, dependencies => { return(ExportDescriptor.Create(Activator, new Dictionary <string, object>())); }); return(new ExportDescriptorPromise[] { target }); }