Skip to content

Commit 0b4c719

Browse files
committed
Address review feedback
1 parent 6fd7f10 commit 0b4c719

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/libraries/Microsoft.Extensions.Options/src/OptionsSnapshot.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ namespace Microsoft.Extensions.Options
1212
/// Implementation of <see cref="IOptions{TOptions}"/> and <see cref="IOptionsSnapshot{TOptions}"/>.
1313
/// </summary>
1414
/// <typeparam name="TOptions">Options type.</typeparam>
15-
internal class OptionsSnapshot<[DynamicallyAccessedMembers(Options.DynamicallyAccessedMembers)] TOptions> :
15+
internal sealed class OptionsSnapshot<[DynamicallyAccessedMembers(Options.DynamicallyAccessedMembers)] TOptions> :
1616
IOptionsSnapshot<TOptions>
1717
where TOptions : class
1818
{
19-
private readonly IOptionsMonitor<TOptions> _factory;
19+
private readonly IOptionsMonitor<TOptions> _optionsMonitor;
2020

2121
private volatile ConcurrentDictionary<string, TOptions> _cache;
2222
private volatile TOptions _unnamedOptionsValue;
2323

2424
/// <summary>
2525
/// Initializes a new instance with the specified options configurations.
2626
/// </summary>
27-
/// <param name="factory">The factory to use to create options.</param>
28-
public OptionsSnapshot(IOptionsMonitor<TOptions> factory)
27+
/// <param name="optionsMonitor">The options monitor to use to provide options.</param>
28+
public OptionsSnapshot(IOptionsMonitor<TOptions> optionsMonitor)
2929
{
30-
_factory = factory;
30+
_optionsMonitor = optionsMonitor;
3131
}
3232

3333
/// <summary>
@@ -47,17 +47,17 @@ public TOptions Get(string name)
4747
return value;
4848
}
4949

50-
return _unnamedOptionsValue = _factory.Get(Options.DefaultName);
50+
return _unnamedOptionsValue = _optionsMonitor.Get(Options.DefaultName);
5151
}
5252

5353
var cache = _cache ?? Interlocked.CompareExchange(ref _cache, new(concurrencyLevel: 1, capacity: 5, StringComparer.Ordinal), null) ?? _cache;
5454

5555
#if NETSTANDARD2_1
56-
TOptions options = cache.GetOrAdd(name, static (name, factory) => factory.Get(name), _factory);
56+
TOptions options = cache.GetOrAdd(name, static (name, optionsMonitor) => optionsMonitor.Get(name), _optionsMonitor);
5757
#else
5858
if (!cache.TryGetValue(name, out TOptions options))
5959
{
60-
options = cache.GetOrAdd(name, _factory.Get(name));
60+
options = cache.GetOrAdd(name, _optionsMonitor.Get(name));
6161
}
6262
#endif
6363
return options;

src/libraries/Microsoft.Extensions.Options/tests/Microsoft.Extensions.Options.Tests/OptionsSnapshotTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public void SnapshotOptionsAreCachedPerScope()
168168
{
169169
var options2 = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FakeOptions>>().Value;
170170
Assert.NotEqual(options, options2);
171+
Assert.Equal(4, TestConfigure.ConfigureCount);
171172
var namedOne2 = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FakeOptions>>().Get("1");
172173
Assert.NotEqual(namedOne, namedOne2);
173174
Assert.Equal(4, TestConfigure.ConfigureCount);

0 commit comments

Comments
 (0)