Skip to content

Commit d04a2d4

Browse files
authored
Refactor ParameterResource to use Lazy initialization for value retrieval (#10361)
1 parent e026c5d commit d04a2d4

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

src/Aspire.Hosting/ApplicationModel/ParameterResource.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace Aspire.Hosting.ApplicationModel;
88
/// </summary>
99
public class ParameterResource : Resource, IResourceWithoutLifetime, IManifestExpressionProvider, IValueProvider
1010
{
11-
private string? _value;
12-
private bool _hasValue;
11+
private readonly Lazy<string> _lazyValue;
1312
private readonly Func<ParameterDefault?, string> _valueGetter;
1413
private string? _configurationKey;
1514

@@ -25,6 +24,7 @@ public ParameterResource(string name, Func<ParameterDefault?, string> callback,
2524
ArgumentNullException.ThrowIfNull(callback);
2625

2726
_valueGetter = callback;
27+
_lazyValue = new Lazy<string>(() => _valueGetter(Default));
2828
Secret = secret;
2929
}
3030

@@ -33,18 +33,7 @@ public ParameterResource(string name, Func<ParameterDefault?, string> callback,
3333
/// </summary>
3434
public string Value => GetValueAsync(default).AsTask().GetAwaiter().GetResult()!;
3535

36-
internal string ValueInternal
37-
{
38-
get
39-
{
40-
if (!_hasValue)
41-
{
42-
_value = _valueGetter(Default);
43-
_hasValue = true;
44-
}
45-
return _value!;
46-
}
47-
}
36+
internal string ValueInternal => _lazyValue.Value;
4837

4938
/// <summary>
5039
/// Represents how the default value of the parameter should be retrieved.

0 commit comments

Comments
 (0)