Skip to content

Commit 592ede5

Browse files
committed
Slimmer IOptions<T>
- Use a Lazy instead of the IOptionsCache (which is a concurrent dictionary)
1 parent 2f2113e commit 592ede5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static IServiceCollection AddOptions(this IServiceCollection services)
2626
throw new ArgumentNullException(nameof(services));
2727
}
2828

29-
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>)));
29+
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(UnnamedOptionsManager<>)));
3030
services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsManager<>)));
3131
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));
3232
services.TryAdd(ServiceDescriptor.Transient(typeof(IOptionsFactory<>), typeof(OptionsFactory<>)));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
namespace Microsoft.Extensions.Options
8+
{
9+
internal class UnnamedOptionsManager<[DynamicallyAccessedMembers(Options.DynamicallyAccessedMembers)] TOptions> :
10+
IOptions<TOptions>
11+
where TOptions : class
12+
{
13+
private readonly Lazy<TOptions> _lazy;
14+
15+
public UnnamedOptionsManager(IOptionsFactory<TOptions> factory)
16+
{
17+
_lazy = new Lazy<TOptions>(() => factory.Create(Options.DefaultName));
18+
}
19+
20+
/// <summary>
21+
/// The default configured <typeparamref name="TOptions"/> instance, equivalent to Get(Options.DefaultName).
22+
/// </summary>
23+
public TOptions Value => _lazy.Value;
24+
}
25+
}

0 commit comments

Comments
 (0)