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)));
        }
        private static TService GetInstance(InstanceProducer producer)
        {
            var service = (TService)producer.GetInstance();

            // This check is an optimization that prevents always calling the helper method, while in the
            // happy path it is not needed.
            // This code is in the happy path, so we want the performance penalty to be minimal.
            // That's why we don't have a lock around this field access. This might cause the value to become
            // stale (when read by other threads), but that's not an issue here; other threads might still see
            // an old value (for some time), but we are actually only interested in getting notifications from
            // the same thread anyway.
            if (ControlledCollectionHelper.ContainsServiceCreatedListeners)
            {
                ControlledCollectionHelper.NotifyServiceCreatedListeners(producer);
            }

            return(service);
        }