Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
- Cache Parameter values once they've been retrieved
  • Loading branch information
eerhardt committed Mar 6, 2024
1 parent 29a45fd commit 6f49f25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed class InputAnnotation : IResourceAnnotation, IValueProvider
{
private string? _value;
private bool _hasValue;
private Func<string>? _valueGetter;

/// <summary>
/// Initializes a new instance of <see cref="InputAnnotation"/>.
Expand Down Expand Up @@ -45,23 +46,28 @@ public InputAnnotation(string name, bool secret = false)
/// </summary>
public InputDefault? Default { get; set; }

internal Func<string>? ValueGetter { get; set; }

ValueTask<string?> IValueProvider.GetValueAsync(CancellationToken cancellationToken)
internal string? Value
{
if (!_hasValue)
get
{
_value = GenerateValue();
_hasValue = true;
if (!_hasValue)
{
_value = GenerateValue();
_hasValue = true;
}
return _value;
}
return new(_value);
}

ValueTask<string?> IValueProvider.GetValueAsync(CancellationToken cancellationToken) => new(Value);

internal void SetValueGetter(Func<string> valueGetter) => _valueGetter = valueGetter;

private string GenerateValue()
{
if (ValueGetter is not null)
if (_valueGetter is not null)
{
return ValueGetter();
return _valueGetter();
}

if (Default is null)
Expand Down
5 changes: 3 additions & 2 deletions src/Aspire.Hosting/ApplicationModel/ParameterResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public sealed class ParameterResource : Resource, IManifestExpressionProvider, I
public ParameterResource(string name, Func<string> callback, bool secret = false) : base(name)
{
_valueInput = new InputAnnotation("value", secret);
_valueInput.ValueGetter = callback;
_valueInput.SetValueGetter(callback);

Annotations.Add(_valueInput);

ValueInputReference = new InputReference(this, "value");
Expand All @@ -28,7 +29,7 @@ public ParameterResource(string name, Func<string> callback, bool secret = false
/// <summary>
/// Gets the value of the parameter.
/// </summary>
public string Value => _valueInput.ValueGetter!();
public string Value => _valueInput.Value ?? throw new InvalidOperationException("A Parameter's value cannot be null.");

/// <summary>
/// Gets a value indicating whether the parameter is secret.
Expand Down

0 comments on commit 6f49f25

Please sign in to comment.