| | | 1 | | using CShells.Features; |
| | | 2 | | using Elsa.Common.Services; |
| | | 3 | | using Elsa.KeyValues.Contracts; |
| | | 4 | | using Elsa.KeyValues.Entities; |
| | | 5 | | using Elsa.KeyValues.Stores; |
| | | 6 | | using Elsa.Platform.PackageManifest.Generator.Hints; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | |
| | | 11 | | namespace Elsa.KeyValues.ShellFeatures; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Installs and configures key-value store features. |
| | | 15 | | /// </summary> |
| | | 16 | | [ManifestFeatureCategory("Storage")] |
| | | 17 | | [ManifestFeatureCategory("Infrastructure")] |
| | | 18 | | [ShellFeature( |
| | | 19 | | DisplayName = "Key-Value Store", |
| | | 20 | | Description = "Provides key-value storage capabilities for workflows")] |
| | | 21 | | [UsedImplicitly] |
| | | 22 | | public class KeyValueFeature : IShellFeature |
| | | 23 | | { |
| | 0 | 24 | | private static readonly Func<IServiceProvider, IKeyValueStore> DefaultKeyValueStore = sp => ActivatorUtilities.Creat |
| | 0 | 25 | | private Func<IServiceProvider, IKeyValueStore> _keyValueStore = DefaultKeyValueStore; |
| | | 26 | | private bool? _registerMemoryStore; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// A factory that instantiates an <see cref="IKeyValueStore"/>. |
| | | 30 | | /// </summary> |
| | | 31 | | public Func<IServiceProvider, IKeyValueStore> KeyValueStore |
| | | 32 | | { |
| | 0 | 33 | | get => _keyValueStore; |
| | 0 | 34 | | set => _keyValueStore = value; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Whether to register the in-memory backing store used by <see cref="MemoryKeyValueStore"/>. |
| | | 39 | | /// </summary> |
| | | 40 | | public bool RegisterMemoryStore |
| | | 41 | | { |
| | 0 | 42 | | get => _registerMemoryStore ?? ReferenceEquals(KeyValueStore, DefaultKeyValueStore); |
| | 0 | 43 | | set => _registerMemoryStore = value; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public void ConfigureServices(IServiceCollection services) |
| | | 47 | | { |
| | 0 | 48 | | if (RegisterMemoryStore) |
| | 0 | 49 | | services.TryAddSingleton<MemoryStore<SerializedKeyValuePair>>(); |
| | | 50 | | |
| | 0 | 51 | | services.AddScoped<IKeyValueStore>(KeyValueStore); |
| | 0 | 52 | | } |
| | | 53 | | } |