using Microsoft.Extensions.DependencyInjection; namespace Elsa.Expressions.Models; public class ExpressionExecutionContext { private readonly IServiceProvider _serviceProvider; public ExpressionExecutionContext( IServiceProvider serviceProvider, MemoryRegister memory, ExpressionExecutionContext? parentContext = default, IDictionary? applicationProperties = default, CancellationToken cancellationToken = default) { _serviceProvider = serviceProvider; Memory = memory; ApplicationProperties = applicationProperties ?? new Dictionary(); ParentContext = parentContext; CancellationToken = cancellationToken; } /// /// A shared register of computer memory. /// public MemoryRegister Memory { get; } public IDictionary ApplicationProperties { get; set; } public ExpressionExecutionContext? ParentContext { get; set; } public CancellationToken CancellationToken { get; } public MemoryBlock GetDatum(MemoryReference reference) => GetMemoryDatumInternal(reference) ?? throw new Exception($"Failed to retrieve memory block with reference {reference.Id}"); public object Get(MemoryReference reference) => GetDatum(reference).Value!; public T Get(MemoryReference reference) => (T)Get(reference); public void Set(MemoryReference reference, object? value) { var datum = GetMemoryDatumInternal(reference) ?? Memory.Declare(reference); datum.Value = value; } public T GetRequiredService() where T : notnull => _serviceProvider.GetRequiredService(); //private MemoryDatum? GetMemoryDatumInternal(MemoryDatumReference locationReference) => MemoryRegister.TryGetMemoryDatum(locationReference.Id, out var location) ? location : ParentContext?.GetMemoryDatumInternal(locationReference); private MemoryBlock? GetMemoryDatumInternal(MemoryReference reference) => Memory.TryGetMemoryDatum(reference.Id, out var location) ? location : default; }