예제 #1
0
        /// <summary>
        /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposer.Registration"/> with a
        /// <see cref="SingletonRegistration"/>.
        /// </summary>
        /// <param name="composer">The registration composer.</param>
        public static void ToSingleton(this RegistrationComposer composer)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }

            composer.Replace(new SingletonRegistration(composer.Registration));
        }
        /// <summary>
        /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposer.Registration"/> with a
        /// <see cref="InjectedSingletonRegistration{TImplementation}"/>.
        /// </summary>
        /// <typeparam name="TImplementation">The type of the <paramref name="value"/>.</typeparam>
        /// <param name="composer">The registration composer.</param>
        /// <param name="value">
        /// The <see cref="InjectedSingletonRegistration{TImplementation}.Value"/> of type
        /// <typeparamref name="TImplementation"/>.
        /// </param>
        /// <returns>The registration <paramref name="composer"/> to be used in a fluent configuration.</returns>
        public static RegistrationComposer <TImplementation> UseFixed <TImplementation>(
            this RegistrationComposer <TImplementation> composer,
            TImplementation value)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }

            composer.Replace(new InjectedSingletonRegistration <TImplementation>(value));
            return(composer);
        }
예제 #3
0
        /// <summary>
        /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposer.Registration"/> with a
        /// <see cref="TypedFactoryRegistration{TImplementation}"/>.
        /// </summary>
        /// <typeparam name="TImplementation">
        /// The type of the value provided by the <paramref name="factory"/>.
        /// </typeparam>
        /// <param name="composer">The registration composer.</param>
        /// <param name="factory">
        /// The factory function that produces services of type <typeparamref name="TImplementation"/>.
        /// </param>
        /// <returns>The registration <paramref name="composer"/> to be used in a fluent configuration.</returns>
        public static RegistrationComposer <TImplementation> UseFactory <TImplementation>(
            this RegistrationComposer <TImplementation> composer,
            Func <TImplementation> factory)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            composer.Replace(new TypedFactoryRegistration <TImplementation>(factory));
            return(composer);
        }
예제 #4
0
        /// <summary>
        /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposer.Registration"/> with a
        /// <see cref="FactoryRegistration"/>.
        /// </summary>
        /// <param name="composer">The registration composer.</param>
        /// <param name="implementationType">The type of the value provided by the <paramref name="factory"/>.</param>
        /// <param name="factory">
        /// The factory function that produces services of type <paramref name="implementationType"/>.
        /// </param>
        /// <returns>The registration <paramref name="composer"/> to be used in a fluent configuration.</returns>
        public static RegistrationComposer UseFactory(
            this RegistrationComposer composer,
            Type implementationType,
            Func <object> factory)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            composer.Replace(new FactoryRegistration(implementationType, factory));
            return(composer);
        }
예제 #5
0
        /// <summary>
        /// Specifies that all the properties of a service need to be injected as a dependency.
        /// </summary>
        /// <param name="composer">The registration composer.</param>
        /// <returns>This registration composer to be used in a fluent configuration.</returns>
        public static RegistrationComposer InjectAllProperties(this RegistrationComposer composer)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }

            if (composer.Registration is PropertyDependencyRegistration)
            {
                string message =
                    $"Cannot inject all properties of '{composer.Registration.ImplementationType}' if existing " +
                    "property injection dependencies have already been registered.";
                throw new RegistrationException(message);
            }

            composer.Replace(new PropertyDependencyRegistration(composer.Registration));
            return(composer);
        }
예제 #6
0
        /// <summary>
        /// Specifies that a <typeparamref name="TProp"/> <paramref name="property"/> of an
        /// <typeparamref name="TImplementation"/> needs to be injected as a dependency.
        /// </summary>
        /// <typeparam name="TImplementation">
        /// The type of the implementation to receive the injected <paramref name="property"/>.
        /// </typeparam>
        /// <typeparam name="TProp">The type of the property to be injected.</typeparam>
        /// <param name="composer">The registration composer.</param>
        /// <param name="property">The expression used to specify the property to inject.</param>
        /// <returns>This registration composer to be used in a fluent configuration.</returns>
        public static RegistrationComposer <TImplementation> InjectProperty <TImplementation, TProp>(
            this RegistrationComposer <TImplementation> composer,
            Expression <Func <TImplementation, TProp> > property)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            if (composer.Registration is PropertyDependencyRegistration registration)
            {
                registration.AddInjectedProperty(property);
            }
            else
            {
                composer.Replace(new PropertyDependencyRegistration(composer.Registration, property));
            }

            return(composer);
        }