/// <summary> /// Registers an <paramref name="implementationType"/> for generation with the /// <see cref="RegistrationSetupBase{TDerived}.Registrations"/>. /// </summary> /// <param name="serviceType"> /// The type of the service to by satisfied during registration. The <paramref name="serviceType"/> should be /// satisfied by being <see cref="TypeInfo.IsAssignableFrom(TypeInfo)"/> the /// <paramref name="implementationType"/>. /// </param> /// <param name="implementationType">The type of the implemented service to provide.</param> /// <param name="compose">The action to further compose the registration.</param> /// <returns><see langword="this"/> context to be used in a fluent configuration.</returns> public RegistrationSetup <TExtra> Register( Type serviceType, Type implementationType, Action <RegistrationComposerExtra <TExtra> > compose = null) { if (implementationType == null) { throw new ArgumentNullException(nameof(implementationType)); } if (serviceType == null) { throw new ArgumentNullException(nameof(serviceType)); } IRegistration defaultRegistration = new SingleConstructorRegistration(implementationType); if (compose == null) { return(Register(serviceType, defaultRegistration)); } var composer = new RegistrationComposerExtra <TExtra>(defaultRegistration); compose(composer); return(Register(serviceType, composer.Registration)); }
/// <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> /// Specifies that all the properties of an <typeparamref name="TImplementation"/> need 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 properties. /// </typeparam> /// <param name="composer">The registration composer.</param> /// <returns>This registration composer to be used in a fluent configuration.</returns> public static RegistrationComposerExtra <TExtra, TImplementation> InjectAllProperties <TExtra, TImplementation>( this RegistrationComposerExtra <TExtra, TImplementation> composer) { if (composer == null) { throw new ArgumentNullException(nameof(composer)); } ((RegistrationComposerExtra <TExtra>)composer).InjectAllProperties(); return(composer); }
/// <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> /// Registers an <typeparamref name="TImplementation"/> for generation with the /// <see cref="RegistrationSetupBase{TDerived}.Registrations"/>. /// </summary> /// <typeparam name="TService"> /// The type of the service to by satisfied during registration. The <typeparamref name="TService"/> should be /// satisfied by being <see cref="TypeInfo.IsAssignableFrom(TypeInfo)"/> the /// <typeparamref name="TImplementation"/>. /// </typeparam> /// <typeparam name="TImplementation">The type of the implemented service.</typeparam> /// <param name="compose">The action to further compose the registration.</param> /// <returns><see langword="this"/> context to be used in a fluent configuration.</returns> public RegistrationSetup <TExtra> Register <TService, TImplementation>( Action <RegistrationComposerExtra <TExtra, TImplementation> > compose = null) where TImplementation : TService { IRegistration defaultRegistration = new SingleConstructorRegistration(typeof(TImplementation)); if (compose == null) { return(Register(typeof(TService), defaultRegistration)); } var composer = new RegistrationComposerExtra <TExtra, TImplementation>(defaultRegistration); compose(composer); return(Register(typeof(TService), composer.Registration)); }
/// <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); }