Make ServiceCollectionExtensions consistent

This commit is contained in:
jacalvar 2016-02-24 22:53:24 -08:00
parent 7d6638bc47
commit 4b348efa59
1 changed files with 13 additions and 20 deletions

View File

@ -8,7 +8,7 @@ using Microsoft.Extensions.Localization;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
/// <summary> /// <summary>
/// Extension methods for adding localization servics to the DI container. /// Extension methods for setting up localization services in an <see cref="IServiceCollection" />.
/// </summary> /// </summary>
public static class LocalizationServiceCollectionExtensions public static class LocalizationServiceCollectionExtensions
{ {
@ -16,24 +16,25 @@ namespace Microsoft.Extensions.DependencyInjection
/// Adds services required for application localization. /// Adds services required for application localization.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns> public static void AddLocalization(this IServiceCollection services)
public static IServiceCollection AddLocalization(this IServiceCollection services)
{ {
if (services == null) if (services == null)
{ {
throw new ArgumentNullException(nameof(services)); throw new ArgumentNullException(nameof(services));
} }
return AddLocalization(services, setupAction: null); services.AddOptions();
services.TryAddSingleton<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>();
services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
} }
/// <summary> /// <summary>
/// Adds services required for application localization. /// Adds services required for application localization.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="setupAction">An action to configure the <see cref="LocalizationOptions"/>.</param> /// <param name="setupAction">An <see cref="Action{LocalizationOptions}"/> to configure the <see cref="LocalizationOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns> public static void AddLocalization(
public static IServiceCollection AddLocalization(
this IServiceCollection services, this IServiceCollection services,
Action<LocalizationOptions> setupAction) Action<LocalizationOptions> setupAction)
{ {
@ -41,21 +42,13 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
throw new ArgumentNullException(nameof(services)); throw new ArgumentNullException(nameof(services));
} }
if (setupAction == null)
services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizerFactory),
typeof(ResourceManagerStringLocalizerFactory),
ServiceLifetime.Singleton));
services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizer<>),
typeof(StringLocalizer<>),
ServiceLifetime.Transient));
if (setupAction != null)
{ {
services.Configure(setupAction); throw new ArgumentNullException(nameof(setupAction));
} }
return services;
services.AddLocalization();
services.Configure(setupAction);
} }
} }
} }