private ContainerControlledCollection <TService> CreateInternal <TService>( IEnumerable <Type> serviceTypes) where TService : class { Requires.IsNotAnAmbiguousType(typeof(TService), nameof(TService)); Requires.IsNotNull(serviceTypes, nameof(serviceTypes)); // Make a copy for correctness and performance. serviceTypes = serviceTypes.ToArray(); Requires.DoesNotContainNullValues(serviceTypes, nameof(serviceTypes)); Requires.ServiceIsAssignableFromImplementations(typeof(TService), serviceTypes, nameof(serviceTypes), typeCanBeServiceType: true); Requires.DoesNotContainOpenGenericTypesWhenServiceTypeIsNotGeneric(typeof(TService), serviceTypes, nameof(serviceTypes)); Requires.OpenGenericTypesDoNotContainUnresolvableTypeArguments(typeof(TService), serviceTypes, nameof(serviceTypes)); var collection = new ContainerControlledCollection <TService>(this.container); collection.AppendAll(serviceTypes); this.RegisterForVerification(collection); return(collection); }
/// <summary> /// Registers a dynamic (container uncontrolled) collection of elements of type /// <typeparamref name="TService"/>. A call to <see cref="GetAllInstances{T}"/> will return the /// <paramref name="containerUncontrolledCollection"/> itself, and updates to the collection will be /// reflected in the result. If updates are allowed, make sure the collection can be iterated safely /// if you're running a multi-threaded application. /// </summary> /// <typeparam name="TService">The interface or base type that can be used to retrieve instances.</typeparam> /// <param name="containerUncontrolledCollection">The container-uncontrolled collection to register.</param> /// <exception cref="InvalidOperationException"> /// Thrown when this container instance is locked and can not be altered, or when a <paramref name="containerUncontrolledCollection"/> /// for <typeparamref name="TService"/> has already been registered. /// </exception> /// <exception cref="ArgumentNullException">Thrown when <paramref name="containerUncontrolledCollection"/> is a null /// reference.</exception> public void RegisterCollection <TService>(IEnumerable <TService> containerUncontrolledCollection) where TService : class { Requires.IsNotAnAmbiguousType(typeof(TService), nameof(TService)); Requires.IsNotNull(containerUncontrolledCollection, nameof(containerUncontrolledCollection)); this.RegisterContainerUncontrolledCollection(typeof(TService), containerUncontrolledCollection); }
public void RegisterSingle <TService>(Func <TService> instanceCreator) where TService : class { Requires.IsNotNull(instanceCreator, "instanceCreator"); Requires.IsNotAnAmbiguousType(typeof(TService), "TService"); // Forward the call. This allows external NuGet packages that depend on this method to keep working. this.Register <TService>(instanceCreator, Lifestyle.Singleton); }
/// <summary> /// Allows appending new registrations to existing registrations made using one of the /// <b>RegisterCollection</b> overloads. /// </summary> /// <param name="serviceType">The service type of the collection.</param> /// <param name="implementationType">The implementation type to append.</param> /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null /// reference (Nothing in VB).</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="serviceType"/> is not a /// reference type, or ambiguous.</exception> /// <exception cref="InvalidOperationException">Thrown when the container is locked.</exception> /// <exception cref="NotSupportedException">Thrown when the method is called for a registration /// that is made with one of the <b>RegisterCollection</b> overloads that accepts a dynamic collection /// (an <b>IEnumerable</b> or <b>IEnumerable<TService></b>).</exception> public void AppendTo(Type serviceType, Type implementationType) { Requires.IsNotNull(serviceType, nameof(serviceType)); Requires.IsNotNull(implementationType, nameof(implementationType)); Requires.IsReferenceType(serviceType, nameof(serviceType)); Requires.IsNotAnAmbiguousType(serviceType, nameof(serviceType)); Requires.ServiceOrItsGenericTypeDefinitionIsAssignableFromImplementation(serviceType, implementationType, nameof(implementationType)); Requires.OpenGenericTypesDoNotContainUnresolvableTypeArguments(serviceType, new[] { implementationType }, nameof(implementationType)); this.container.AppendToCollectionInternal(serviceType, implementationType); }
/// <summary> /// Allows appending new registrations to existing registrations made using one of the /// <b>RegisterCollection</b> overloads. /// </summary> /// <param name="serviceType">The service type of the collection.</param> /// <param name="registration">The registration to append.</param> /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null /// reference (Nothing in VB).</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="serviceType"/> is not a /// reference type, is open generic, or ambiguous.</exception> /// <exception cref="InvalidOperationException">Thrown when the container is locked.</exception> /// <exception cref="NotSupportedException">Thrown when the method is called for a registration /// that is made with one of the <b>RegisterCollection</b> overloads that accepts a dynamic collection /// (an <b>IEnumerable</b> or <b>IEnumerable<TService></b>).</exception> public void AppendTo(Type serviceType, Registration registration) { Requires.IsNotNull(serviceType, nameof(serviceType)); Requires.IsNotNull(registration, nameof(registration)); Requires.IsReferenceType(serviceType, nameof(serviceType)); Requires.IsNotAnAmbiguousType(serviceType, nameof(serviceType)); Requires.IsRegistrationForThisContainer(this.container, registration, nameof(registration)); Requires.ServiceOrItsGenericTypeDefinitionIsAssignableFromImplementation(serviceType, registration.ImplementationType, nameof(registration)); Requires.OpenGenericTypesDoNotContainUnresolvableTypeArguments(serviceType, new[] { registration }, nameof(registration)); this.container.AppendToCollectionInternal(serviceType, registration); }
/// <summary> /// Registers a dynamic (container uncontrolled) collection of elements of type /// <paramref name="serviceType"/>. A call to <see cref="GetAllInstances{T}"/> will return the /// <paramref name="containerUncontrolledCollection"/> itself, and updates to the collection will be /// reflected in the result. If updates are allowed, make sure the collection can be iterated safely /// if you're running a multi-threaded application. /// </summary> /// <param name="serviceType">The base type or interface for elements in the collection.</param> /// <param name="containerUncontrolledCollection">The collection of items to register.</param> /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null /// reference (Nothing in VB).</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="serviceType"/> represents an /// open generic type.</exception> public void RegisterCollection(Type serviceType, IEnumerable containerUncontrolledCollection) { Requires.IsNotNull(serviceType, nameof(serviceType)); Requires.IsNotNull(containerUncontrolledCollection, nameof(containerUncontrolledCollection)); Requires.IsNotOpenGenericType(serviceType, nameof(serviceType)); Requires.IsNotAnAmbiguousType(serviceType, nameof(serviceType)); try { this.RegisterContainerUncontrolledCollection(serviceType, containerUncontrolledCollection.Cast <object>()); } catch (MemberAccessException ex) { // This happens when the user tries to resolve an internal type inside a (Silverlight) sandbox. throw new ArgumentException( StringResources.UnableToResolveTypeDueToSecurityConfiguration(serviceType, ex) + Environment.NewLine + "paramName: " + nameof(serviceType), ex); } }
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); }
private ContainerControlledCollection <TService> CreateInternal <TService>( IEnumerable <Registration> registrations) where TService : class { Requires.IsNotAnAmbiguousType(typeof(TService), nameof(TService)); Requires.IsNotNull(registrations, nameof(registrations)); Requires.DoesNotContainNullValues(registrations, nameof(registrations)); Requires.AreRegistrationsForThisContainer(this.container, registrations, nameof(registrations)); Requires.ServiceIsAssignableFromImplementations(typeof(TService), registrations, nameof(registrations), typeCanBeServiceType: true); Requires.OpenGenericTypesDoNotContainUnresolvableTypeArguments(typeof(TService), registrations, nameof(registrations)); var collection = new ContainerControlledCollection <TService>(this.container); collection.AppendAll(registrations); this.RegisterForVerification(collection); return(collection); }