| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Elsa.Features; |
| | | 3 | | using Elsa.Features.Services; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides extension methods to <see cref="IServiceCollection"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class ModuleExtensions |
| | | 12 | | { |
| | 1 | 13 | | private static readonly IDictionary<IServiceCollection, IModule> Modules = new ConcurrentDictionary<IServiceCollecti |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a new Elsa module and adds the <see cref="ElsaFeature"/> to it. |
| | | 17 | | /// </summary> |
| | | 18 | | public static IModule AddElsa(this IServiceCollection services, Action<IModule>? configure = null) |
| | | 19 | | { |
| | 1 | 20 | | var module = services.GetOrCreateModule(); |
| | 2 | 21 | | module.Configure<AppFeature>(app => app.Configurator += configure); |
| | 1 | 22 | | module.Apply(); |
| | | 23 | | |
| | 1 | 24 | | return module; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Configures the Elsa module. |
| | | 29 | | /// </summary> |
| | | 30 | | public static IModule ConfigureElsa(this IServiceCollection services, Action<IModule>? configure = null) |
| | | 31 | | { |
| | 0 | 32 | | var module = services.GetOrCreateModule(); |
| | | 33 | | |
| | 0 | 34 | | if(configure != null) |
| | 0 | 35 | | module.Configure<AppFeature>(app => app.Configurator += configure); |
| | | 36 | | |
| | 0 | 37 | | return module; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private static IModule GetOrCreateModule(this IServiceCollection services) |
| | | 41 | | { |
| | 1 | 42 | | if(Modules.TryGetValue(services, out var module)) |
| | 0 | 43 | | return module; |
| | | 44 | | |
| | 1 | 45 | | module = services.CreateModule(); |
| | | 46 | | |
| | 1 | 47 | | Modules[services] = module; |
| | 1 | 48 | | return module; |
| | | 49 | | } |
| | | 50 | | } |