private InstanceProducer BuildInstanceProducerForMetadataList(Type enumerableOfProducersType) { Type metadataType = enumerableOfProducersType.GetGenericArguments()[0]; Type serviceType = metadataType.GetGenericArguments()[0]; var collection = this.container.GetAllInstances(serviceType) as IContainerControlledCollection; if (collection is null) { // This exception might not be expressive enough. If GetAllInstances succeeds, but the // returned type is not an IContainerControlledCollection, it likely means the collection is // container uncontrolled. this.container.ThrowMissingInstanceProducerException(serviceType); } IContainerControlledCollection metadataCollection = ControlledCollectionHelper.CreateContainerControlledCollection(metadataType, this.container); metadataCollection.AppendAll( from producer in collection !.GetProducers() let metadata = CreateMetadata(metadataType, producer) let reg = Lifestyle.Singleton.CreateRegistration(metadataType, metadata, this.container) select ContainerControlledItem.CreateFromRegistration(reg)); return(new InstanceProducer( serviceType: enumerableOfProducersType, registration: metadataCollection.CreateRegistration(enumerableOfProducersType, this.container))); }
internal ContainerControlledCollectionRegistration(Type serviceType, IContainerControlledCollection collection, Container container) : base(Lifestyle.Singleton, container) { this.Collection = collection; this.ImplementationType = serviceType; }
internal static void AppendAll(this IContainerControlledCollection collection, IEnumerable <ContainerControlledItem> registrations) { foreach (ContainerControlledItem registration in registrations) { collection.Append(registration); } }
internal static void AppendAll( this IContainerControlledCollection collection, IEnumerable <ContainerControlledItem> items) { foreach (ContainerControlledItem item in items) { collection.Append(item); } }
internal static Registration CreateRegistration( this IContainerControlledCollection instance, Type collectionType, Container container) { // We need special handling for Collection<T>, because the ContainerControlledCollection does not // (and can't) inherit from Collection<T>. So we have to wrap that stream into a Collection<T>. return(collectionType.GetGenericTypeDefinition() == typeof(Collection <>) ? CreateRegistrationForCollectionOfT(instance, collectionType, container) : new ContainerControlledCollectionRegistration(collectionType, instance, container)); }
internal static Registration CreateRegistration( this IContainerControlledCollection instance, Type collectionType, Container container) { // We need special handling for Collection<T> (and ReadOnlyCollection<T>), because the // ContainerControlledCollection does not (and can't) inherit it. So we have to wrap that // stream into a Collection<T> or ReadOnlyCollection<T>. return(TryCreateRegistrationForCollectionOfT(collectionType, instance, container) ?? new ContainerControlledCollectionRegistration(collectionType, instance, container)); }
internal ContainerControlledCollectionRegistration( Type collectionType, IContainerControlledCollection collection, Container container) : base(Lifestyle.Singleton, container, collectionType) { this.Collection = collection; this.IsCollection = true; }
private Registration BuildContainerControlledRegistration(Type closedServiceType, ContainerControlledItem[] closedGenericImplementations) { IContainerControlledCollection collection = DecoratorHelpers.CreateContainerControlledCollection( closedServiceType, this.Container); collection.AppendAll(closedGenericImplementations); return(DecoratorHelpers.CreateRegistrationForContainerControlledCollection(closedServiceType, collection, this.Container)); }
private static Registration CreateRegistrationForCollectionOfT( IContainerControlledCollection controlledCollection, Type collectionType, Container container) { var collection = Activator.CreateInstance(collectionType, controlledCollection); return(new ContainerControlledCollectionRegistration( collectionType, controlledCollection, container) { Expression = Expression.Constant( Activator.CreateInstance(collectionType, controlledCollection), collectionType), }); }
protected override InstanceProducer BuildCollectionProducer(Type closedServiceType) { ContainerControlledItem[] closedGenericImplementations = this.GetClosedContainerControlledItemsFor(closedServiceType); IContainerControlledCollection collection = DecoratorHelpers.CreateContainerControlledCollection( closedServiceType, this.Container); collection.AppendAll(closedGenericImplementations); return(new InstanceProducer( typeof(IEnumerable <>).MakeGenericType(closedServiceType), DecoratorHelpers.CreateRegistrationForContainerControlledCollection(closedServiceType, collection, this.Container))); }
private static Registration?TryCreateRegistrationForCollectionOfT( Type collectionType, IContainerControlledCollection controlledCollection, Container container) { if (collectionType.GetGenericTypeDefinition() == typeof(Collection <>) || collectionType.GetGenericTypeDefinition() == typeof(ReadOnlyCollection <>)) { return(new ContainerControlledCollectionRegistration( collectionType, controlledCollection, container) { Expression = Expression.Constant( Activator.CreateInstance(collectionType, controlledCollection), collectionType), }); } return(null); }
private Registration BuildContainerControlledRegistration(Type closedServiceType, Type[] closedGenericImplementations) { var registrations = ( from closedGenericImplementation in closedGenericImplementations select this.CreateRegistrationForClosedGenericImplementation( closedServiceType, closedGenericImplementation)) .ToArray(); IContainerControlledCollection collection = DecoratorHelpers.CreateContainerControlledCollection(closedServiceType, this.Container); collection.AppendAll(registrations); return(DecoratorHelpers.CreateRegistrationForContainerControlledCollection(closedServiceType, collection, this.Container)); }
private static ScopedRegistration?TryCreateRegistrationForFlowingCollection( IContainerControlledCollection instance, Type collectionType, Container container) { // Only create a scoped collection when we're in flowing mode and the graph contains scoped // components, because this makes it less likely to hit a Lifestyle Mismatch, as users typically // expect collections to be singletons. Downside of this approach is that all wrapped instance // producers need to be built here. if (ScopedLifestyle.Flowing != container.Options.DefaultScopedLifestyle || !ContainsScopedComponents(instance)) { return(null); } Type elementType = collectionType.GetGenericArguments()[0]; var ctor = typeof(FlowingContainerControlledCollection <>).MakeGenericType(elementType) .GetConstructors().Single(); Expression newCollectionExpression = Expression.New( ctor, container.GetRegistration(typeof(Scope), true) !.BuildExpression(), Expression.Constant(instance)); // Wrap the collection in a Collection<T> or ReadOnlyCollection<T> if (collectionType.GetGenericTypeDefinition() == typeof(Collection <>) || collectionType.GetGenericTypeDefinition() == typeof(ReadOnlyCollection <>)) { newCollectionExpression = Expression.New( collectionType.GetConstructors().Where(c => c.GetParameters().Length == 1).First(), newCollectionExpression); } return(new ScopedRegistration( container.Options.DefaultScopedLifestyle !, container, collectionType, (Func <object>)container.Options.ExpressionCompilationBehavior.Compile(newCollectionExpression)) { AdditionalInformationForLifestyleMismatchDiagnostics = StringResources.FlowingCollectionIsScopedBecause(collectionType) });
internal static Registration CreateRegistrationForContainerControlledCollection(Type serviceType, IContainerControlledCollection instance, Container container) { Type enumerableServiceType = typeof(IEnumerable <>).MakeGenericType(serviceType); return(new ContainerControlledCollectionRegistration(enumerableServiceType, instance, container) { IsCollection = true }); }
internal static void AppendAll(this IContainerControlledCollection collection, IEnumerable <Registration> registrations) { collection.AppendAll(registrations.Select(ContainerControlledItem.CreateFromRegistration)); }
internal static void AppendAll(this IContainerControlledCollection collection, IEnumerable <Type> types) { collection.AppendAll(types.Select(ContainerControlledItem.CreateFromType)); }
internal static void AppendAll(this IContainerControlledCollection collection, IEnumerable <Type> serviceTypes) { collection.AppendAll(serviceTypes.Select(type => new ContainerControlledItem(type))); }