| | | 1 | | using Amazon; |
| | | 2 | | using Amazon.DynamoDBv2; |
| | | 3 | | using Amazon.DynamoDBv2.DataModel; |
| | | 4 | | using Amazon.Runtime; |
| | | 5 | | using ArturRios.Data.DynamoDb.Configuration; |
| | | 6 | | using ArturRios.Data.DynamoDb.Interfaces; |
| | | 7 | | using ArturRios.Data.DynamoDb.Repositories; |
| | | 8 | | using Microsoft.Extensions.Configuration; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | |
| | | 11 | | namespace ArturRios.Data.DynamoDb.DependencyInjection; |
| | | 12 | | |
| | | 13 | | /// <summary>Dependency-injection registration for the ArturRios.Data.DynamoDb store.</summary> |
| | | 14 | | public static class ServiceCollectionExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary>Builds an <see cref="IAmazonDynamoDB" /> client from the given options.</summary> |
| | | 17 | | /// <param name="options">The DynamoDB options.</param> |
| | | 18 | | private static IAmazonDynamoDB CreateClient(DynamoOptions options) |
| | 2 | 19 | | { |
| | 2 | 20 | | var hasKeys = !string.IsNullOrEmpty(options.AccessKey) && !string.IsNullOrEmpty(options.SecretKey); |
| | 2 | 21 | | var credentials = hasKeys ? new BasicAWSCredentials(options.AccessKey, options.SecretKey) : null; |
| | | 22 | | |
| | 2 | 23 | | if (!string.IsNullOrEmpty(options.ServiceUrl)) |
| | 1 | 24 | | { |
| | 1 | 25 | | var config = new AmazonDynamoDBConfig { ServiceURL = options.ServiceUrl }; |
| | | 26 | | |
| | 1 | 27 | | if (!string.IsNullOrEmpty(options.Region)) |
| | 1 | 28 | | { |
| | 1 | 29 | | config.AuthenticationRegion = options.Region; |
| | 1 | 30 | | } |
| | | 31 | | |
| | 1 | 32 | | return new AmazonDynamoDBClient(credentials ?? new BasicAWSCredentials("dummy", "dummy"), config); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // no ServiceUrl -> real AWS |
| | 1 | 36 | | if (string.IsNullOrEmpty(options.Region)) |
| | 0 | 37 | | { |
| | | 38 | | // Defer region resolution to the SDK's default chain (env / profile / instance metadata). |
| | 0 | 39 | | return credentials is null ? new AmazonDynamoDBClient() : new AmazonDynamoDBClient(credentials); |
| | | 40 | | } |
| | | 41 | | |
| | 1 | 42 | | var region = RegionEndpoint.GetBySystemName(options.Region); |
| | 1 | 43 | | return credentials is null ? new AmazonDynamoDBClient(region) : new AmazonDynamoDBClient(credentials, region); |
| | 2 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <param name="services">The service collection.</param> |
| | | 47 | | extension(IServiceCollection services) |
| | | 48 | | { |
| | | 49 | | /// <summary>Registers the DynamoDB store, binding options from configuration.</summary> |
| | | 50 | | /// <param name="configuration">Application configuration.</param> |
| | | 51 | | /// <param name="sectionName">Configuration section holding the options. Defaults to "ArturRios.Data.DynamoDb".< |
| | | 52 | | public IServiceCollection AddDynamoData(IConfiguration configuration, |
| | | 53 | | string sectionName = "ArturRios.Data.DynamoDb") |
| | 0 | 54 | | { |
| | 0 | 55 | | var options = configuration.GetSection(sectionName).Get<DynamoOptions>() ?? new DynamoOptions(); |
| | 0 | 56 | | return services.AddDynamoData(options); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary>Registers the DynamoDB store from an explicit options instance.</summary> |
| | | 60 | | /// <param name="options">The DynamoDB options.</param> |
| | | 61 | | public IServiceCollection AddDynamoData(DynamoOptions options) |
| | 2 | 62 | | { |
| | 4 | 63 | | services.AddSingleton<IAmazonDynamoDB>(_ => CreateClient(options)); |
| | 2 | 64 | | services.AddSingleton<IDynamoDBContext>(sp => |
| | 3 | 65 | | new DynamoDBContextBuilder() |
| | 3 | 66 | | .WithDynamoDBClient(sp.GetRequiredService<IAmazonDynamoDB>) |
| | 3 | 67 | | .Build()); |
| | 2 | 68 | | services.AddScoped(typeof(IAsyncDynamoRepository<>), typeof(DynamoRepository<>)); |
| | | 69 | | |
| | 2 | 70 | | return services; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |