private InstanceProducer TryBuildStreamInstanceProducer(Type collectionType) { Type serviceTypeDefinition = collectionType.GetGenericTypeDefinition(); if (serviceTypeDefinition == typeof(IEnumerable <>)) { return(null); } Type elementType = collectionType.GetGenericArguments()[0]; object stream = this.GetAllInstances(elementType); if (!(stream is IContainerControlledCollection)) { return(null); } // 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>. if (serviceTypeDefinition == typeof(Collection <>)) { Type listType = typeof(IList <>).MakeGenericType(elementType); stream = collectionType.GetConstructor(new[] { listType }).Invoke(new[] { stream }); } var registration = SingletonLifestyle.CreateSingleInstanceRegistration(collectionType, stream, this); return(new InstanceProducer(collectionType, registration) { IsContainerAutoRegistered = !((IEnumerable <object>)stream).Any() }); }
private InstanceProducer TryBuildCollectionInstanceProducer(Type serviceType) { Type serviceTypeDefinition = serviceType.GetGenericTypeDefinition(); if ( #if NET45 serviceTypeDefinition == typeof(IReadOnlyList <>) || serviceTypeDefinition == typeof(IReadOnlyCollection <>) || #endif serviceTypeDefinition == typeof(IList <>) || serviceTypeDefinition == typeof(ICollection <>)) { Type elementType = serviceType.GetGenericArguments()[0]; var collection = this.GetAllInstances(elementType) as IContainerControlledCollection; if (collection != null) { var registration = SingletonLifestyle.CreateSingleInstanceRegistration(serviceType, collection, this); var producer = new InstanceProducer(serviceType, registration); if (!((IEnumerable <object>)collection).Any()) { producer.IsContainerAutoRegistered = true; } return(producer); } } return(null); }
private static IEnumerable <InstanceProducer[]> GetTornRegistrationGroups( IEnumerable <InstanceProducer> producers) => from producer in producers where !producer.IsDecorated where producer.Registration.Lifestyle != Lifestyle.Transient where !SingletonLifestyle.IsSingletonInstanceRegistration(producer.Registration) where !producer.Registration.WrapsInstanceCreationDelegate group producer by producer.Registration into registrationGroup let registration = registrationGroup.Key let lifestyle = registration.Lifestyle.IdentificationKey let key = new { registration.ImplementationType, lifestyle } group registrationGroup by key into registrationLifestyleGroup let possibleConflictingProducers = registrationLifestyleGroup.SelectMany(p => p).ToArray() let hasConflict = registrationLifestyleGroup.Count() > 1 // HACK: Fixes #769. In case all producers in the group are Singleton and produce the same // instance, it will not result in a torn registration, as a torn registration produces // multiple instances. This is kind-of a hack, because the source of the problem lies within // the ContainerControlledCollection.GetOrCreateInstanceProducer method, as it creates a new // ExpressionRegistration instead of reusing the same. Changing that code, however, causes // other bugs (and failing tests). Because of that, we filter the problem out at this stage. && (possibleConflictingProducers.Any(p => p.Lifestyle != Lifestyle.Singleton) || possibleConflictingProducers.Select(p => p.GetInstance()).Distinct().Count() > 1) where hasConflict select possibleConflictingProducers;
private void RegisterGenericContainerUncontrolledCollection(Type itemType, IEnumerable collection) { var resolver = this.GetContainerUncontrolledResolver(itemType); var producer = SingletonLifestyle.CreateUncontrolledCollectionProducer(itemType, collection, this); resolver.RegisterUncontrolledCollection(itemType, producer); }
public void ProxyFluentExportStrategyConfiguration_UsingLifestyle([Locate] FluentWithCtorConfiguration <BasicService, int> configuration, IFluentExportStrategyConfiguration <BasicService> strategyConfiguration) { var lifestyle = new SingletonLifestyle(); configuration.UsingLifestyle(lifestyle); strategyConfiguration.Lifestyle.Received().Custom(lifestyle); }
public void ProxyFluentExportStrategyConfiguration_UsingLifestyle(FluentWithCtorConfiguration <int> configuration, IFluentExportStrategyConfiguration strategyConfiguration) { ICompiledLifestyle lifestyle = new SingletonLifestyle(); configuration.UsingLifestyle(lifestyle); strategyConfiguration.Lifestyle.Received().Custom(lifestyle); }
private void RegisterForVerification <TService>(ContainerControlledCollection <TService> collection) { // By creating a Producer, Simple Injector will automatically register it as 'external producer', // which allows it to be verified. To prevent memory leaks however, this external producer is // linked using a WeakReference to allow it to be GCed. To prevent this from happening, while // the application keeps referencing the collection, we let the collection reference the producer. collection.ParentProducer = SingletonLifestyle.CreateControlledCollectionProducer(collection, this.container); }
private static InstanceProducer[] CreateSingletonInstanceProducers(Container container, TService[] instances) { return(( from instance in instances let type = instance.GetType() let registration = SingletonLifestyle.CreateSingleRegistration(type, instance, container) let producer = new InstanceProducer(type, registration) select producer) .ToArray()); }
private static IEnumerable <InstanceProducer[]> GetTornRegistrationGroups( IEnumerable <InstanceProducer> producers) => from producer in producers where !producer.IsDecorated where producer.Registration.Lifestyle != Lifestyle.Transient where !SingletonLifestyle.IsSingletonInstanceRegistration(producer.Registration) where !producer.Registration.WrapsInstanceCreationDelegate group producer by producer.Registration into registrationGroup let registration = registrationGroup.Key let key = new { registration.ImplementationType, Lifestyle = registration.Lifestyle.GetType() } group registrationGroup by key into registrationLifestyleGroup let hasConflict = registrationLifestyleGroup.Count() > 1 where hasConflict select registrationLifestyleGroup.SelectMany(p => p).ToArray();
private static IEnumerable <InstanceProducer[]> GetTornRegistrationGroups( IEnumerable <InstanceProducer> producers) => from producer in producers where !producer.IsDecorated where producer.Registration.Lifestyle != Lifestyle.Transient where !SingletonLifestyle.IsSingletonInstanceRegistration(producer.Registration) where !producer.Registration.WrapsInstanceCreationDelegate group producer by producer.Registration into registrationGroup let registration = registrationGroup.Key let lifestyle = registration.Lifestyle.IdentificationKey let key = new { registration.ImplementationType, lifestyle } group registrationGroup by key into registrationLifestyleGroup let possibleConflictingProducers = registrationLifestyleGroup.SelectMany(p => p).ToArray() where HasConflict(registrationLifestyleGroup.Count(), possibleConflictingProducers) select possibleConflictingProducers;
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)); }
public void RegisterCollection <TService>(params TService[] singletons) where TService : class { Requires.IsNotNull(singletons, nameof(singletons)); Requires.DoesNotContainNullValues(singletons, nameof(singletons)); if (typeof(TService) == typeof(Type) && singletons.Any()) { throw new ArgumentException( StringResources.RegisterCollectionCalledWithTypeAsTService(singletons.Cast <Type>()), nameof(TService)); } Requires.IsNotAnAmbiguousType(typeof(TService), nameof(TService)); var singletonRegistrations = from singleton in singletons select SingletonLifestyle.CreateSingleInstanceRegistration(typeof(TService), singleton, this, singleton.GetType()); this.RegisterCollection(typeof(TService), singletonRegistrations); }
public ILifestyle CreateSingletonLifestyle(ServiceEntry entry) { ILifestyle lifestyle = new SingletonLifestyle(_activatorFactory, entry); lifestyle.Initialize(); return lifestyle; }