Esempio n. 1
0
 private static ContainerControlledItem[] GetClosedGenericImplementationsFor(
     Type closedGenericServiceType, IEnumerable <ContainerControlledItem> containerControlledItems)
 {
     return((
                from item in containerControlledItems
                let openGenericImplementation = item.ImplementationType
                                                let builder = new GenericTypeBuilder(closedGenericServiceType, openGenericImplementation)
                                                              let result = builder.BuildClosedGenericImplementation()
                                                                           where result.ClosedServiceTypeSatisfiesAllTypeConstraints
                                                                           select item.Registration != null
             ? item
             : ContainerControlledItem.CreateFromType(
                    openGenericImplementation, result.ClosedGenericImplementation !))
            .ToArray());
 }
 internal override void AddControlledRegistrations(Type serviceType,
     ContainerControlledItem[] registrations, bool append)
 {
     if (append)
     {
         throw new NotSupportedException(
             StringResources.AppendingRegistrationsToContainerUncontrolledCollectionsIsNotSupported(
                 serviceType));
     }
     else
     {
         throw new NotSupportedException(
             StringResources.MixingRegistrationsWithControlledAndUncontrolledIsNotSupported(serviceType,
                 controlled: true));
     }
 }
Esempio n. 3
0
        private InstanceProducer CreateNewExternalProducer(ContainerControlledItem item)
        {
            if (!Types.IsConcreteConstructableType(item.ImplementationType))
            {
                throw new ActivationException(
                          StringResources.UnregisteredAbstractionFoundInCollection(
                              serviceType: typeof(TService),
                              registeredType: item.RegisteredImplementationType,
                              foundAbstractType: item.ImplementationType));
            }

            Lifestyle lifestyle = this.container.SelectionBasedLifestyle;

            // This producer will be automatically registered as external producer.
            return(lifestyle.CreateProducer(typeof(TService), item.ImplementationType, this.container));
        }
Esempio n. 4
0
        // Note that the 'implementationType' could in fact be a service type as well and it is allowed
        // for the implementationType to equal TService. This will happen when someone does the following:
        // container.Collections.Register<ILogger>(typeof(ILogger));
        private InstanceProducer GetOrCreateInstanceProducer(ContainerControlledItem item)
        {
            Type implementationType = item.ImplementationType;

            // If the implementationType is explicitly registered (using a Register call) we select this
            // producer (but we skip any implicit registrations or anything that is assignable, since
            // there could be more than one and it would be unclear which one to pick).
            InstanceProducer?producer = this.GetExplicitRegisteredInstanceProducer(implementationType);

            // If that doesn't result in a producer, we request a registration using unregistered type
            // resolution, were we prevent concrete types from being created by the container, since
            // the creation of concrete type would 'pollute' the list of registrations, and might result
            // in two registrations (since below we need to create a new instance producer out of it),
            // and that might cause duplicate diagnostic warnings.
            if (producer == null)
            {
                producer = this.GetInstanceProducerThroughUnregisteredTypeResolution(implementationType);
            }

            // If that still hasn't resulted in a producer, we create a new producer and return (or throw
            // an exception in case the implementation type is not a concrete type).
            if (producer == null)
            {
                return(this.CreateNewExternalProducer(item));
            }

            // If there is such a producer registered we return a new one with the service type.
            // This producer will be automatically registered as external producer.
            if (producer.ServiceType == typeof(TService))
            {
                return(producer);
            }

            return(new InstanceProducer(typeof(TService),
                                        new ExpressionRegistration(producer.BuildExpression(), this.container)));
        }
 private Lazy <InstanceProducer> ToLazyInstanceProducer(ContainerControlledItem registration) =>
 registration.Registration != null
         ? ToLazyInstanceProducer(registration.Registration)
         : this.ToLazyInstanceProducer(registration.ImplementationType);
 void IContainerControlledCollection.Append(ContainerControlledItem registration)
 {
     this.producers.Add(this.ToLazyInstanceProducer(registration));
 }
 internal abstract void AddControlledRegistrations(Type serviceType, 
     ContainerControlledItem[] registrations, bool append);
 internal static RegistrationGroup CreateForControlledItems(Type serviceType,
     ContainerControlledItem[] registrations, bool appended) => 
     new RegistrationGroup
     {
         ServiceType = serviceType,
         ControlledItems = registrations,
         Appended = appended
     };
Esempio n. 9
0
 private LazyEx <InstanceProducer> ToLazyInstanceProducer(ContainerControlledItem item) =>
 item.Registration != null
         ? ToLazyInstanceProducer(item.Registration)
         : new LazyEx <InstanceProducer>(() => this.GetOrCreateInstanceProducer(item));
Esempio n. 10
0
 void IContainerControlledCollection.Append(ContainerControlledItem item)
 {
     this.producers.Add(this.ToLazyInstanceProducer(item));
 }
 internal override void AddControlledRegistrations(Type serviceType,
     ContainerControlledItem[] registrations, bool append)
 {
     var group = RegistrationGroup.CreateForControlledItems(serviceType, registrations, append);
     this.AddRegistrationGroup(group);
 }