| | | 1 | | using Elsa.Common.Services; |
| | | 2 | | using Elsa.Features.Abstractions; |
| | | 3 | | using Elsa.Features.Services; |
| | | 4 | | using Elsa.KeyValues.Contracts; |
| | | 5 | | using Elsa.KeyValues.Entities; |
| | | 6 | | using Elsa.KeyValues.Stores; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 9 | | |
| | | 10 | | namespace Elsa.KeyValues.Features; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Installs and configures instance management features. |
| | | 14 | | /// </summary> |
| | | 15 | | public class KeyValueFeature : FeatureBase |
| | | 16 | | { |
| | 1 | 17 | | private static readonly Func<IServiceProvider, IKeyValueStore> DefaultKeyValueStore = sp => ActivatorUtilities.Creat |
| | 3 | 18 | | private Func<IServiceProvider, IKeyValueStore> _keyValueStore = DefaultKeyValueStore; |
| | | 19 | | private bool? _registerMemoryStore; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 3 | 22 | | public KeyValueFeature(IModule module) : base(module) |
| | | 23 | | { |
| | 3 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// A factory that instantiates an <see cref="IKeyValueStore"/>. |
| | | 28 | | /// </summary> |
| | | 29 | | public Func<IServiceProvider, IKeyValueStore> KeyValueStore |
| | | 30 | | { |
| | 6 | 31 | | get => _keyValueStore; |
| | 3 | 32 | | set => _keyValueStore = value; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Whether to register the in-memory backing store used by <see cref="MemoryKeyValueStore"/>. |
| | | 37 | | /// </summary> |
| | | 38 | | public bool RegisterMemoryStore |
| | | 39 | | { |
| | 3 | 40 | | get => _registerMemoryStore ?? ReferenceEquals(KeyValueStore, DefaultKeyValueStore); |
| | 0 | 41 | | set => _registerMemoryStore = value; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public override void Apply() |
| | | 46 | | { |
| | 3 | 47 | | if (RegisterMemoryStore) |
| | 0 | 48 | | Services.TryAddSingleton<MemoryStore<SerializedKeyValuePair>>(); |
| | | 49 | | |
| | 3 | 50 | | Services.AddScoped<IKeyValueStore>(KeyValueStore); |
| | 3 | 51 | | } |
| | | 52 | | } |