Example #1
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="serviceType"/>  using the supplied <paramref name="instanceCreator"/>
        /// with the caching as specified by this lifestyle.
        /// </summary>
        /// <param name="serviceType">The interface or base type that can be used to retrieve the instances.</param>
        /// <param name="instanceCreator">The delegate that will be responsible for creating new instances.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type serviceType, Func <object> instanceCreator,
                                               Container container)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(instanceCreator, nameof(instanceCreator));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsReferenceType(serviceType, nameof(serviceType));
            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));

            var closedCreateRegistrationMethod = OpenCreateRegistrationTServiceFuncMethod
                                                 .MakeGenericMethod(serviceType);

            try
            {
                // Build the following delegate: () => (ServiceType)instanceCreator();
                var typeSafeInstanceCreator = ConvertDelegateToTypeSafeDelegate(serviceType, instanceCreator);

                return((Registration)closedCreateRegistrationMethod.Invoke(this,
                                                                           new object[] { typeSafeInstanceCreator, container }));
            }
            catch (MemberAccessException ex)
            {
                throw BuildUnableToResolveTypeDueToSecurityConfigException(serviceType, ex, nameof(serviceType));
            }
        }
Example #2
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="implementationType"/> with the caching as specified by this lifestyle.
        /// This method might fail when run in a partial trust sandbox when <paramref name="implementationType"/>
        /// is an internal type.
        /// </summary>
        /// <param name="serviceType">The interface or base type that can be used to retrieve the instances.</param>
        /// <param name="implementationType">The concrete type that will be registered.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type serviceType, Type implementationType, Container container)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(implementationType, nameof(implementationType));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsReferenceType(serviceType, nameof(serviceType));
            Requires.IsReferenceType(implementationType, nameof(implementationType));

            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));
            Requires.IsNotOpenGenericType(implementationType, nameof(implementationType));

            Requires.ServiceIsAssignableFromImplementation(serviceType, implementationType,
                                                           nameof(implementationType));

            var closedCreateRegistrationMethod = OpenCreateRegistrationTServiceTImplementationMethod
                                                 .MakeGenericMethod(serviceType, implementationType);

            try
            {
                return((Registration)closedCreateRegistrationMethod.Invoke(this, new object[] { container }));
            }
            catch (MemberAccessException ex)
            {
                throw BuildUnableToResolveTypeDueToSecurityConfigException(implementationType, ex,
                                                                           nameof(implementationType));
            }
        }
Example #3
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="concreteType"/> with the caching as specified by this lifestyle,
        /// or returns an already created <see cref="Registration"/> instance for this container + lifestyle
        /// + type combination.
        /// This method might fail when run in a partial trust sandbox when <paramref name="concreteType"/>
        /// is an internal type.
        /// </summary>
        /// <param name="concreteType">The concrete type that will be registered.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type concreteType, Container container)
        {
            Requires.IsNotNull(concreteType, nameof(concreteType));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsReferenceType(concreteType, nameof(concreteType));

            Requires.IsNotOpenGenericType(concreteType, nameof(concreteType));

            return(this.CreateRegistrationInternal(concreteType, container, preventTornLifestyles: true));
        }
        /// <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&lt;TService&gt;</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&lt;TService&gt;</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);
        }