private InstanceProducer TryGetProducerFromUnregisteredTypeResolutionCacheOrAdd(
            UnregisteredTypeEventArgs e)
        {
            lock (this.resolveUnregisteredTypeRegistrations)
            {
                if (this.resolveUnregisteredTypeRegistrations.ContainsKey(e.UnregisteredServiceType))
                {
                    // This line will only get hit, in case a different thread came here first.
                    return(this.resolveUnregisteredTypeRegistrations[e.UnregisteredServiceType].Value);
                }

                var registration = e.Registration ?? new ExpressionRegistration(e.Expression, this);

                // By creating the InstanceProducer after checking the dictionary, we prevent the producer
                // from being created twice when multiple threads are running. Having the same duplicate
                // producer can cause a torn lifestyle warning in the container.
                var producer = new InstanceProducer(e.UnregisteredServiceType, registration);

                this.resolveUnregisteredTypeRegistrations[e.UnregisteredServiceType] = Helpers.ToLazy(producer);

                return(producer);
            }
        }
        // Instead of using the this.registrations instance, this method takes a snapshot. This allows the
        // container to be thread-safe, without using locks.
        private InstanceProducer GetInstanceProducerForType(Type serviceType,
                                                            Func <InstanceProducer> buildInstanceProducer,
                                                            bool suppressRegisteringUnexistingProducersForConcreteTypes = false)
        {
            InstanceProducer instanceProducer = null;

            if (!this.registrations.TryGetValue(serviceType, out instanceProducer))
            {
                var producer = buildInstanceProducer();

                // Always register the producer, even if it is null. This improves performance for the
                // GetService and GetRegistration methods. Exception to this rule is when this method is
                // called while suppressRegisteringUnexistingProducersForConcreteTypes is true, because this
                // causes us to invalidly flag the registration is invalid.
                if (producer != null || !suppressRegisteringUnexistingProducersForConcreteTypes)
                {
                    this.RegisterInstanceProducer(serviceType, producer);
                }

                return(producer);
            }

            return(instanceProducer);
        }
        private InstanceProducer CombineProducersToOne(Type closedServiceType, InstanceProducer[] producers)
        {
            IEnumerable instanceStream =
                from producer in producers
                from instances in (IEnumerable<object>)producer.GetInstance()
                select instances;

            instanceStream = Helpers.CastCollection(instanceStream, closedServiceType);

            var registration = SingletonLifestyle.CreateUncontrolledCollectionRegistration(
                closedServiceType,
                instanceStream,
                this.Container);

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

            return new InstanceProducer(collectionType, registration);
        }
 internal override void RegisterUncontrolledCollection(Type serviceType, InstanceProducer producer)
 {
     this.AddRegistrationGroup(RegistrationGroup.CreateForUncontrolledProducer(serviceType, producer));
 }
Example #5
0
 private void SetCurrentProducer(InstanceProducer producer)
 {
     this.currentProducer.Value = producer;
 }
        private static void VerifyContainerUncontrolledCollection(object instance, InstanceProducer producer)
        {
            bool isContainerUncontrolledCollection =
                producer.Registration.IsCollection && !(instance is IContainerControlledCollection);

            if (isContainerUncontrolledCollection)
            {
                Type collectionType = producer.ServiceType;
                Type serviceType = collectionType.GetGenericArguments()[0];

                Helpers.VerifyCollection((IEnumerable)instance, serviceType);
            }
        }
 internal DependencyMetadata(InstanceProducer dependency)
 {
     this.Dependency = dependency;
 }
 internal PredicateContext(InstanceProducer producer, InjectionConsumerInfo consumer, bool handled)
     : this(producer.ServiceType, producer.Registration.ImplementationType, consumer, handled)
 {
 }
 internal abstract void RegisterUncontrolledCollection(Type serviceType, InstanceProducer producer);
        private object GetInstanceForType <TService>() where TService : class
        {
            InstanceProducer producer = this.GetInstanceProducerForType <TService>();

            return(this.GetInstanceFromProducer(producer, typeof(TService)));
        }
 internal PredicateContext(InstanceProducer producer, InjectionConsumerInfo consumer, bool handled)
     : this(producer.ServiceType, producer.Registration.ImplementationType, consumer, handled)
 {
 }
Example #12
0
 internal InstanceProducerDebugView(InstanceProducer instanceProducer)
 {
     this.instanceProducer = instanceProducer;
 }
 internal override void RegisterUncontrolledCollection(Type serviceType, InstanceProducer producer)
 {
     throw new NotSupportedException(
         StringResources.MixingRegistrationsWithControlledAndUncontrolledIsNotSupported(serviceType,
             controlled: false));
 }
 internal InstanceProducerDebugView(InstanceProducer producer)
 {
     this.producer = producer;
 }
        internal void AddProducerToVerify(InstanceProducer currentProducer)
        {
            lock (this.locker)
            {
                if (this.wrappedProducers == null)
                {
                    this.wrappedProducers = new List<InstanceProducer>();
                }

                this.wrappedProducers.Add(currentProducer);
            }
        }
 internal InstanceProducerDebugView(InstanceProducer producer)
 {
     this.producer = producer;
 }
 internal static RegistrationGroup CreateForUncontrolledProducer(Type serviceType,
     InstanceProducer producer) => 
     new RegistrationGroup
     {
         ServiceType = serviceType,
         UncontrolledProducer = producer
     };
        private object GetInstanceForType(Type serviceType)
        {
            InstanceProducer producer = this.GetInstanceProducerForType(serviceType);

            return(this.GetInstanceFromProducer(producer, serviceType));
        }
Example #19
0
 public DependencyData(ParameterInfo parameter, Expression expression, InstanceProducer producer)
 {
     this.Parameter  = parameter;
     this.Expression = expression;
     this.Producer   = producer;
 }
        private static void VerifyThatAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify)
        {
            foreach (var producer in producersToVerify)
            {
                var expression = producer.VerifyExpressionBuilding();

                VerifyInstanceProducersOfContainerControlledCollection(expression);
            }
        }
 internal InstanceProducerDebugView(InstanceProducer instanceProducer)
 {
     this.instanceProducer = instanceProducer;
 }
        private static void VerifyInstanceCreation(InstanceProducer[] producersToVerify)
        {
            foreach (var producer in producersToVerify)
            {
                if (!producer.InstanceSuccessfullyCreated)
                {
                    var instance = producer.VerifyInstanceCreation();

                    VerifyContainerUncontrolledCollection(instance, producer);
                }

                if (!producer.VerifiersAreSuccessfullyCalled)
                {
                    producer.DoExtraVerfication();
                }
            }
        }