/// <summary> /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposerExtra{T}.Registration"/> with a /// <see cref="SingletonRegistration"/>. /// </summary> /// <typeparam name="TExtra"> /// The type of the <see cref="ConstructionContext{TExtra}.Extra"/> construction context information. /// </typeparam> /// <param name="composer">The registration composer.</param> public static void ToSingleton <TExtra>(this RegistrationComposerExtra <TExtra> composer) { if (composer == null) { throw new ArgumentNullException(nameof(composer)); } composer.Replace(new SingletonRegistration(composer.Registration)); }
/// <summary> /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposerExtra{T}.Registration"/> with a /// <see cref="TypedFactoryRegistration{TImplementation}"/>. /// </summary> /// <typeparam name="TExtra"> /// The type of the <see cref="ConstructionContext{TExtra}.Extra"/> construction context information. /// </typeparam> /// <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 RegistrationComposerExtra <TExtra, TImplementation> UseFactory <TExtra, TImplementation>( this RegistrationComposerExtra <TExtra, TImplementation> composer, Func <ConstructionContext <TExtra>, TImplementation> factory) { if (composer == null) { throw new ArgumentNullException(nameof(composer)); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } composer.Replace(new TypedFactoryRegistration <TExtra, TImplementation>(factory)); return(composer); }
/// <summary> /// Replaces the <paramref name="composer"/>.<see cref="RegistrationComposerExtra{T}.Registration"/> with a /// <see cref="FactoryRegistration"/>. /// </summary> /// <typeparam name="TExtra"> /// The type of the <see cref="ConstructionContext{TExtra}.Extra"/> construction context information. /// </typeparam> /// <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 RegistrationComposerExtra <TExtra> UseFactory <TExtra>( this RegistrationComposerExtra <TExtra> composer, Type implementationType, Func <ConstructionContext <TExtra>, object> factory) { if (composer == null) { throw new ArgumentNullException(nameof(composer)); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } composer.Replace(new FactoryRegistration <TExtra>(implementationType, factory)); return(composer); }
/// <summary> /// Specifies that all the properties of a service need to be injected as a dependency. /// </summary> /// <typeparam name="TExtra"> /// The type of the <see cref="ConstructionContext{TExtra}.Extra"/> construction context information. /// </typeparam> /// <param name="composer">The registration composer.</param> /// <returns>This registration composer to be used in a fluent configuration.</returns> public static RegistrationComposerExtra <TExtra> InjectAllProperties <TExtra>( this RegistrationComposerExtra <TExtra> 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); }
/// <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="TExtra"> /// The type of the <see cref="ConstructionContext{TExtra}.Extra"/> construction context information. /// </typeparam> /// <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 RegistrationComposerExtra <TExtra, TImplementation> InjectProperty <TExtra, TImplementation, TProp>( this RegistrationComposerExtra <TExtra, 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); }