< Summary

Information
Class: Elsa.Workflows.Memory.VariableBlockMetadata
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Memory/Variable.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 128
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Variable()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Memory/Variable.cs

#LineLine coverage
 1using Elsa.Expressions.Helpers;
 2using Elsa.Expressions.Models;
 3using Humanizer;
 4
 5namespace Elsa.Workflows.Memory;
 6
 7/// <summary>
 8/// Represents a variable that references a memory block.
 9/// </summary>
 10public class Variable : MemoryBlockReference
 11{
 12    /// <inheritdoc />
 13    public Variable()
 14    {
 15    }
 16
 17    /// <inheritdoc />
 18    public Variable(string name)
 19    {
 20        Id = GetIdFromName(name);
 21        Name = name;
 22    }
 23
 24    /// <inheritdoc />
 25    public Variable(string name, object? value = null) : this(name)
 26    {
 27        Value = value;
 28    }
 29
 30    public Variable(string name, object? value = null, string? id = null) : this(name, value)
 31    {
 32        if (id != null)
 33        {
 34            Id = id;
 35        }
 36    }
 37
 38    /// <summary>
 39    /// The name of the variable.
 40    /// </summary>
 41    public string Name { get; set; } = null!;
 42
 43    /// <summary>
 44    /// A default value for the variable.
 45    /// </summary>
 46    public object? Value { get; set; }
 47
 48    /// <summary>
 49    /// The storage driver type to use for persistence.
 50    /// If no driver is specified, the referenced memory block will remain in memory for as long as the expression execu
 51    /// </summary>
 52    public Type? StorageDriverType { get; set; }
 53
 54    /// <inheritdoc />
 55    public override MemoryBlock Declare() => new(Value, new VariableBlockMetadata(this, StorageDriverType, false));
 56
 57    private string GetIdFromName(string? name) => $"{name?.Camelize() ?? "Unnamed"}{nameof(Variable)}";
 58}
 59
 60/// <summary>
 61/// Represents a variable that references a memory block.
 62/// </summary>
 63/// <typeparam name="T">The type of the variable.</typeparam>
 64public class Variable<T> : Variable
 65{
 66    /// <inheritdoc />
 67    public Variable()
 68    {
 69    }
 70
 71    /// <inheritdoc />
 72    [Obsolete("Use the constructor that takes a name parameter instead.", true)]
 73    public Variable(T value)
 74    {
 75        Value = value;
 76    }
 77
 78    /// <inheritdoc />
 79    public Variable(string name, T value) : base(name, value)
 80    {
 81    }
 82
 83    public Variable(string name, T value, string? id = null) : base(name, value, id)
 84    {
 85    }
 86
 87    /// <summary>
 88    /// Gets the value of the variable.
 89    /// </summary>
 90    public T? Get(ActivityExecutionContext context) => Get(context.ExpressionExecutionContext).ConvertTo<T?>();
 91
 92    /// <summary>
 93    /// Gets the value of the variable.
 94    /// </summary>
 95    public new T? Get(ExpressionExecutionContext context) => base.Get(context).ConvertTo<T?>();
 96
 97    /// <summary>
 98    /// Sets the <see cref="Variable.StorageDriverType"/> to the specified type.
 99    /// </summary>
 100    public Variable<T> WithStorageDriver<TDriver>() where TDriver:IStorageDriver
 101    {
 102        StorageDriverType = typeof(TDriver);
 103        return this;
 104    }
 105
 106    public Variable<T> WithId(string id)
 107    {
 108        Id = id;
 109        return this;
 110    }
 111
 112    public Variable<T> WithName(string name)
 113    {
 114        Name = name;
 115        return this;
 116    }
 117
 118    public Variable<T> WithValue(T value)
 119    {
 120        Value = value;
 121        return this;
 122    }
 123}
 124
 125/// <summary>
 126/// Provides metadata about the variable block.
 127/// </summary>
 674128public record VariableBlockMetadata(Variable Variable, Type? StorageDriverType, bool IsInitialized);

Methods/Properties

get_Variable()