< Summary

Information
Class: Elsa.Expressions.Models.ExpressionExecutionContext
Assembly: Elsa.Expressions
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions/Models/ExpressionExecutionContext.cs
Line coverage
83%
Covered lines: 35
Uncovered lines: 7
Coverable lines: 42
Total lines: 137
Line coverage: 83.3%
Branch coverage
72%
Covered branches: 13
Total branches: 18
Branch coverage: 72.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
get_ServiceProvider()100%11100%
get_Memory()100%11100%
get_TransientProperties()100%11100%
get_ParentContext()100%11100%
get_CancellationToken()100%11100%
GetBlock(...)100%11100%
GetBlock(...)50%22100%
TryGetBlock(...)50%22100%
Get(...)100%210%
Get(...)100%11100%
TryGet(...)50%2260%
Get(...)100%210%
Get(...)100%210%
Set(...)100%210%
Set(...)83.33%66100%
GetRequiredService()100%11100%
GetBlockInternal(...)83.33%6688.88%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions/Models/ExpressionExecutionContext.cs

#LineLine coverage
 1using Elsa.Expressions.Helpers;
 2using Microsoft.Extensions.DependencyInjection;
 3
 4namespace Elsa.Expressions.Models;
 5
 6/// <summary>
 7/// Provides context to workflow expressions.
 8/// </summary>
 28959public class ExpressionExecutionContext(
 289510    IServiceProvider serviceProvider,
 289511    MemoryRegister memory,
 289512    ExpressionExecutionContext? parentContext = null,
 289513    IDictionary<object, object>? transientProperties = null,
 289514    Action? onChange = null,
 289515    CancellationToken cancellationToken = default)
 16{
 17    /// <summary>
 18    /// A scoped service provider.
 19    /// </summary>
 291220    public IServiceProvider ServiceProvider { get; } = serviceProvider;
 21
 22    /// <summary>
 23    /// A shared register of computer memory.
 24    /// </summary>
 1592325    public MemoryRegister Memory { get; } = memory;
 26
 27    /// <summary>
 28    /// A dictionary of transient properties.
 29    /// </summary>
 730430    public IDictionary<object, object> TransientProperties { get; set; } = transientProperties ?? new Dictionary<object,
 31
 32    /// <summary>
 33    /// Provides access to the parent <see cref="ExpressionExecutionContext"/>, if there is any.
 34    /// </summary>
 1048035    public ExpressionExecutionContext? ParentContext { get; set; } = parentContext;
 36
 37    /// <summary>
 38    /// A cancellation token.
 39    /// </summary>
 290940    public CancellationToken CancellationToken { get; } = cancellationToken;
 41
 42    /// <summary>
 43    /// Returns the <see cref="MemoryBlock"/> pointed to by the specified memory block reference.
 44    /// </summary>
 21745    public MemoryBlock GetBlock(Func<MemoryBlockReference> blockReference) => GetBlock(blockReference());
 46
 47    /// <summary>
 48    /// Returns the <see cref="MemoryBlock"/> pointed to by the specified memory block reference.
 49    /// </summary>
 24350    public MemoryBlock GetBlock(MemoryBlockReference blockReference) => GetBlockInternal(blockReference) ?? throw new Ex
 51
 52    /// <summary>
 53    /// Returns the <see cref="MemoryBlock"/> pointed to by the specified memory block reference.
 54    /// </summary>
 55    public bool TryGetBlock(MemoryBlockReference blockReference, out MemoryBlock block)
 56    {
 94657        var b = GetBlockInternal(blockReference);
 94658        block = b ?? null!;
 94659        return b != null;
 60    }
 61
 62    /// <summary>
 63    /// Returns the value of the memory block pointed to by the specified memory block reference.
 64    /// </summary>
 065    public object? Get(Func<MemoryBlockReference> blockReference) => Get(blockReference());
 66
 67    /// <summary>
 68    /// Returns the value of the memory block pointed to by the specified memory block reference.
 69    /// </summary>
 2670    public object? Get(MemoryBlockReference blockReference) => GetBlock(blockReference).Value;
 71
 72    /// <summary>
 73    /// Returns the value of the memory block pointed to by the specified memory block reference.
 74    /// </summary>
 75    public bool TryGet(MemoryBlockReference blockReference, out object? value)
 76    {
 277        if (TryGetBlock(blockReference, out var block))
 78        {
 279            value = block.Value;
 280            return true;
 81        }
 82
 083        value = null;
 084        return false;
 85    }
 86
 87    /// <summary>
 88    /// Returns the value of the memory block pointed to by the specified memory block reference.
 89    /// </summary>
 090    public T? Get<T>(Func<MemoryBlockReference> blockReference) => Get<T>(blockReference());
 91
 92    /// <summary>
 93    /// Returns the value of the memory block pointed to by the specified memory block reference.
 94    /// </summary>
 095    public T? Get<T>(MemoryBlockReference blockReference) => Get(blockReference).ConvertTo<T>();
 96
 97    /// <summary>
 98    /// Sets the value of the memory block pointed to by the specified memory block reference.
 99    /// </summary>
 0100    public void Set(Func<MemoryBlockReference> blockReference, object? value, Action<MemoryBlock>? configure = null) => 
 101
 102    /// <summary>
 103    /// Sets the value of the memory block pointed to by the specified memory block reference.
 104    /// </summary>
 105    public void Set(MemoryBlockReference blockReference, object? value, Action<MemoryBlock>? configure = null)
 106    {
 1615107        var block = GetBlockInternal(blockReference) ?? Memory.Declare(blockReference);
 1615108        block.Value = value;
 1615109        configure?.Invoke(block);
 1615110        onChange?.Invoke();
 1413111    }
 112
 113    /// <summary>
 114    /// Returns the service of the specified type.
 115    /// </summary>
 17116    public T GetRequiredService<T>() where T : notnull => ServiceProvider.GetRequiredService<T>();
 117
 118    private MemoryBlock? GetBlockInternal(MemoryBlockReference blockReference)
 119    {
 2804120        if (blockReference.Id == null!)
 0121            return null;
 122
 2804123        var currentContext = this;
 124
 9926125        while (currentContext != null)
 126        {
 8338127            var register = currentContext.Memory;
 128
 8338129            if (register.TryGetBlock(blockReference.Id, out var block))
 1216130                return block;
 131
 7122132            currentContext = currentContext.ParentContext;
 133        }
 134
 1588135        return null;
 136    }
 137}