Skip to content

Commit b7c2985

Browse files
authored
Merge pull request #2 from albumprinter/feature/service-collection-imps
Implemented ServiceCollection Helper Properly and Added Related Tests
2 parents 3105b18 + df018c7 commit b7c2985

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

src/Albelli.Extensions.Caching.DynamoDb/DynamoDbCache.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Amazon.DynamoDBv2;
77
using Amazon.DynamoDBv2.Model;
88
using Microsoft.Extensions.Caching.Distributed;
9+
using Microsoft.Extensions.Options;
910

1011
namespace Albelli.Extensions.Caching.DynamoDb
1112
{
@@ -15,9 +16,9 @@ public sealed class DynamoDbCache : IDistributedCache
1516
private readonly IAmazonDynamoDB _dynamoDb;
1617
private readonly ISystemClock _systemClock;
1718

18-
public DynamoDbCache(DynamoDbCacheOptions options, ISystemClock systemClock, IAmazonDynamoDB dynamoDb = null)
19+
public DynamoDbCache(IOptions<DynamoDbCacheOptions> options, ISystemClock systemClock, IAmazonDynamoDB dynamoDb = null)
1920
{
20-
_options = options ?? throw new ArgumentNullException(nameof(options));
21+
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
2122
if (_options.CustomDynamoDbClient != null)
2223
{
2324
_dynamoDb = _options.CustomDynamoDbClient;

src/Albelli.Extensions.Caching.DynamoDb/DynamoDbCacheServiceCollectionExtensions.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ namespace Albelli.Extensions.Caching.DynamoDb
66
{
77
public static class DynamoDbCacheServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services, Action<DynamoDbCacheOptions> setupAction)
9+
/// <summary>
10+
/// Use DynamoDb as a provider for the IDistributedCache.
11+
/// </summary>
12+
/// <param name="services">The service collection to add this to.</param>
13+
/// <param name="setupAction">The configuration object for this implementation.</param>
14+
/// <returns>A chain result with the same service collection.</returns>
15+
/// <exception cref="ArgumentNullException"></exception>
16+
public static IServiceCollection AddDynamoDbCache(this IServiceCollection services, Action<DynamoDbCacheOptions> setupAction)
1017
{
1118
if (services == null)
1219
{

test/Albelli.Extensions.Caching.Tests.Integration/Albelli.Extensions.Caching.Tests.Integration.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1313
<PackageReference Include="coverlet.collector" Version="1.2.0" />
1414
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.6" />
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.6" />
1516
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.106.39" />
17+
<PackageReference Include="Moq" Version="4.14.5" />
1618
</ItemGroup>
1719

1820
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Linq;
3+
using Albelli.Extensions.Caching.DynamoDb;
4+
using Amazon.DynamoDBv2;
5+
using Microsoft.Extensions.Caching.Distributed;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Moq;
8+
using Xunit;
9+
10+
namespace Albelli.Extensions.Caching.Integration
11+
{
12+
public class DynamoDbCacheServiceCollectionExtensionsTests
13+
{
14+
[Fact]
15+
public void DynamoDb_Is_Registered_Correctly()
16+
{
17+
var serviceCollection = new ServiceCollection();
18+
serviceCollection.AddDynamoDbCache(o =>
19+
{
20+
o.TableName = "test";
21+
});
22+
var distributedCache = serviceCollection.FirstOrDefault(desc => desc.ServiceType == typeof(IDistributedCache));
23+
Assert.NotNull(distributedCache);
24+
Assert.Equal(ServiceLifetime.Singleton, distributedCache.Lifetime);
25+
var systemClock = serviceCollection.FirstOrDefault(desc => desc.ServiceType == typeof(ISystemClock));
26+
Assert.NotNull(systemClock);
27+
Assert.Equal(ServiceLifetime.Singleton, systemClock.Lifetime);
28+
}
29+
30+
[Fact]
31+
public void ServiceProvider_Throws_Without_A_DynamoDbClient_Registered_In_the_DI()
32+
{
33+
var serviceCollection = new ServiceCollection();
34+
serviceCollection.AddDynamoDbCache(o =>
35+
{
36+
o.TableName = "test";
37+
});
38+
var serviceProvider = serviceCollection.BuildServiceProvider();
39+
Assert.Throws<ArgumentNullException>(() => serviceProvider.GetService(typeof(IDistributedCache)));
40+
}
41+
42+
[Fact]
43+
public void ServiceProvider_Is_Retrieved_Successfully_With_A_DynamoDbClient_Registered_In_the_DI()
44+
{
45+
var serviceCollection = new ServiceCollection();
46+
serviceCollection.AddDynamoDbCache(o =>
47+
{
48+
o.TableName = "test";
49+
});
50+
serviceCollection.AddSingleton<IAmazonDynamoDB>(m => new Mock<AmazonDynamoDBClient>().Object);
51+
var serviceProvider = serviceCollection.BuildServiceProvider();
52+
Assert.NotNull(serviceProvider.GetService(typeof(IDistributedCache)));
53+
}
54+
55+
[Fact]
56+
public void ServiceProvider_Is_Retrieved_Successfully_With_A_Manually_Provided_DynamoDBClient()
57+
{
58+
var serviceCollection = new ServiceCollection();
59+
serviceCollection.AddDynamoDbCache(o =>
60+
{
61+
o.TableName = "test";
62+
o.CustomDynamoDbClient = new Mock<AmazonDynamoDBClient>().Object;
63+
});
64+
var serviceProvider = serviceCollection.BuildServiceProvider();
65+
Assert.NotNull(serviceProvider.GetService(typeof(IDistributedCache)));
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)