2022-05-26 10:47:31 +00:00
using Microsoft.Extensions.DependencyInjection ;
namespace Elsa.Expressions.Models ;
public class ExpressionExecutionContext
{
private readonly IServiceProvider _serviceProvider ;
public ExpressionExecutionContext (
IServiceProvider serviceProvider ,
2022-06-04 18:03:40 +00:00
MemoryRegister memory ,
2022-05-26 10:47:31 +00:00
ExpressionExecutionContext ? parentContext = default ,
IDictionary < object , object > ? applicationProperties = default ,
CancellationToken cancellationToken = default )
{
_serviceProvider = serviceProvider ;
2022-06-04 18:03:40 +00:00
Memory = memory ;
2022-05-26 10:47:31 +00:00
ApplicationProperties = applicationProperties ? ? new Dictionary < object , object > ( ) ;
ParentContext = parentContext ;
2022-06-04 16:52:58 +00:00
2022-05-26 10:47:31 +00:00
CancellationToken = cancellationToken ;
}
/// <summary>
/// A shared register of computer memory.
/// </summary>
2022-06-04 18:03:40 +00:00
public MemoryRegister Memory { get ; }
2022-05-26 10:47:31 +00:00
public IDictionary < object , object > ApplicationProperties { get ; set ; }
public ExpressionExecutionContext ? ParentContext { get ; set ; }
public CancellationToken CancellationToken { get ; }
2022-06-04 18:01:20 +00:00
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 < T > ( MemoryReference reference ) = > ( T ) Get ( reference ) ;
2022-05-26 10:47:31 +00:00
2022-06-04 18:01:20 +00:00
public void Set ( MemoryReference reference , object? value )
2022-05-26 10:47:31 +00:00
{
2022-06-04 18:03:40 +00:00
var datum = GetMemoryDatumInternal ( reference ) ? ? Memory . Declare ( reference ) ;
2022-06-04 16:52:58 +00:00
datum . Value = value ;
2022-05-26 10:47:31 +00:00
}
public T GetRequiredService < T > ( ) where T : notnull = > _serviceProvider . GetRequiredService < T > ( ) ;
2022-06-04 16:52:58 +00:00
//private MemoryDatum? GetMemoryDatumInternal(MemoryDatumReference locationReference) => MemoryRegister.TryGetMemoryDatum(locationReference.Id, out var location) ? location : ParentContext?.GetMemoryDatumInternal(locationReference);
2022-06-04 18:03:40 +00:00
private MemoryBlock ? GetMemoryDatumInternal ( MemoryReference reference ) = > Memory . TryGetMemoryDatum ( reference . Id , out var location ) ? location : default ;
2022-05-26 10:47:31 +00:00
}