Skip to content

Commit

Permalink
Add TryAddDefaultAWSOptions methods and unit tests
Browse files Browse the repository at this point in the history
Introduce TryAddDefaultAWSOptions methods to ServiceCollectionExtensions for adding AWSOptions without overriding existing registrations. Add unit tests to verify the new methods' behavior. Enhance dependency injection setup for AWS services with added flexibility and robustness.
  • Loading branch information
paulomorgado authored and dscpinheiro committed Sep 23, 2024
1 parent 4999792 commit 304ad72
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ public static IServiceCollection AddDefaultAWSOptions(
return collection;
}

/// <summary>
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients if they haven't already been registered.
/// </summary>
/// <param name="collection"></param>
/// <param name="options">The default AWS options used to construct AWS service clients with.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddDefaultAWSOptions(this IServiceCollection collection, AWSOptions options)
{
collection.TryAdd(new ServiceDescriptor(typeof(AWSOptions), options));
return collection;
}

/// <summary>
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients if they haven't already been registered.
/// </summary>
/// <param name="collection"></param>
/// <param name="implementationFactory">The factory that creates the default AWS options.
/// The AWS options will be used to construct AWS service clients
/// </param>
/// <param name="lifetime">The lifetime of the AWSOptions. The default is Singleton.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddDefaultAWSOptions(
this IServiceCollection collection,
Func<IServiceProvider, AWSOptions> implementationFactory,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.TryAdd(new ServiceDescriptor(typeof(AWSOptions), implementationFactory, lifetime));
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
Expand Down
42 changes: 42 additions & 0 deletions extensions/test/NETCore.SetupTests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,48 @@ namespace DependencyInjectionTests
{
public class DependencyInjectionTests
{
[Fact]
public void TryAddDefaultConfigDontOverrideWhenAlreadySetup()
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("./TestFiles/GetClientConfigSettingsTest.json");

IConfiguration config = builder.Build();

ServiceCollection services = new ServiceCollection();

var mockOptions = Mock.Of<AWSOptions>();

services.AddDefaultAWSOptions(mockOptions);
services.TryAddDefaultAWSOptions(config.GetAWSOptions());

var serviceProvider = services.BuildServiceProvider();

var options = serviceProvider.GetService<AWSOptions>();
Assert.True(object.ReferenceEquals(options, mockOptions));
}

[Fact]
public void TryAddDefaultConfigWithFunctionDontOverrideWhenAlreadySetup()
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("./TestFiles/GetClientConfigSettingsTest.json");

IConfiguration config = builder.Build();

ServiceCollection services = new ServiceCollection();

var mockOptions = Mock.Of<AWSOptions>();

services.AddDefaultAWSOptions(mockOptions);
services.TryAddDefaultAWSOptions(s => config.GetAWSOptions());

var serviceProvider = services.BuildServiceProvider();

var options = serviceProvider.GetService<AWSOptions>();
Assert.True(object.ReferenceEquals(options, mockOptions));
}

[Fact]
public void InjectS3ClientWithDefaultConfig()
{
Expand Down

0 comments on commit 304ad72

Please sign in to comment.