Example #1
0
        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)));
        }
        protected override InstanceProducer BuildCollectionProducer(Type closedServiceType)
        {
            ContainerControlledItem[] closedGenericImplementations =
                this.GetClosedContainerControlledItemsFor(closedServiceType);

            IContainerControlledCollection collection =
                ControlledCollectionHelper.CreateContainerControlledCollection(
                    closedServiceType, this.Container);

            collection.AppendAll(closedGenericImplementations);

            var collectionType = typeof(IEnumerable <>).MakeGenericType(closedServiceType);

            return(new InstanceProducer(
                       serviceType: collectionType,
                       registration: collection.CreateRegistration(collectionType, this.Container)));
        }