/// <summary> /// Registers an <paramref name="implementationType"/> for generation with the <see cref="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 TDerived Register( Type serviceType, Type implementationType, Action <RegistrationComposer> 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 RegistrationComposer(defaultRegistration); compose(composer); return(Register(serviceType, composer.Registration)); }
/// <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> /// Specifies that all the properties of an <typeparamref name="TImplementation"/> need to be injected as a /// dependency. /// </summary> /// <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 RegistrationComposer <TImplementation> InjectAllProperties <TImplementation>( this RegistrationComposer <TImplementation> composer) { if (composer == null) { throw new ArgumentNullException(nameof(composer)); } ((RegistrationComposer)composer).InjectAllProperties(); return(composer); }
/// <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); }
/// <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); }
/// <summary> /// Registers an <typeparamref name="TImplementation"/> for generation with the <see cref="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 TDerived Register <TService, TImplementation>( Action <RegistrationComposer <TImplementation> > compose = null) where TImplementation : TService { IRegistration defaultRegistration = new SingleConstructorRegistration(typeof(TImplementation)); if (compose == null) { return(Register(typeof(TService), defaultRegistration)); } var composer = new RegistrationComposer <TImplementation>(defaultRegistration); compose(composer); return(Register(typeof(TService), composer.Registration)); }
/// <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); }
/// <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); }
/// <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); }