| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | // Backwards compatibility for the old namespace. |
| | | 7 | | namespace Elsa.Workflows.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A storage driver that stores objects in the workflow state itself. |
| | | 11 | | /// </summary> |
| | | 12 | | [Display(Name = "Workflow")] |
| | | 13 | | [Obsolete("This is no longer used and will be removed in a future version. Use the WorkflowInstanceStorageDriver instead |
| | | 14 | | public class WorkflowStorageDriver : IStorageDriver |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The key used to store the variables in the workflow state. |
| | | 18 | | /// </summary> |
| | | 19 | | public const string VariablesDictionaryStateKey = "PersistentVariablesDictionary"; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 0 | 22 | | public double Priority => -1; |
| | | 23 | | /// <inheritdoc /> |
| | 0 | 24 | | public IEnumerable<string> Tags => []; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public ValueTask WriteAsync(string id, object value, StorageDriverContext context) |
| | | 28 | | { |
| | 0 | 29 | | UpdateVariablesDictionary(context, dictionary => dictionary[id] = value); |
| | 0 | 30 | | return ValueTask.CompletedTask; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public ValueTask<object?> ReadAsync(string id, StorageDriverContext context) |
| | | 35 | | { |
| | 0 | 36 | | var dictionary = GetVariablesDictionary(context); |
| | 0 | 37 | | var value = dictionary.TryGetValue(id, out var v) ? v : default; |
| | 0 | 38 | | return new(value); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public ValueTask DeleteAsync(string id, StorageDriverContext context) |
| | | 43 | | { |
| | 2 | 44 | | UpdateVariablesDictionary(context, dictionary => dictionary.Remove(id)); |
| | 1 | 45 | | return ValueTask.CompletedTask; |
| | | 46 | | } |
| | | 47 | | |
| | 2 | 48 | | private IDictionary<string, object> GetVariablesDictionary(StorageDriverContext context) => context.ExecutionContext |
| | 1 | 49 | | private void SetVariablesDictionary(StorageDriverContext context, IDictionary<string, object> dictionary) => context |
| | | 50 | | |
| | | 51 | | private void UpdateVariablesDictionary(StorageDriverContext context, Action<IDictionary<string, object>> update) |
| | | 52 | | { |
| | 1 | 53 | | var dictionary = GetVariablesDictionary(context); |
| | 1 | 54 | | update(dictionary); |
| | 1 | 55 | | SetVariablesDictionary(context, dictionary); |
| | 1 | 56 | | } |
| | | 57 | | } |