Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename AddResilienceStrategy to AddResilienceStrategyRegistry #1397

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static IServiceCollection AddResilienceStrategy<TKey, TResult>(
});
});

return AddResilienceStrategy<TKey>(services);
return AddResilienceStrategyRegistry<TKey>(services);
}

/// <summary>
Expand Down Expand Up @@ -156,24 +156,47 @@ public static IServiceCollection AddResilienceStrategy<TKey>(
});
});

return AddResilienceStrategy<TKey>(services);
return AddResilienceStrategyRegistry<TKey>(services);
}

/// <summary>
/// Adds the infrastructure that allows configuring and retrieving resilience strategies using the <typeparamref name="TKey"/> key.
/// Adds <see cref="ResilienceStrategyRegistry{TKey}"/> and <see cref="ResilienceStrategyProvider{TKey}"/> that allows configuring
/// and retrieving resilience strategies using the <typeparamref name="TKey"/> key.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience strategy.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience strategy to.</param>
/// <param name="configure">The action that configures the <see cref="ResilienceStrategyRegistryOptions{TKey}"/> that are used by the registry.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with additional services added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> is <see langword="null"/>.</exception>
/// <remarks>
/// You can retrieve the strategy registry by resolving the <see cref="ResilienceStrategyProvider{TKey}"/>
/// or <see cref="ResilienceStrategyRegistry{TKey}"/> class from the dependency injection container.
/// <para>
/// This call enables telemetry for all resilience strategies created using <see cref="ResilienceStrategyRegistry{TKey}"/>.
/// </para>
/// </remarks>
public static IServiceCollection AddResilienceStrategy<TKey>(this IServiceCollection services)
public static IServiceCollection AddResilienceStrategyRegistry<TKey>(
this IServiceCollection services,
Action<ResilienceStrategyRegistryOptions<TKey>> configure)
where TKey : notnull
{
Guard.NotNull(services);
Guard.NotNull(configure);

services.AddResilienceStrategyRegistry<TKey>();
services.Configure(configure);

return services;
}

/// <summary>
/// Adds <see cref="ResilienceStrategyRegistry{TKey}"/> and <see cref="ResilienceStrategyProvider{TKey}"/> that allows configuring
/// and retrieving resilience strategies using the <typeparamref name="TKey"/> key.
/// </summary>
/// <typeparam name="TKey">The type of the key used to identify the resilience strategy.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the resilience strategy to.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with additional services added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="services"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This call enables telemetry for all resilience strategies created using <see cref="ResilienceStrategyRegistry{TKey}"/>.
/// </remarks>
public static IServiceCollection AddResilienceStrategyRegistry<TKey>(this IServiceCollection services)
where TKey : notnull
{
Guard.NotNull(services);
Expand All @@ -188,7 +211,6 @@ public static IServiceCollection AddResilienceStrategy<TKey>(this IServiceCollec
services.AddOptions();
services.Add(RegistryMarker<TKey>.ServiceDescriptor);
services.AddResilienceStrategyBuilder();
services.AddResilienceStrategy<TKey>();

services.TryAddSingleton(serviceProvider =>
{
Expand All @@ -204,10 +226,7 @@ public static IServiceCollection AddResilienceStrategy<TKey>(this IServiceCollec
return registry;
});

services.TryAddSingleton<ResilienceStrategyProvider<TKey>>(serviceProvider =>
{
return serviceProvider.GetRequiredService<ResilienceStrategyRegistry<TKey>>();
});
services.TryAddSingleton<ResilienceStrategyProvider<TKey>>(serviceProvider => serviceProvider.GetRequiredService<ResilienceStrategyRegistry<TKey>>());

// configure options
services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,27 @@ public void AddResilienceStrategy_Multiple_Ok()
}

[Fact]
public void AddResilienceStrategyInfra_Ok()
public void AddResilienceStrategyRegistry_Ok()
{
var provider = new ServiceCollection().AddResilienceStrategy<string>().BuildServiceProvider();
var provider = new ServiceCollection().AddResilienceStrategyRegistry<string>().BuildServiceProvider();

provider.GetRequiredService<ResilienceStrategyRegistry<string>>().Should().NotBeNull();
provider.GetRequiredService<ResilienceStrategyProvider<string>>().Should().NotBeNull();
provider.GetRequiredService<ResilienceStrategyBuilder>().DiagnosticSource.Should().NotBeNull();
}

[Fact]
public void AddResilienceStrategyRegistry_ConfigureCallback_Ok()
{
Func<string, string> formatter = s => s;

var provider = new ServiceCollection().AddResilienceStrategyRegistry<string>(options => options.InstanceNameFormatter = formatter).BuildServiceProvider();

provider.GetRequiredService<ResilienceStrategyRegistry<string>>().Should().NotBeNull();
provider.GetRequiredService<ResilienceStrategyProvider<string>>().Should().NotBeNull();
provider.GetRequiredService<IOptions<ResilienceStrategyRegistryOptions<string>>>().Value.InstanceNameFormatter.Should().Be(formatter);
}

private void AddResilienceStrategy(string key, Action<ResilienceStrategyBuilderContext>? onBuilding = null)
{
_services.AddResilienceStrategy(key, builder =>
Expand Down